Repository: google/ko
Pull request #341 (2021-04-20 19:00:23)

Plumb through context in kind.Tag and kind.Write

codecov-commenter: # [Codecov](https://codecov.io/gh/google/ko/pull/341?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#341](https://codecov.io/gh/google/ko/pull/341?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (a982e9a) into [main](https://codecov.io/gh/google/ko/commit/d4987345562115ab5b8985301d1f89bf70c91966?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (d498734) will **not change** coverage.
> The diff coverage is `50.00%`.

[![Impacted file tree graph](https://codecov.io/gh/google/ko/pull/341/graphs/tree.svg?width=650&height=150&src=pr&token=UhqMbLrA3r&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)](https://codecov.io/gh/google/ko/pull/341?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)

```diff
@@           Coverage Diff           @@
##             main     #341   +/-   ##
=======================================
  Coverage   38.24%   38.24%           
=======================================
  Files          33       33           
  Lines        1527     1527           
=======================================
  Hits          584      584           
  Misses        849      849           
  Partials       94       94           
```


| [Impacted Files](https://codecov.io/gh/google/ko/pull/341?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Coverage Δ | |
|---|---|---|
| [pkg/publish/kind.go](https://codecov.io/gh/google/ko/pull/341/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-cGtnL3B1Ymxpc2gva2luZC5nbw==) | `0.00% <0.00%> (ø)` | |
| [pkg/publish/kind/write.go](https://codecov.io/gh/google/ko/pull/341/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-cGtnL3B1Ymxpc2gva2luZC93cml0ZS5nbw==) | `82.85% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/google/ko/pull/341?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/google/ko/pull/341?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google). Last update [d498734...a982e9a](https://codecov.io/gh/google/ko/pull/341?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).


Patch:
diff --git a/pkg/publish/kind.go b/pkg/publish/kind.go
index 48f7598c..f20303cb 100644
--- a/pkg/publish/kind.go
+++ b/pkg/publish/kind.go
@@ -46,7 +46,7 @@ func NewKindPublisher(namer Namer, tags []string) Interface {
 }
 
 // Publish implements publish.Interface.
-func (t *kindPublisher) Publish(_ context.Context, br build.Result, s string) (name.Reference, error) {
+func (t *kindPublisher) 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)
@@ -102,7 +102,7 @@ func (t *kindPublisher) Publish(_ context.Context, br build.Result, s string) (n
 	}
 
 	log.Printf("Loading %v", digestTag)
-	if err := kind.Write(digestTag, img); err != nil {
+	if err := kind.Write(ctx, digestTag, img); err != nil {
 		return nil, err
 	}
 	log.Printf("Loaded %v", digestTag)
@@ -114,7 +114,7 @@ func (t *kindPublisher) Publish(_ context.Context, br build.Result, s string) (n
 			return nil, err
 		}
 
-		if err := kind.Tag(digestTag, tag); err != nil {
+		if err := kind.Tag(ctx, digestTag, tag); err != nil {
 			return nil, err
 		}
 		log.Printf("Added tag %v", tagName)
diff --git a/pkg/publish/kind/write.go b/pkg/publish/kind/write.go
index 0e9bb064..179615e3 100644
--- a/pkg/publish/kind/write.go
+++ b/pkg/publish/kind/write.go
@@ -15,6 +15,7 @@
 package kind
 
 import (
+	"context"
 	"fmt"
 	"io"
 	"os"
@@ -43,9 +44,9 @@ var GetProvider = func() provider {
 }
 
 // Tag adds a tag to an already existent image.
-func Tag(src, dest name.Tag) error {
+func Tag(ctx context.Context, src, dest name.Tag) error {
 	return onEachNode(func(n nodes.Node) error {
-		cmd := n.Command("ctr", "--namespace=k8s.io", "images", "tag", "--force", src.String(), dest.String())
+		cmd := n.CommandContext(ctx, "ctr", "--namespace=k8s.io", "images", "tag", "--force", src.String(), dest.String())
 		if err := cmd.Run(); err != nil {
 			return fmt.Errorf("failed to tag image: %w", err)
 		}
@@ -54,7 +55,7 @@ func Tag(src, dest name.Tag) error {
 }
 
 // Write saves the image into the kind nodes as the given tag.
-func Write(tag name.Tag, img v1.Image) error {
+func Write(ctx context.Context, tag name.Tag, img v1.Image) error {
 	return onEachNode(func(n nodes.Node) error {
 		pr, pw := io.Pipe()
 
@@ -63,7 +64,7 @@ func Write(tag name.Tag, img v1.Image) error {
 			return pw.CloseWithError(tarball.Write(tag, img, pw))
 		})
 
-		cmd := n.Command("ctr", "--namespace=k8s.io", "images", "import", "-").SetStdin(pr)
+		cmd := n.CommandContext(ctx, "ctr", "--namespace=k8s.io", "images", "import", "-").SetStdin(pr)
 		if err := cmd.Run(); err != nil {
 			return fmt.Errorf("failed to load image to node %q: %w", n, err)
 		}


Test patch:
diff --git a/pkg/publish/kind/write_test.go b/pkg/publish/kind/write_test.go
index ec08e936..013e9f1e 100644
--- a/pkg/publish/kind/write_test.go
+++ b/pkg/publish/kind/write_test.go
@@ -30,6 +30,7 @@ import (
 )
 
 func TestWrite(t *testing.T) {
+	ctx := context.Background()
 	img, err := random.Image(1024, 1)
 	if err != nil {
 		t.Fatalf("random.Image() = %v", err)
@@ -46,7 +47,7 @@ func TestWrite(t *testing.T) {
 		return &fakeProvider{nodes: []nodes.Node{n1, n2}}
 	}
 
-	if err := Write(tag, img); err != nil {
+	if err := Write(ctx, tag, img); err != nil {
 		t.Fatalf("Write() = %v", err)
 	}
 
@@ -64,6 +65,7 @@ func TestWrite(t *testing.T) {
 }
 
 func TestTag(t *testing.T) {
+	ctx := context.Background()
 	oldTag, err := name.NewTag("kind.local/test:test")
 	if err != nil {
 		t.Fatalf("name.NewTag() = %v", err)
@@ -80,7 +82,7 @@ func TestTag(t *testing.T) {
 		return &fakeProvider{nodes: []nodes.Node{n1, n2}}
 	}
 
-	if err := Tag(oldTag, newTag); err != nil {
+	if err := Tag(ctx, oldTag, newTag); err != nil {
 		t.Fatalf("Tag() = %v", err)
 	}
 
@@ -98,6 +100,7 @@ func TestTag(t *testing.T) {
 }
 
 func TestFailWithNoNodes(t *testing.T) {
+	ctx := context.Background()
 	img, err := random.Image(1024, 1)
 	if err != nil {
 		panic(err)
@@ -117,15 +120,16 @@ func TestFailWithNoNodes(t *testing.T) {
 		return &fakeProvider{}
 	}
 
-	if err := Write(newTag, img); err == nil {
+	if err := Write(ctx, newTag, img); err == nil {
 		t.Fatal("Write() = nil, wanted an error")
 	}
-	if err := Tag(oldTag, newTag); err == nil {
+	if err := Tag(ctx, oldTag, newTag); err == nil {
 		t.Fatal("Tag() = nil, wanted an error")
 	}
 }
 
 func TestFailCommands(t *testing.T) {
+	ctx := context.Background()
 	img, err := random.Image(1024, 1)
 	if err != nil {
 		panic(err)
@@ -149,10 +153,10 @@ func TestFailCommands(t *testing.T) {
 		return &fakeProvider{nodes: []nodes.Node{n1, n2}}
 	}
 
-	if err := Write(newTag, img); !errors.Is(err, errTest) {
+	if err := Write(ctx, newTag, img); !errors.Is(err, errTest) {
 		t.Fatalf("Write() = %v, want %v", err, errTest)
 	}
-	if err := Tag(oldTag, newTag); !errors.Is(err, errTest) {
+	if err := Tag(ctx, oldTag, newTag); !errors.Is(err, errTest) {
 		t.Fatalf("Write() = %v, want %v", err, errTest)
 	}
 }
@@ -171,7 +175,7 @@ type fakeNode struct {
 	err  error
 }
 
-func (f *fakeNode) Command(cmd string, args ...string) exec.Cmd {
+func (f *fakeNode) CommandContext(ctx context.Context, cmd string, args ...string) exec.Cmd {
 	command := &fakeCmd{
 		cmd: strings.Join(append([]string{cmd}, args...), " "),
 		err: f.err,
@@ -185,10 +189,10 @@ func (f *fakeNode) String() string {
 }
 
 // The following functions are not used by our code at all.
-func (f *fakeNode) CommandContext(context.Context, string, ...string) exec.Cmd { return nil }
-func (f *fakeNode) Role() (string, error)                                      { return "", nil }
-func (f *fakeNode) IP() (ipv4 string, ipv6 string, err error)                  { return "", "", nil }
-func (f *fakeNode) SerialLogs(writer io.Writer) error                          { return nil }
+func (f *fakeNode) Command(string, ...string) exec.Cmd        { return nil }
+func (f *fakeNode) Role() (string, error)                     { return "", nil }
+func (f *fakeNode) IP() (ipv4 string, ipv6 string, err error) { return "", "", nil }
+func (f *fakeNode) SerialLogs(writer io.Writer) error         { return nil }
 
 type fakeCmd struct {
 	cmd   string