diff --git a/pkg/build/gobuild.go b/pkg/build/gobuild.go index ae6310e476..1da4b9c2c4 100644 --- a/pkg/build/gobuild.go +++ b/pkg/build/gobuild.go @@ -46,7 +46,7 @@ const ( ) // GetBase takes an importpath and returns a base image. -type GetBase func(string) (Result, error) +type GetBase func(context.Context, string) (Result, error) type builder func(context.Context, string, v1.Platform, bool) (string, error) @@ -105,14 +105,14 @@ type modInfo struct { // using go modules, otherwise returns nil. // // Related: https://github.com/golang/go/issues/26504 -func moduleInfo() (*modules, error) { +func moduleInfo(ctx context.Context) (*modules, error) { modules := modules{ deps: make(map[string]*modInfo), } // TODO we read all the output as a single byte array - it may // be possible & more efficient to stream it - output, err := exec.Command("go", "list", "-mod=readonly", "-json", "-m", "all").Output() + output, err := exec.CommandContext(ctx, "go", "list", "-mod=readonly", "-json", "-m", "all").Output() if err != nil { return nil, nil } @@ -149,8 +149,8 @@ func moduleInfo() (*modules, error) { // NewGo returns a build.Interface implementation that: // 1. builds go binaries named by importpath, // 2. containerizes the binary on a suitable base, -func NewGo(options ...Option) (Interface, error) { - module, err := moduleInfo() +func NewGo(ctx context.Context, options ...Option) (Interface, error) { + module, err := moduleInfo(ctx) if err != nil { return nil, err } @@ -579,7 +579,7 @@ func updatePath(cf *v1.ConfigFile) { // Build implements build.Interface func (g *gobuild) Build(ctx context.Context, s string) (Result, error) { // Determine the appropriate base image for this import path. - base, err := g.getBase(s) + base, err := g.getBase(ctx, s) if err != nil { return nil, err } diff --git a/pkg/build/gobuild_test.go b/pkg/build/gobuild_test.go index 61a00b159d..486d0db9c2 100644 --- a/pkg/build/gobuild_test.go +++ b/pkg/build/gobuild_test.go @@ -39,7 +39,7 @@ func TestGoBuildIsSupportedRef(t *testing.T) { t.Fatalf("random.Image() = %v", err) } - ng, err := NewGo(WithBaseImages(func(string) (Result, error) { return base, nil })) + ng, err := NewGo(context.Background(), WithBaseImages(func(context.Context, string) (Result, error) { return base, nil })) if err != nil { t.Fatalf("NewGo() = %v", err) } @@ -88,7 +88,7 @@ func TestGoBuildIsSupportedRefWithModules(t *testing.T) { } opts := []Option{ - WithBaseImages(func(string) (Result, error) { return base, nil }), + WithBaseImages(func(context.Context, string) (Result, error) { return base, nil }), withModuleInfo(mods), withBuildContext(stubBuildContext{ // make all referenced deps commands @@ -99,7 +99,7 @@ func TestGoBuildIsSupportedRefWithModules(t *testing.T) { }), } - ng, err := NewGo(opts...) + ng, err := NewGo(context.Background(), opts...) if err != nil { t.Fatalf("NewGo() = %v", err) } @@ -158,8 +158,9 @@ func TestGoBuildNoKoData(t *testing.T) { creationTime := v1.Time{Time: time.Unix(5000, 0)} ng, err := NewGo( + context.Background(), WithCreationTime(creationTime), - WithBaseImages(func(string) (Result, error) { return base, nil }), + WithBaseImages(func(context.Context, string) (Result, error) { return base, nil }), withBuilder(writeTempFile), ) if err != nil { @@ -399,8 +400,9 @@ func TestGoBuild(t *testing.T) { creationTime := v1.Time{Time: time.Unix(5000, 0)} ng, err := NewGo( + context.Background(), WithCreationTime(creationTime), - WithBaseImages(func(string) (Result, error) { return base, nil }), + WithBaseImages(func(context.Context, string) (Result, error) { return base, nil }), withBuilder(writeTempFile), ) if err != nil { @@ -453,8 +455,9 @@ func TestGoBuildIndex(t *testing.T) { creationTime := v1.Time{Time: time.Unix(5000, 0)} ng, err := NewGo( + context.Background(), WithCreationTime(creationTime), - WithBaseImages(func(string) (Result, error) { return base, nil }), + WithBaseImages(func(context.Context, string) (Result, error) { return base, nil }), withBuilder(writeTempFile), ) if err != nil { @@ -523,8 +526,9 @@ func TestNestedIndex(t *testing.T) { creationTime := v1.Time{Time: time.Unix(5000, 0)} ng, err := NewGo( + context.Background(), WithCreationTime(creationTime), - WithBaseImages(func(string) (Result, error) { return nestedBase, nil }), + WithBaseImages(func(context.Context, string) (Result, error) { return nestedBase, nil }), withBuilder(writeTempFile), ) if err != nil { diff --git a/pkg/commands/apply.go b/pkg/commands/apply.go index 42b3fc150c..55f521ac5a 100644 --- a/pkg/commands/apply.go +++ b/pkg/commands/apply.go @@ -69,7 +69,10 @@ func addApply(topLevel *cobra.Command) { return } - builder, err := makeBuilder(bo) + // Cancel on signals. + ctx := createCancellableContext() + + builder, err := makeBuilder(ctx, bo) if err != nil { log.Fatalf("error creating builder: %v", err) } @@ -97,7 +100,7 @@ func addApply(topLevel *cobra.Command) { // to which we will pipe the resolved files. argv := []string{"apply", "-f", "-"} argv = append(argv, kubectlFlags...) - kubectlCmd := exec.Command("kubectl", argv...) + kubectlCmd := exec.CommandContext(ctx, "kubectl", argv...) // Pass through our environment kubectlCmd.Env = os.Environ() @@ -111,9 +114,6 @@ func addApply(topLevel *cobra.Command) { log.Fatalf("error piping to 'kubectl apply': %v", err) } - // Cancel on signals. - ctx := createCancellableContext() - // Make sure builds are cancelled if kubectl apply fails. g, ctx := errgroup.WithContext(ctx) g.Go(func() error { diff --git a/pkg/commands/config.go b/pkg/commands/config.go index a8e0056af1..e9a3b9b0f5 100644 --- a/pkg/commands/config.go +++ b/pkg/commands/config.go @@ -51,7 +51,7 @@ func getBaseImage(platform string) build.GetBase { } } - return func(s string) (build.Result, error) { + return func(ctx context.Context, s string) (build.Result, error) { s = strings.TrimPrefix(s, build.StrictScheme) // Viper configuration file keys are case insensitive, and are // returned as all lowercase. This means that import paths with @@ -66,6 +66,7 @@ func getBaseImage(platform string) build.GetBase { ropt := []remote.Option{ remote.WithAuthFromKeychain(authn.DefaultKeychain), remote.WithTransport(defaultTransport()), + remote.WithContext(ctx), } // Using --platform=all will use an image index for the base, diff --git a/pkg/commands/create.go b/pkg/commands/create.go index df94fe0882..defb9e1cdc 100644 --- a/pkg/commands/create.go +++ b/pkg/commands/create.go @@ -69,7 +69,10 @@ func addCreate(topLevel *cobra.Command) { return } - builder, err := makeBuilder(bo) + // Cancel on signals. + ctx := createCancellableContext() + + builder, err := makeBuilder(ctx, bo) if err != nil { log.Fatalf("error creating builder: %v", err) } @@ -97,7 +100,7 @@ func addCreate(topLevel *cobra.Command) { // to which we will pipe the resolved files. argv := []string{"create", "-f", "-"} argv = append(argv, kubectlFlags...) - kubectlCmd := exec.Command("kubectl", argv...) + kubectlCmd := exec.CommandContext(ctx, "kubectl", argv...) // Pass through our environment kubectlCmd.Env = os.Environ() @@ -111,9 +114,6 @@ func addCreate(topLevel *cobra.Command) { log.Fatalf("error piping to 'kubectl create': %v", err) } - // Cancel on signals. - ctx := createCancellableContext() - // Make sure builds are cancelled if kubectl create fails. g, ctx := errgroup.WithContext(ctx) g.Go(func() error { diff --git a/pkg/commands/delete.go b/pkg/commands/delete.go index 0576eef712..89f4f93110 100644 --- a/pkg/commands/delete.go +++ b/pkg/commands/delete.go @@ -34,9 +34,12 @@ func passthru(command string) runCmd { return } + // Cancel on signals. + ctx := createCancellableContext() + // Start building a command line invocation by passing // through our arguments to command's CLI. - cmd := exec.Command(command, os.Args[1:]...) + cmd := exec.CommandContext(ctx, command, os.Args[1:]...) // Pass through our environment cmd.Env = os.Environ() diff --git a/pkg/commands/publish.go b/pkg/commands/publish.go index 2c651a1ce2..236dbb94e8 100644 --- a/pkg/commands/publish.go +++ b/pkg/commands/publish.go @@ -58,7 +58,8 @@ func addPublish(topLevel *cobra.Command) { ko publish --local github.com/foo/bar/cmd/baz github.com/foo/bar/cmd/blah`, Args: cobra.MinimumNArgs(1), Run: func(_ *cobra.Command, args []string) { - builder, err := makeBuilder(bo) + ctx := createCancellableContext() + builder, err := makeBuilder(ctx, bo) if err != nil { log.Fatalf("error creating builder: %v", err) } @@ -67,7 +68,6 @@ func addPublish(topLevel *cobra.Command) { log.Fatalf("error creating publisher: %v", err) } defer publisher.Close() - ctx := createCancellableContext() images, err := publishImages(ctx, args, publisher, builder) if err != nil { log.Fatalf("failed to publish images: %v", err) diff --git a/pkg/commands/publisher.go b/pkg/commands/publisher.go index b40dadf560..300e1eed79 100644 --- a/pkg/commands/publisher.go +++ b/pkg/commands/publisher.go @@ -63,7 +63,7 @@ func publishImages(ctx context.Context, importpaths []string, pub publish.Interf if err != nil { return nil, fmt.Errorf("error building %q: %v", importpath, err) } - ref, err := pub.Publish(img, importpath) + ref, err := pub.Publish(ctx, img, importpath) if err != nil { return nil, fmt.Errorf("error publishing %s: %v", importpath, err) } diff --git a/pkg/commands/resolve.go b/pkg/commands/resolve.go index 6a348ffa06..8b81e27416 100644 --- a/pkg/commands/resolve.go +++ b/pkg/commands/resolve.go @@ -56,7 +56,8 @@ func addResolve(topLevel *cobra.Command) { ko resolve --local -f config/`, Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { - builder, err := makeBuilder(bo) + ctx := createCancellableContext() + builder, err := makeBuilder(ctx, bo) if err != nil { log.Fatalf("error creating builder: %v", err) } @@ -65,7 +66,6 @@ func addResolve(topLevel *cobra.Command) { log.Fatalf("error creating publisher: %v", err) } defer publisher.Close() - ctx := createCancellableContext() if err := resolveFilesToWriter(ctx, builder, publisher, fo, so, sto, os.Stdout); err != nil { log.Fatal(err) } diff --git a/pkg/commands/resolver.go b/pkg/commands/resolver.go index b71a932f25..79cc5cd957 100644 --- a/pkg/commands/resolver.go +++ b/pkg/commands/resolver.go @@ -81,12 +81,12 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) { return opts, nil } -func makeBuilder(bo *options.BuildOptions) (*build.Caching, error) { +func makeBuilder(ctx context.Context, bo *options.BuildOptions) (*build.Caching, error) { opt, err := gobuildOptions(bo) if err != nil { return nil, fmt.Errorf("error setting up builder options: %v", err) } - innerBuilder, err := build.NewGo(opt...) + innerBuilder, err := build.NewGo(ctx, opt...) if err != nil { return nil, err } @@ -188,7 +188,7 @@ type nopPublisher struct { namer publish.Namer } -func (n nopPublisher) Publish(br build.Result, s string) (name.Reference, error) { +func (n nopPublisher) Publish(_ context.Context, br build.Result, s string) (name.Reference, error) { h, err := br.Digest() if err != nil { return nil, err diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 2b43de2680..87d85214ad 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -47,6 +47,8 @@ func addRun(topLevel *cobra.Command) { # You can also supply args and flags to the command. ko run ./cmd/baz -- -v arg1 arg2 --yes`, Run: func(cmd *cobra.Command, args []string) { + ctx := createCancellableContext() + // Args after -- are for kubectl, so only consider importPaths before it. importPaths := args dashes := cmd.Flags().ArgsLenAtDash() @@ -63,7 +65,7 @@ func addRun(topLevel *cobra.Command) { kubectlArgs = os.Args[dashes:] } - builder, err := makeBuilder(bo) + builder, err := makeBuilder(ctx, bo) if err != nil { log.Fatalf("error creating builder: %v", err) } @@ -80,7 +82,6 @@ func addRun(topLevel *cobra.Command) { if strings.HasPrefix(ip, "-") { log.Fatalf("expected first arg to be positional, got %q", ip) } - ctx := createCancellableContext() imgs, err := publishImages(ctx, importPaths, publisher, builder) if err != nil { log.Fatalf("failed to publish images: %v", err) @@ -115,7 +116,7 @@ func addRun(topLevel *cobra.Command) { argv = append([]string{"run", pod}, argv...) log.Printf("$ kubectl %s", strings.Join(argv, " ")) - kubectlCmd := exec.Command("kubectl", argv...) + kubectlCmd := exec.CommandContext(ctx, "kubectl", argv...) // Pass through our environment kubectlCmd.Env = os.Environ() diff --git a/pkg/internal/testing/fixed.go b/pkg/internal/testing/fixed.go index 054caee494..fe807a1b28 100644 --- a/pkg/internal/testing/fixed.go +++ b/pkg/internal/testing/fixed.go @@ -66,7 +66,7 @@ func NewFixedPublish(base name.Repository, entries map[string]v1.Hash) publish.I } // Publish implements publish.Interface -func (f *fixedPublish) Publish(_ build.Result, s string) (name.Reference, error) { +func (f *fixedPublish) Publish(_ context.Context, _ build.Result, s string) (name.Reference, error) { s = strings.TrimPrefix(s, build.StrictScheme) h, ok := f.entries[s] if !ok { diff --git a/pkg/internal/testing/fixed_test.go b/pkg/internal/testing/fixed_test.go index 4c7556a368..62f6936302 100644 --- a/pkg/internal/testing/fixed_test.go +++ b/pkg/internal/testing/fixed_test.go @@ -39,7 +39,7 @@ func TestFixedPublish(t *testing.T) { }, }) - fooDigest, err := f.Publish(nil, "foo") + fooDigest, err := f.Publish(context.Background(), nil, "foo") if err != nil { t.Errorf("Publish(foo) = %v", err) } @@ -47,7 +47,7 @@ func TestFixedPublish(t *testing.T) { t.Errorf("Publish(foo) = %q, want %q", got, want) } - barDigest, err := f.Publish(nil, "bar") + barDigest, err := f.Publish(context.Background(), nil, "bar") if err != nil { t.Errorf("Publish(bar) = %v", err) } @@ -55,7 +55,7 @@ func TestFixedPublish(t *testing.T) { t.Errorf("Publish(bar) = %q, want %q", got, want) } - d, err := f.Publish(nil, "baz") + d, err := f.Publish(context.Background(), nil, "baz") if err == nil { t.Errorf("Publish(baz) = %v, want error", d) } diff --git a/pkg/publish/daemon.go b/pkg/publish/daemon.go index a7a200bbf6..2bce7e154e 100644 --- a/pkg/publish/daemon.go +++ b/pkg/publish/daemon.go @@ -15,6 +15,7 @@ package publish import ( + "context" "fmt" "log" "os" @@ -43,7 +44,7 @@ func NewDaemon(namer Namer, tags []string) Interface { } // Publish implements publish.Interface -func (d *demon) Publish(br build.Result, s string) (name.Reference, error) { +func (d *demon) Publish(_ context.Context, br build.Result, s string) (name.Reference, error) { s = strings.TrimPrefix(s, build.StrictScheme) // https://github.com/google/go-containerregistry/issues/212 s = strings.ToLower(s) diff --git a/pkg/publish/daemon_test.go b/pkg/publish/daemon_test.go index e6b9d223ef..a356569f71 100644 --- a/pkg/publish/daemon_test.go +++ b/pkg/publish/daemon_test.go @@ -55,7 +55,7 @@ func TestDaemon(t *testing.T) { } def := NewDaemon(md5Hash, []string{}) - if d, err := def.Publish(img, importpath); err != nil { + if d, err := def.Publish(context.Background(), img, importpath); err != nil { t.Errorf("Publish() = %v", err) } else if got, want := d.String(), md5Hash("ko.local", importpath); !strings.HasPrefix(got, want) { t.Errorf("Publish() = %v, wanted prefix %v", got, want) @@ -72,7 +72,7 @@ func TestDaemonTags(t *testing.T) { } def := NewDaemon(md5Hash, []string{"v2.0.0", "v1.2.3", "production"}) - if d, err := def.Publish(img, importpath); err != nil { + if d, err := def.Publish(context.Background(), img, importpath); err != nil { t.Errorf("Publish() = %v", err) } else if got, want := d.String(), md5Hash("ko.local", importpath); !strings.HasPrefix(got, want) { t.Errorf("Publish() = %v, wanted prefix %v", got, want) diff --git a/pkg/publish/default.go b/pkg/publish/default.go index cb4fe78c2a..c8935a0316 100644 --- a/pkg/publish/default.go +++ b/pkg/publish/default.go @@ -15,6 +15,7 @@ package publish import ( + "context" "fmt" "log" "net/http" @@ -121,12 +122,12 @@ func pushResult(tag name.Tag, br build.Result, opt []remote.Option) error { } // Publish implements publish.Interface -func (d *defalt) Publish(br build.Result, s string) (name.Reference, error) { +func (d *defalt) Publish(ctx context.Context, br build.Result, s string) (name.Reference, error) { s = strings.TrimPrefix(s, build.StrictScheme) // https://github.com/google/go-containerregistry/issues/212 s = strings.ToLower(s) - ro := []remote.Option{remote.WithAuth(d.auth), remote.WithTransport(d.t)} + ro := []remote.Option{remote.WithAuth(d.auth), remote.WithTransport(d.t), remote.WithContext(ctx)} no := []name.Option{} if d.insecure { no = append(no, name.Insecure) diff --git a/pkg/publish/default_test.go b/pkg/publish/default_test.go index 27749f1007..597165fcd4 100644 --- a/pkg/publish/default_test.go +++ b/pkg/publish/default_test.go @@ -15,6 +15,7 @@ package publish import ( + "context" "crypto/md5" "encoding/hex" "fmt" @@ -59,7 +60,7 @@ func TestDefault(t *testing.T) { if err != nil { t.Errorf("NewDefault() = %v", err) } - if d, err := def.Publish(br, build.StrictScheme+importpath); err != nil { + if d, err := def.Publish(context.Background(), br, build.StrictScheme+importpath); err != nil { t.Errorf("Publish() = %v", err) } else if !strings.HasPrefix(d.String(), tag.Repository.String()) { t.Errorf("Publish() = %v, wanted prefix %v", d, tag.Repository) @@ -97,7 +98,7 @@ func TestDefaultWithCustomNamer(t *testing.T) { if err != nil { t.Errorf("NewDefault() = %v", err) } - if d, err := def.Publish(br, build.StrictScheme+importpath); err != nil { + if d, err := def.Publish(context.Background(), br, build.StrictScheme+importpath); err != nil { t.Errorf("Publish() = %v", err) } else if !strings.HasPrefix(d.String(), repoName) { t.Errorf("Publish() = %v, wanted prefix %v", d, tag.Repository) @@ -129,7 +130,7 @@ func TestDefaultWithTags(t *testing.T) { if err != nil { t.Errorf("NewDefault() = %v", err) } - if d, err := def.Publish(br, build.StrictScheme+importpath); err != nil { + if d, err := def.Publish(context.Background(), br, build.StrictScheme+importpath); err != nil { t.Errorf("Publish() = %v", err) } else if !strings.HasPrefix(d.String(), repoName) { t.Errorf("Publish() = %v, wanted prefix %v", d, tag.Repository) @@ -210,7 +211,7 @@ func TestDefaultWithReleaseTag(t *testing.T) { if err != nil { t.Errorf("NewDefault() = %v", err) } - if d, err := def.Publish(img, build.StrictScheme+importpath); err != nil { + if d, err := def.Publish(context.Background(), img, build.StrictScheme+importpath); err != nil { t.Errorf("Publish() = %v", err) } else if !strings.HasPrefix(d.String(), repoName) { t.Errorf("Publish() = %v, wanted prefix %v", d, tag.Repository) diff --git a/pkg/publish/kind.go b/pkg/publish/kind.go index a949dbf34a..48f7598cd5 100644 --- a/pkg/publish/kind.go +++ b/pkg/publish/kind.go @@ -15,6 +15,7 @@ package publish import ( + "context" "fmt" "log" "os" @@ -45,7 +46,7 @@ func NewKindPublisher(namer Namer, tags []string) Interface { } // Publish implements publish.Interface. -func (t *kindPublisher) Publish(br build.Result, s string) (name.Reference, error) { +func (t *kindPublisher) Publish(_ context.Context, br build.Result, s string) (name.Reference, error) { s = strings.TrimPrefix(s, build.StrictScheme) // https://github.com/google/go-containerregistry/issues/212 s = strings.ToLower(s) diff --git a/pkg/publish/layout.go b/pkg/publish/layout.go index 2b2f8dbbe2..d325a33bf2 100644 --- a/pkg/publish/layout.go +++ b/pkg/publish/layout.go @@ -15,6 +15,7 @@ package publish import ( + "context" "fmt" "log" @@ -67,7 +68,7 @@ func (l *LayoutPublisher) writeResult(br build.Result) error { } // Publish implements publish.Interface. -func (l *LayoutPublisher) Publish(br build.Result, s string) (name.Reference, error) { +func (l *LayoutPublisher) Publish(_ context.Context, br build.Result, s string) (name.Reference, error) { log.Printf("Saving %v", s) if err := l.writeResult(br); err != nil { return nil, err diff --git a/pkg/publish/layout_test.go b/pkg/publish/layout_test.go index c15c2a9955..59e34cd0ee 100644 --- a/pkg/publish/layout_test.go +++ b/pkg/publish/layout_test.go @@ -15,6 +15,7 @@ package publish import ( + "context" "io/ioutil" "os" "strings" @@ -40,7 +41,7 @@ func TestLayout(t *testing.T) { if err != nil { t.Errorf("NewLayout() = %v", err) } - if d, err := lp.Publish(img, importpath); err != nil { + if d, err := lp.Publish(context.Background(), img, importpath); err != nil { t.Errorf("Publish() = %v", err) } else if !strings.HasPrefix(d.String(), tmp) { t.Errorf("Publish() = %v, wanted prefix %v", d, tmp) diff --git a/pkg/publish/multi.go b/pkg/publish/multi.go index 6824000596..38c47321e0 100644 --- a/pkg/publish/multi.go +++ b/pkg/publish/multi.go @@ -15,6 +15,7 @@ package publish import ( + "context" "errors" "github.com/google/go-containerregistry/pkg/name" @@ -35,13 +36,13 @@ type multiPublisher struct { } // Publish implements publish.Interface. -func (p *multiPublisher) Publish(br build.Result, s string) (ref name.Reference, err error) { +func (p *multiPublisher) Publish(ctx context.Context, br build.Result, s string) (ref name.Reference, err error) { if len(p.publishers) == 0 { return nil, errors.New("MultiPublisher configured with zero publishers") } for _, pub := range p.publishers { - ref, err = pub.Publish(br, s) + ref, err = pub.Publish(ctx, br, s) if err != nil { return } diff --git a/pkg/publish/multi_test.go b/pkg/publish/multi_test.go index 4112fa29de..71b178f03d 100644 --- a/pkg/publish/multi_test.go +++ b/pkg/publish/multi_test.go @@ -15,6 +15,7 @@ package publish import ( + "context" "fmt" "io/ioutil" "os" @@ -53,7 +54,7 @@ func TestMulti(t *testing.T) { } p := MultiPublisher(lp, tp) - if _, err := p.Publish(img, importpath); err != nil { + if _, err := p.Publish(context.Background(), img, importpath); err != nil { t.Errorf("Publish() = %v", err) } @@ -69,7 +70,7 @@ func TestMulti_Zero(t *testing.T) { } p := MultiPublisher() // No publishers. - if _, err := p.Publish(img, "foo"); err == nil { + if _, err := p.Publish(context.Background(), img, "foo"); err == nil { t.Errorf("Publish() got nil error") } } diff --git a/pkg/publish/publish.go b/pkg/publish/publish.go index d0094958cc..025669fdf7 100644 --- a/pkg/publish/publish.go +++ b/pkg/publish/publish.go @@ -15,6 +15,8 @@ package publish import ( + "context" + "github.com/google/go-containerregistry/pkg/name" "github.com/google/ko/pkg/build" ) @@ -24,7 +26,7 @@ type Interface interface { // Publish uploads the given build.Result to a registry incorporating the // provided string into the image's repository name. Returns the digest // of the published image. - Publish(build.Result, string) (name.Reference, error) + Publish(context.Context, build.Result, string) (name.Reference, error) // Close exists for the tarball implementation so we can // do the whole thing in one write. diff --git a/pkg/publish/shared.go b/pkg/publish/shared.go index f1a6474ca5..bce53bc26e 100644 --- a/pkg/publish/shared.go +++ b/pkg/publish/shared.go @@ -15,6 +15,7 @@ package publish import ( + "context" "sync" "github.com/google/go-containerregistry/pkg/name" @@ -50,7 +51,7 @@ func NewCaching(inner Interface) (Interface, error) { } // Publish implements Interface -func (c *caching) Publish(br build.Result, ref string) (name.Reference, error) { +func (c *caching) Publish(ctx context.Context, br build.Result, ref string) (name.Reference, error) { f := func() *future { // Lock the map of futures. c.m.Lock() @@ -66,7 +67,7 @@ func (c *caching) Publish(br build.Result, ref string) (name.Reference, error) { } // Otherwise create and record a future for publishing "br" to "ref". f := newFuture(func() (name.Reference, error) { - return c.inner.Publish(br, ref) + return c.inner.Publish(ctx, br, ref) }) c.results[ref] = &entry{br: br, f: f} return f diff --git a/pkg/publish/shared_test.go b/pkg/publish/shared_test.go index b7c075fa1a..2d4573d19f 100644 --- a/pkg/publish/shared_test.go +++ b/pkg/publish/shared_test.go @@ -15,6 +15,7 @@ package publish import ( + "context" "testing" "time" @@ -30,7 +31,7 @@ type slowpublish struct { // slowpublish implements Interface var _ Interface = (*slowpublish)(nil) -func (sp *slowpublish) Publish(br build.Result, ref string) (name.Reference, error) { +func (sp *slowpublish) Publish(_ context.Context, br build.Result, ref string) (name.Reference, error) { time.Sleep(sp.sleep) return makeRef() } @@ -55,7 +56,7 @@ func TestCaching(t *testing.T) { img, _ := random.Index(256, 8, 1) start := time.Now() - ref1, err := cb.Publish(img, ref) + ref1, err := cb.Publish(context.Background(), img, ref) if err != nil { t.Errorf("Publish() = %v", err) } @@ -73,7 +74,7 @@ func TestCaching(t *testing.T) { previousDigest = d1 start = time.Now() - ref2, err := cb.Publish(img, ref) + ref2, err := cb.Publish(context.Background(), img, ref) if err != nil { t.Errorf("Publish() = %v", err) } diff --git a/pkg/publish/tarball.go b/pkg/publish/tarball.go index 4691b58aa8..66f37ffe6f 100644 --- a/pkg/publish/tarball.go +++ b/pkg/publish/tarball.go @@ -15,6 +15,7 @@ package publish import ( + "context" "fmt" "log" "strings" @@ -45,7 +46,7 @@ func NewTarball(file, base string, namer Namer, tags []string) Interface { } // Publish implements publish.Interface. -func (t *tar) Publish(br build.Result, s string) (name.Reference, error) { +func (t *tar) Publish(_ context.Context, br build.Result, s string) (name.Reference, error) { s = strings.TrimPrefix(s, build.StrictScheme) // https://github.com/google/go-containerregistry/issues/212 s = strings.ToLower(s) diff --git a/pkg/publish/tarball_test.go b/pkg/publish/tarball_test.go index d2d71dd445..e9c7da2137 100644 --- a/pkg/publish/tarball_test.go +++ b/pkg/publish/tarball_test.go @@ -15,6 +15,7 @@ package publish import ( + "context" "fmt" "io/ioutil" "os" @@ -60,7 +61,7 @@ func TestTarball(t *testing.T) { }} for _, tags := range tagss { tp := NewTarball(fp.Name(), repoName, md5Hash, tags) - if d, err := tp.Publish(img, importpath); err != nil { + if d, err := tp.Publish(context.Background(), img, importpath); err != nil { t.Errorf("Publish() = %v", err) } else if !strings.HasPrefix(d.String(), tag.Repository.String()) { t.Errorf("Publish() = %v, wanted prefix %v", d, tag.Repository) diff --git a/pkg/resolve/resolve.go b/pkg/resolve/resolve.go index d605c41684..5a441bfa59 100644 --- a/pkg/resolve/resolve.go +++ b/pkg/resolve/resolve.go @@ -59,7 +59,7 @@ func ImageReferences(ctx context.Context, docs []*yaml.Node, strict bool, builde if err != nil { return err } - digest, err := publisher.Publish(img, ref) + digest, err := publisher.Publish(ctx, img, ref) if err != nil { return err }