Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove extraneous code from CraneEngine #1216

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 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 @@ -152,28 +145,27 @@ func (c *craneEngine) ExecuteChecks(ctx context.Context) error {
return fmt.Errorf("failed to create container expansion directory: %s: %v", containerFSPath, err)
}

// 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)
// Wrap this critical section in a closure to that we can close the
// mutate.Extract reader sooner than the end of the checks
if err := func() error {
// export/flatten, and extract
logger.V(log.DBG).Info("exporting and flattening image")
fs := mutate.Extract(img)
defer fs.Close()

// 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))
}()

logger.V(log.DBG).Info("extracting container filesystem", "path", containerFSPath)
if err := untar(ctx, containerFSPath, r); err != nil {
return fmt.Errorf("failed to extract tarball: %v", err)
}
logger.V(log.DBG).Info("extracting container filesystem", "path", containerFSPath)
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)
if err != nil {
return fmt.Errorf("failed to drain io reader: %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, fs)
if err != nil {
return fmt.Errorf("failed to drain io reader: %v", err)
}
return nil
}(); err != nil {
return err
}

reference, err := name.ParseReference(c.image)
Expand Down
Loading