Skip to content

Commit

Permalink
Plumb build.WithTempDir through apko (#403)
Browse files Browse the repository at this point in the history
The layers we build aren't getting cleaned up because we don't plumb
this properly.

Signed-off-by: Jon Johnson <jon.johnson@chainguard.dev>
  • Loading branch information
jonjohnsonjr authored Oct 30, 2024
1 parent c0c31e7 commit 5999cbb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
9 changes: 2 additions & 7 deletions internal/provider/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,7 @@ type imagesbom struct {
predicateSHA256 string
}

func doBuild(ctx context.Context, data BuildResourceModel) (v1.Hash, coci.SignedEntity, map[string]imagesbom, error) {
tempDir, err := os.MkdirTemp("", "apko-*")
if err != nil {
return v1.Hash{}, nil, nil, fmt.Errorf("failed to create temporary directory: %w", err)
}
defer os.RemoveAll(tempDir)

func doBuild(ctx context.Context, data BuildResourceModel, tempDir string) (v1.Hash, coci.SignedEntity, map[string]imagesbom, error) {
var ic types.ImageConfiguration
if diags := assignValue(data.Config, &ic); diags.HasError() {
return v1.Hash{}, nil, nil, fmt.Errorf("assigning value: %v", diags.Errors())
Expand Down Expand Up @@ -118,6 +112,7 @@ func doBuild(ctx context.Context, data BuildResourceModel) (v1.Hash, coci.Signed
build.WithCache("", false, data.popts.cache),
build.WithSBOMFormats([]string{"spdx"}),
build.WithSBOM(tempDir),
build.WithTempDir(tempDir),
build.WithExtraKeys(data.popts.keyring),
build.WithExtraBuildRepos(data.popts.buildRespositories),
build.WithExtraRuntimeRepos(data.popts.repositories))
Expand Down
19 changes: 17 additions & 2 deletions internal/provider/resource_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"os"

"github.com/chainguard-dev/terraform-provider-oci/pkg/validators"
"github.com/google/go-containerregistry/pkg/name"
Expand Down Expand Up @@ -154,7 +155,14 @@ func (r *BuildResource) Create(ctx context.Context, req resource.CreateRequest,
return
}

digest, se, sboms, err := doBuild(ctx, *data)
tempDir, err := os.MkdirTemp("", "apko-*")
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Errorf("failed to create temporary directory: %w", err).Error())
return
}
defer os.RemoveAll(tempDir)

digest, se, sboms, err := doBuild(ctx, *data, tempDir)
if err != nil {
resp.Diagnostics.AddError("Client Error", err.Error())
return
Expand Down Expand Up @@ -234,7 +242,14 @@ func (r *BuildResource) Update(ctx context.Context, req resource.UpdateRequest,
return
}

digest, _, _, err := doBuild(ctx, *data)
tempDir, err := os.MkdirTemp("", "apko-*")
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Errorf("failed to create temporary directory: %w", err).Error())
return
}
defer os.RemoveAll(tempDir)

digest, _, _, err := doBuild(ctx, *data, tempDir)
if err != nil {
resp.Diagnostics.AddError("Client Error", err.Error())
return
Expand Down

0 comments on commit 5999cbb

Please sign in to comment.