From e072b66bc1d8d2e4ee5dd52d3227aefe64c71f31 Mon Sep 17 00:00:00 2001 From: Furkan Date: Wed, 26 Oct 2022 14:36:12 +0300 Subject: [PATCH] Layout omit image.ref.name annotation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #869 Signed-off-by: Furkan Co-authored-by: Batuhan Signed-off-by: Batuhan Apaydın --- pkg/commands/resolver.go | 2 +- pkg/publish/layout.go | 33 +++++++++++++++++++---- pkg/publish/layout_test.go | 54 ++++++++++++++++++++++++++------------ 3 files changed, 66 insertions(+), 23 deletions(-) diff --git a/pkg/commands/resolver.go b/pkg/commands/resolver.go index 2b341c7d62..d1254e37fb 100644 --- a/pkg/commands/resolver.go +++ b/pkg/commands/resolver.go @@ -207,7 +207,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) { publishers := []publish.Interface{} if po.OCILayoutPath != "" { - lp, err := publish.NewLayout(po.OCILayoutPath) + lp, err := publish.NewLayout(po.OCILayoutPath, po.Tags) if err != nil { return nil, fmt.Errorf("failed to create LayoutPublisher for %q: %w", po.OCILayoutPath, err) } diff --git a/pkg/publish/layout.go b/pkg/publish/layout.go index d325a33bf2..c1ea94685a 100644 --- a/pkg/publish/layout.go +++ b/pkg/publish/layout.go @@ -25,14 +25,16 @@ import ( "github.com/google/go-containerregistry/pkg/v1/layout" "github.com/google/go-containerregistry/pkg/v1/types" "github.com/google/ko/pkg/build" + specsv1 "github.com/opencontainers/image-spec/specs-go/v1" ) type LayoutPublisher struct { - p layout.Path + p layout.Path + tags []string } // NewLayout returns a new publish.Interface that saves images to an OCI Image Layout. -func NewLayout(path string) (Interface, error) { +func NewLayout(path string, tags ...string) (Interface, error) { p, err := layout.FromPath(path) if err != nil { p, err = layout.Write(path, empty.Index) @@ -40,7 +42,10 @@ func NewLayout(path string) (Interface, error) { return nil, err } } - return &LayoutPublisher{p}, nil + if len(tags) == 0 { + tags = []string{"latest"} + } + return &LayoutPublisher{p, tags}, nil } func (l *LayoutPublisher) writeResult(br build.Result) error { @@ -55,13 +60,31 @@ func (l *LayoutPublisher) writeResult(br build.Result) error { if !ok { return fmt.Errorf("failed to interpret result as index: %v", br) } - return l.p.AppendIndex(idx) + for _, t := range l.tags { + if err := l.p.AppendIndex(idx, + layout.WithAnnotations(map[string]string{ + specsv1.AnnotationRefName: t, + }), + ); err != nil { + return err + } + } + return nil case types.OCIManifestSchema1, types.DockerManifestSchema2: img, ok := br.(v1.Image) if !ok { return fmt.Errorf("failed to interpret result as image: %v", br) } - return l.p.AppendImage(img) + for _, t := range l.tags { + if err := l.p.AppendImage(img, + layout.WithAnnotations(map[string]string{ + specsv1.AnnotationRefName: t, + }), + ); err != nil { + return err + } + } + return nil default: return fmt.Errorf("result image media type: %s", mt) } diff --git a/pkg/publish/layout_test.go b/pkg/publish/layout_test.go index d46e21c0e0..8abf61d30b 100644 --- a/pkg/publish/layout_test.go +++ b/pkg/publish/layout_test.go @@ -25,25 +25,45 @@ import ( ) func TestLayout(t *testing.T) { - img, err := random.Image(1024, 1) - if err != nil { - t.Fatalf("random.Image() = %v", err) + cases := []struct { + name string + tags []string + }{ + { + name: "no tags", + tags: nil, + }, { + name: "foo and bar", + tags: []string{"foo", "bar"}, + }, { + name: "latest and foo", + tags: []string{"latest", "foo"}, + }, } - importpath := "github.com/Google/go-containerregistry/cmd/crane" - tmp, err := ioutil.TempDir("/tmp", "ko") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tmp) + for _, tt := range cases { + t.Run(tt.name, func(t *testing.T) { + img, err := random.Image(1024, 1) + if err != nil { + t.Fatalf("random.Image() = %v", err) + } + importpath := "github.com/example/importpath" - lp, err := NewLayout(tmp) - if err != nil { - t.Errorf("NewLayout() = %v", err) - } - 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) + tmp, err := ioutil.TempDir("/tmp", "ko") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(tmp) + + lp, err := NewLayout(tmp, tt.tags...) + if err != nil { + t.Errorf("NewLayout() = %v", err) + } + 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) + } + }) } }