Skip to content

Commit

Permalink
Layout omit image.ref.name annotation
Browse files Browse the repository at this point in the history
Fixes #869

Signed-off-by: Furkan <[email protected]>
Co-authored-by: Batuhan <[email protected]>
Signed-off-by: Batuhan Apaydın <[email protected]>
  • Loading branch information
Dentrax and developer-guy committed Nov 2, 2022
1 parent f158992 commit e072b66
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
33 changes: 28 additions & 5 deletions pkg/publish/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,27 @@ 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)
if err != nil {
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 {
Expand All @@ -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)
}
Expand Down
54 changes: 37 additions & 17 deletions pkg/publish/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit e072b66

Please sign in to comment.