From 59ac803ed9d64e1bf2a5e87afa5fb7ea39223df7 Mon Sep 17 00:00:00 2001 From: Austin Abro Date: Wed, 28 Aug 2024 16:41:58 +0000 Subject: [PATCH] showoff potential ReadGeneratedZarfYaml Signed-off-by: Austin Abro --- src/pkg/layout/package.go | 28 ++++++++++++++++++++++++++++ src/pkg/packager/sources/oci.go | 2 +- src/pkg/packager/sources/tarball.go | 4 ++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/pkg/layout/package.go b/src/pkg/layout/package.go index a38ec599a1..04ac466af0 100644 --- a/src/pkg/layout/package.go +++ b/src/pkg/layout/package.go @@ -5,7 +5,9 @@ package layout import ( + "errors" "fmt" + "io/fs" "os" "path/filepath" "slices" @@ -17,6 +19,7 @@ import ( "github.com/mholt/archiver/v3" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/zarf-dev/zarf/src/api/v1alpha1" + "github.com/zarf-dev/zarf/src/api/v1beta1" "github.com/zarf-dev/zarf/src/pkg/interactive" "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/deprecated" @@ -50,6 +53,31 @@ func New(baseDir string) *PackagePaths { } } +// ReadGeneratedZarfYaml reads a zarf package from the a generated zarfv1beta1.yaml file or from a zarf.yaml file and translates it +// This should only be used on generated Zarf packages +func (pp *PackagePaths) ReadGeneratedZarfYaml() (v1beta1.ZarfPackage, error) { + path := filepath.Join(pp.Base, "zarfv1beta1.yaml") + _, err := os.Stat(path) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return v1beta1.ZarfPackage{}, err + } + if errors.Is(err, fs.ErrNotExist) { + alphaPkg, _, err := pp.ReadZarfYAML() + if err != nil { + return v1beta1.ZarfPackage{}, err + } + betaPkg, err := v1beta1.TranslateAlphaPackage(alphaPkg) + if err != nil { + return v1beta1.ZarfPackage{}, err + } + return betaPkg, nil + } + + var pkg v1beta1.ZarfPackage + err = utils.ReadYaml(path, &pkg) + return pkg, err +} + // ReadZarfYAML reads a zarf.yaml file into memory, // checks if it's using the legacy layout, and migrates deprecated component configs. func (pp *PackagePaths) ReadZarfYAML() (pkg v1alpha1.ZarfPackage, warnings []string, err error) { diff --git a/src/pkg/packager/sources/oci.go b/src/pkg/packager/sources/oci.go index 8bf6d6d1a6..7937837360 100644 --- a/src/pkg/packager/sources/oci.go +++ b/src/pkg/packager/sources/oci.go @@ -120,7 +120,7 @@ func (s *OCISource) LoadPackageMetadata(ctx context.Context, dst *layout.Package } dst.SetFromLayers(layersFetched) - pkg, warnings, err = dst.ReadZarfYAML() + pkg, warnings, err = dst.ReadGeneratedZarfYaml() if err != nil { return pkg, nil, err } diff --git a/src/pkg/packager/sources/tarball.go b/src/pkg/packager/sources/tarball.go index db1b2ed01a..f3bd06497d 100644 --- a/src/pkg/packager/sources/tarball.go +++ b/src/pkg/packager/sources/tarball.go @@ -84,7 +84,7 @@ func (s *TarballSource) LoadPackage(ctx context.Context, dst *layout.PackagePath dst.SetFromPaths(pathsExtracted) - pkg, warnings, err = dst.ReadZarfYAML() + pkg, warnings, err = dst.ReadGeneratedZarfYaml() if err != nil { return pkg, nil, err } @@ -164,7 +164,7 @@ func (s *TarballSource) LoadPackageMetadata(ctx context.Context, dst *layout.Pac dst.SetFromPaths(pathsExtracted) - pkg, warnings, err = dst.ReadZarfYAML() + pkg, warnings, err = dst.ReadGeneratedZarfYaml() if err != nil { return pkg, nil, err }