Skip to content

Commit

Permalink
Remove extraneous code from CraneEngine
Browse files Browse the repository at this point in the history
Turns out that we were making an extra call to io.Copy that is
completely unnecessary. We can use the io.Reader directly from crane's
mutate.Extract call.

Signed-off-by: Brad P. Crochet <brad@redhat.com>
  • Loading branch information
bcrochet committed Nov 22, 2024
1 parent ed06ae7 commit 160e232
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (

"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
cranev1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/cache"
"github.com/google/go-containerregistry/pkg/v1/mutate"
)
Expand Down Expand Up @@ -96,12 +95,6 @@ type craneEngine struct {
results certification.Results
}

func export(img cranev1.Image, w io.Writer) error {
fs := mutate.Extract(img)
_, err := io.Copy(w, fs)
return err
}

func (c *craneEngine) CranePlatform() string {
return c.platform
}
Expand Down Expand Up @@ -154,24 +147,16 @@ func (c *craneEngine) ExecuteChecks(ctx context.Context) error {

// export/flatten, and extract
logger.V(log.DBG).Info("exporting and flattening image")
r, w := io.Pipe()
go func() {
logger.V(log.DBG).Info("writing container filesystem", "outputDirectory", containerFSPath)

// Close the writer with any errors encountered during
// extraction. These errors will be returned by the reader end
// on subsequent reads. If err == nil, the reader will return
// EOF.
w.CloseWithError(export(img, w))
}()
fs := mutate.Extract(img)
defer fs.Close()

logger.V(log.DBG).Info("extracting container filesystem", "path", containerFSPath)
if err := untar(ctx, containerFSPath, r); err != nil {
if err := untar(ctx, containerFSPath, fs); err != nil {
return fmt.Errorf("failed to extract tarball: %v", err)
}

// explicitly discarding from the reader for cases where there is data in the reader after it sends an EOF
_, err = io.Copy(io.Discard, r)
_, err = io.Copy(io.Discard, fs)
if err != nil {
return fmt.Errorf("failed to drain io reader: %v", err)
}
Expand Down

0 comments on commit 160e232

Please sign in to comment.