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

refactor: add error to packager set paths #2753

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
13 changes: 9 additions & 4 deletions src/pkg/layout/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,22 @@ func (pp *PackagePaths) AddSBOMs() *PackagePaths {
}

// SetFromLayers maps layers to package paths.
func (pp *PackagePaths) SetFromLayers(layers []ocispec.Descriptor) {
func (pp *PackagePaths) SetFromLayers(layers []ocispec.Descriptor) error {
paths := []string{}
for _, layer := range layers {
if layer.Annotations[ocispec.AnnotationTitle] != "" {
paths = append(paths, layer.Annotations[ocispec.AnnotationTitle])
}
}
pp.SetFromPaths(paths)
err := pp.SetFromPaths(paths)
if err != nil {
return err
}
return nil
}

// SetFromPaths maps paths to package paths.
func (pp *PackagePaths) SetFromPaths(paths []string) {
func (pp *PackagePaths) SetFromPaths(paths []string) error {
for _, rel := range paths {
// Convert from the standard '/' to the OS path separator for Windows support
switch path := filepath.FromSlash(rel); {
Expand Down Expand Up @@ -310,9 +314,10 @@ func (pp *PackagePaths) SetFromPaths(paths []string) {
}
pp.Components.Tarballs[componentName] = filepath.Join(pp.Base, path)
default:
message.Debug("ignoring path", path)
return fmt.Errorf("unknown path %s", path)
}
}
return nil
}

// Files returns a map of all the files in the package.
Expand Down
17 changes: 15 additions & 2 deletions src/pkg/layout/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ func TestPackageFiles(t *testing.T) {
normalizePath("images/oci-layout"),
normalizePath("images/blobs/sha256/" + strings.Repeat("1", 64)),
}
pp.SetFromPaths(paths)
err := pp.SetFromPaths(paths)
require.NoError(t, err)

files := pp.Files()
expected := map[string]string{
Expand Down Expand Up @@ -150,7 +151,8 @@ func TestPackageFiles(t *testing.T) {
},
}
pp.AddImages()
pp.SetFromLayers(descs)
err := pp.SetFromLayers(descs)
require.NoError(t, err)

files := pp.Files()
expected := map[string]string{
Expand All @@ -165,6 +167,17 @@ func TestPackageFiles(t *testing.T) {
})
}

func TestInvalidSetPaths(t *testing.T) {
t.Parallel()

pp := New("test")
paths := []string{
"foo",
}
err := pp.SetFromPaths(paths)
require.EqualError(t, err, "unknown path foo")
}

// normalizePath ensures that the filepaths being generated are normalized to the host OS.
func normalizePath(path string) string {
if runtime.GOOS != "windows" {
Expand Down
15 changes: 12 additions & 3 deletions src/pkg/packager/sources/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func (s *OCISource) LoadPackage(ctx context.Context, dst *layout.PackagePaths, f
if err != nil {
return pkg, nil, fmt.Errorf("unable to pull the package: %w", err)
}
dst.SetFromLayers(layersFetched)
err = dst.SetFromLayers(layersFetched)
if err != nil {
return types.ZarfPackage{}, nil, err
}

if err := dst.MigrateLegacy(); err != nil {
return pkg, nil, err
Expand Down Expand Up @@ -119,7 +122,10 @@ func (s *OCISource) LoadPackageMetadata(ctx context.Context, dst *layout.Package
if err != nil {
return pkg, nil, err
}
dst.SetFromLayers(layersFetched)
err = dst.SetFromLayers(layersFetched)
if err != nil {
return types.ZarfPackage{}, nil, err
}

pkg, warnings, err = dst.ReadZarfYAML()
if err != nil {
Expand Down Expand Up @@ -174,7 +180,10 @@ func (s *OCISource) Collect(ctx context.Context, dir string) (string, error) {
}

loaded := layout.New(tmp)
loaded.SetFromLayers(fetched)
err = loaded.SetFromLayers(fetched)
if err != nil {
return "", err
}

var pkg types.ZarfPackage

Expand Down
10 changes: 8 additions & 2 deletions src/pkg/packager/sources/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func (s *TarballSource) LoadPackage(_ context.Context, dst *layout.PackagePaths,
return pkg, nil, err
}

dst.SetFromPaths(pathsExtracted)
err = dst.SetFromPaths(pathsExtracted)
if err != nil {
return types.ZarfPackage{}, nil, err
}

pkg, warnings, err = dst.ReadZarfYAML()
if err != nil {
Expand Down Expand Up @@ -161,7 +164,10 @@ func (s *TarballSource) LoadPackageMetadata(_ context.Context, dst *layout.Packa
}
}

dst.SetFromPaths(pathsExtracted)
err = dst.SetFromPaths(pathsExtracted)
if err != nil {
return types.ZarfPackage{}, nil, err
}

pkg, warnings, err = dst.ReadZarfYAML()
if err != nil {
Expand Down
Loading