From 6bb217806349238dfe45f0106926b04421c19efd Mon Sep 17 00:00:00 2001 From: Jeff Mendoza Date: Wed, 4 Sep 2024 09:56:54 -0700 Subject: [PATCH] Change OCI Layout publisher to lazy create layout after build. When using ko with OCI Layout, Git status will always be dirty because the layout is created before this build. This changes the layout to be created after the build so that git will be clean. The means that any error creating the layout won't be seen until after the build. Signed-off-by: Jeff Mendoza --- pkg/commands/resolver.go | 5 +---- pkg/publish/layout.go | 40 +++++++++++++++++++++++--------------- pkg/publish/layout_test.go | 5 +---- pkg/publish/multi_test.go | 5 +---- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/pkg/commands/resolver.go b/pkg/commands/resolver.go index c55b961c0d..157b467d02 100644 --- a/pkg/commands/resolver.go +++ b/pkg/commands/resolver.go @@ -213,10 +213,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) { publishers := []publish.Interface{} if po.OCILayoutPath != "" { - lp, err := publish.NewLayout(po.OCILayoutPath) - if err != nil { - return nil, fmt.Errorf("failed to create LayoutPublisher for %q: %w", po.OCILayoutPath, err) - } + lp := publish.NewLayout(po.OCILayoutPath) publishers = append(publishers, lp) } if po.TarballFile != "" { diff --git a/pkg/publish/layout.go b/pkg/publish/layout.go index c0d5bb3cbf..6a0cb1810e 100644 --- a/pkg/publish/layout.go +++ b/pkg/publish/layout.go @@ -28,49 +28,57 @@ import ( ) type LayoutPublisher struct { - p layout.Path + p string } // NewLayout returns a new publish.Interface that saves images to an OCI Image Layout. -func NewLayout(path string) (Interface, error) { - p, err := layout.FromPath(path) +func NewLayout(p string) Interface { + return &LayoutPublisher{p} +} + +func (l *LayoutPublisher) writeResult(br build.Result) (layout.Path, error) { + p, err := layout.FromPath(l.p) if err != nil { - p, err = layout.Write(path, empty.Index) + p, err = layout.Write(l.p, empty.Index) if err != nil { - return nil, err + return "", err } } - return &LayoutPublisher{p}, nil -} -func (l *LayoutPublisher) writeResult(br build.Result) error { mt, err := br.MediaType() if err != nil { - return err + return "", err } switch mt { case types.OCIImageIndex, types.DockerManifestList: idx, ok := br.(v1.ImageIndex) if !ok { - return fmt.Errorf("failed to interpret result as index: %v", br) + return "", fmt.Errorf("failed to interpret result as index: %v", br) } - return l.p.AppendIndex(idx) + if err := p.AppendIndex(idx); err != nil { + return "", err + } + return p, 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 "", fmt.Errorf("failed to interpret result as image: %v", br) + } + if err := p.AppendImage(img); err != nil { + return "", err } - return l.p.AppendImage(img) + return p, nil default: - return fmt.Errorf("result image media type: %s", mt) + return "", fmt.Errorf("result image media type: %s", mt) } } // Publish implements publish.Interface. 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 { + p, err := l.writeResult(br) + if err != nil { return nil, err } log.Printf("Saved %v", s) @@ -80,7 +88,7 @@ func (l *LayoutPublisher) Publish(_ context.Context, br build.Result, s string) return nil, err } - dig, err := name.NewDigest(fmt.Sprintf("%s@%s", l.p, h)) + dig, err := name.NewDigest(fmt.Sprintf("%s@%s", p, h)) if err != nil { return nil, err } diff --git a/pkg/publish/layout_test.go b/pkg/publish/layout_test.go index 2942746b04..07b2dffd74 100644 --- a/pkg/publish/layout_test.go +++ b/pkg/publish/layout_test.go @@ -36,10 +36,7 @@ func TestLayout(t *testing.T) { } defer os.RemoveAll(tmp) - lp, err := NewLayout(tmp) - if err != nil { - t.Errorf("NewLayout() = %v", err) - } + lp := NewLayout(tmp) if d, err := lp.Publish(context.Background(), img, importpath); err != nil { t.Errorf("Publish() = %v", err) } else if !strings.HasPrefix(d.String(), tmp) { diff --git a/pkg/publish/multi_test.go b/pkg/publish/multi_test.go index b21870f50f..371ec13d84 100644 --- a/pkg/publish/multi_test.go +++ b/pkg/publish/multi_test.go @@ -48,10 +48,7 @@ func TestMulti(t *testing.T) { } defer os.RemoveAll(tmp) - lp, err := publish.NewLayout(tmp) - if err != nil { - t.Errorf("NewLayout() = %v", err) - } + lp := publish.NewLayout(tmp) p := publish.MultiPublisher(lp, tp) if _, err := p.Publish(context.Background(), img, importpath); err != nil {