From b7521deef1cd85581f78d3e04d32d956efe67aed Mon Sep 17 00:00:00 2001 From: Jeff Mendoza Date: Thu, 22 Aug 2024 14:06:04 -0700 Subject: [PATCH 1/2] Update recorder to lazy create file. Currently the resolver command creates the image-refs file on initilization. This causes git to be dirty during builds. This change moves the file creation to be in the recorder itself and on the first time the Publish() method is called. This happens after the build so git is clean. This will mean that any errors on file creation will be reported after the build rather than before. Signed-off-by: Jeff Mendoza --- pkg/commands/resolver.go | 6 +----- pkg/publish/recorder.go | 20 +++++++++++++++----- pkg/publish/recorder_test.go | 14 ++++++++++---- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/pkg/commands/resolver.go b/pkg/commands/resolver.go index c55b961c0d..2e1ce881cc 100644 --- a/pkg/commands/resolver.go +++ b/pkg/commands/resolver.go @@ -266,11 +266,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) { } if po.ImageRefsFile != "" { - f, err := os.OpenFile(po.ImageRefsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - return nil, err - } - innerPublisher, err = publish.NewRecorder(innerPublisher, f) + innerPublisher, err = publish.NewRecorder(innerPublisher, po.ImageRefsFile) if err != nil { return nil, err } diff --git a/pkg/publish/recorder.go b/pkg/publish/recorder.go index c886b5c909..d12068d4a0 100644 --- a/pkg/publish/recorder.go +++ b/pkg/publish/recorder.go @@ -17,6 +17,7 @@ package publish import ( "context" "io" + "os" "strings" "github.com/google/go-containerregistry/pkg/name" @@ -29,8 +30,9 @@ import ( // recorder wraps a publisher implementation in a layer that recordes the published // references to an io.Writer. type recorder struct { - inner Interface - wc io.Writer + inner Interface + fileName string + wc io.Writer } // recorder implements Interface @@ -38,10 +40,10 @@ var _ Interface = (*recorder)(nil) // NewRecorder wraps the provided publish.Interface in an implementation that // records publish results to an io.Writer. -func NewRecorder(inner Interface, wc io.Writer) (Interface, error) { +func NewRecorder(inner Interface, name string) (Interface, error) { return &recorder{ - inner: inner, - wc: wc, + inner: inner, + fileName: name, }, nil } @@ -70,6 +72,14 @@ func (r *recorder) Publish(ctx context.Context, br build.Result, ref string) (na references = append(references, result.String()) } + if r.wc == nil { + f, err := os.OpenFile(r.fileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return nil, err + } + r.wc = f + } + if _, err := r.wc.Write([]byte(strings.Join(references, "\n") + "\n")); err != nil { return nil, err } diff --git a/pkg/publish/recorder_test.go b/pkg/publish/recorder_test.go index 06079c3aeb..3f737b4586 100644 --- a/pkg/publish/recorder_test.go +++ b/pkg/publish/recorder_test.go @@ -15,8 +15,9 @@ package publish import ( - "bytes" "context" + "os" + "path" "strings" "testing" @@ -50,9 +51,10 @@ func TestRecorder(t *testing.T) { return repo.Context().Digest(h.String()), nil }} - buf := bytes.NewBuffer(nil) + dir := t.TempDir() + file := path.Join(dir, "testfile") - recorder, err := NewRecorder(inner, buf) + recorder, err := NewRecorder(inner, file) if err != nil { t.Fatalf("NewRecorder() = %v", err) } @@ -82,7 +84,11 @@ func TestRecorder(t *testing.T) { t.Errorf("recorder.Close() = %v", err) } - refs := strings.Split(strings.TrimSpace(buf.String()), "\n") + buf, err := os.ReadFile(file) + if err != nil { + t.Fatalf("os.ReadFile() = %v", err) + } + refs := strings.Split(strings.TrimSpace(string(buf)), "\n") if want, got := len(refs), 5; got != want { t.Errorf("len(refs) = %d, wanted %d", got, want) From 31bf4b1615ed310aee83caa07adee31f53be7b60 Mon Sep 17 00:00:00 2001 From: Jeff Mendoza Date: Thu, 22 Aug 2024 15:08:03 -0700 Subject: [PATCH 2/2] Update doc comment Signed-off-by: Jeff Mendoza --- pkg/publish/recorder.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/publish/recorder.go b/pkg/publish/recorder.go index d12068d4a0..e2915d08c9 100644 --- a/pkg/publish/recorder.go +++ b/pkg/publish/recorder.go @@ -28,7 +28,7 @@ import ( ) // recorder wraps a publisher implementation in a layer that recordes the published -// references to an io.Writer. +// references to a file. type recorder struct { inner Interface fileName string @@ -39,7 +39,7 @@ type recorder struct { var _ Interface = (*recorder)(nil) // NewRecorder wraps the provided publish.Interface in an implementation that -// records publish results to an io.Writer. +// records publish results to a file. func NewRecorder(inner Interface, name string) (Interface, error) { return &recorder{ inner: inner,