Skip to content

Commit

Permalink
Merge pull request #135 from imjasonh/dont-publish
Browse files Browse the repository at this point in the history
don't publish on ko_build read
  • Loading branch information
imjasonh authored Apr 2, 2024
2 parents f6d4e53 + ff52adc commit da40ce2
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
54 changes: 37 additions & 17 deletions internal/provider/resource_ko_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"log"
"os"
"strconv"
"sync"
Expand Down Expand Up @@ -202,31 +201,48 @@ func (o *buildOptions) makeBuilder(ctx context.Context) (*build.Caching, error)

var baseImages sync.Map // Cache of base image lookups.

func doBuild(ctx context.Context, opts buildOptions) (string, error) {
// doBuild builds the image and returns the built image, and the full name.Reference by digest that the image would be pushed to.
//
// doBuild doesn't publish images, use doPublish to publish the build.Result that doBuild returns.
func doBuild(ctx context.Context, opts buildOptions) (build.Result, string, error) {
if opts.imageRepo == "" {
return "", errors.New("one of KO_DOCKER_REPO env var, or provider `docker_repo` or `repo`, or image resource `repo` must be set")
return nil, "", errors.New("one of KO_DOCKER_REPO env var, or provider `docker_repo` or `repo`, or image resource `repo` must be set")
}

b, err := opts.makeBuilder(ctx)
if err != nil {
return "", fmt.Errorf("NewGo: %w", err)
return nil, "", fmt.Errorf("NewGo: %w", err)
}
r, err := b.Build(ctx, opts.ip)
res, err := b.Build(ctx, opts.ip)
if err != nil {
return "", fmt.Errorf("build: %w", err)
return nil, "", fmt.Errorf("build: %w", err)
}
dig, err := res.Digest()
if err != nil {
return nil, "", fmt.Errorf("digest: %w", err)
}
ref, err := name.ParseReference(namer(opts)(opts.imageRepo, opts.ip))
if err != nil {
return nil, "", fmt.Errorf("ParseReference: %w", err)
}

return res, ref.Context().Digest(dig.String()).String(), nil
}

func namer(opts buildOptions) publish.Namer {
return options.MakeNamer(&options.PublishOptions{
DockerRepo: opts.imageRepo,
Bare: opts.bare,
PreserveImportPaths: !opts.bare,
})
}

func doPublish(ctx context.Context, r build.Result, opts buildOptions) (string, error) {
kc := keychain
if opts.auth != nil {
kc = authn.NewMultiKeychain(staticKeychain{opts.imageRepo, opts.auth}, kc)
}
po := []publish.Option{publish.WithAuthFromKeychain(kc)}
if opts.bare {
po = append(po, publish.WithNamer(options.MakeNamer(&options.PublishOptions{
DockerRepo: opts.imageRepo,
Bare: true,
})))
}
po := []publish.Option{publish.WithAuthFromKeychain(kc), publish.WithNamer(namer(opts))}

p, err := publish.NewDefault(opts.imageRepo, po...)
if err != nil {
Expand Down Expand Up @@ -284,10 +300,14 @@ func resourceKoBuildCreate(ctx context.Context, d *schema.ResourceData, meta int
return diag.Errorf("configuring provider: %v", err)
}

ref, err := doBuild(ctx, fromData(d, po))
res, _, err := doBuild(ctx, fromData(d, po))
if err != nil {
return diag.Errorf("[id=%s] create doBuild: %v", d.Id(), err)
}
ref, err := doPublish(ctx, res, fromData(d, po))
if err != nil {
return diag.Errorf("[id=%s] create doPublish: %v", d.Id(), err)
}

_ = d.Set("image_ref", ref)
d.SetId(ref)
Expand All @@ -300,7 +320,7 @@ func resourceKoBuildRead(ctx context.Context, d *schema.ResourceData, meta inter
return diag.Errorf("configuring provider: %v", err)
}

ref, err := doBuild(ctx, fromData(d, po))
_, ref, err := doBuild(ctx, fromData(d, po))
if err != nil {
// Check for conditions that might indicate that the underlying module has been deleted.
// This is not an exhaustive list, but is a best effort check to see if the build failed because a deletion.
Expand All @@ -314,9 +334,9 @@ func resourceKoBuildRead(ctx context.Context, d *schema.ResourceData, meta inter

_ = d.Set("image_ref", ref)
if ref != d.Id() {
d.SetId("")
d.SetId("") // triggers create on next apply.
} else {
log.Println("image not changed")
d.SetId(ref)
}
return nil
}
Expand Down
24 changes: 24 additions & 0 deletions internal/provider/resource_ko_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,27 @@ func TestAccResourceKoBuild_ProviderRepo(t *testing.T) {
}},
})
}

func TestAccResourceKoBuild_PlanNoPush(t *testing.T) {
// Don't run a registry at this endpoint, we want to test that we don't push anything.
t.Setenv("KO_DOCKER_REPO", "localhost:12345/test")

// Test that the repo attribute of the provider is respected, and overrides
// the KO_DOCKER_REPO.
// When configured in the provider, the importpath is appended to the image ref.
resource.Test(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{{
Config: `
resource "ko_build" "foo" {
importpath = "github.com/ko-build/terraform-provider-ko/cmd/test"
}
`,
PlanOnly: true, // Only plan, and expect a diff.
ExpectNonEmptyPlan: true,
Check: resource.ComposeTestCheckFunc(
resource.TestMatchResourceAttr("ko_build.foo", "image_ref", regexp.MustCompile("^localhost:12345/github.com/ko-build/terraform-provider-ko/cmd/test@sha256:")),
),
}},
})
}

0 comments on commit da40ce2

Please sign in to comment.