Skip to content

Commit

Permalink
Plumb through context in kind.Tag and kind.Write (ko-build#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh authored Apr 20, 2021
1 parent d498734 commit 75ab991
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
6 changes: 3 additions & 3 deletions pkg/publish/kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions pkg/publish/kind/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package kind

import (
"context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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()

Expand All @@ -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)
}
Expand Down
26 changes: 15 additions & 11 deletions pkg/publish/kind/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand All @@ -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)
Expand All @@ -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)
}

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
}
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 75ab991

Please sign in to comment.