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: packager inspect command #2990

Merged
merged 25 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
41 changes: 32 additions & 9 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/zarf-dev/zarf/src/pkg/packager"
"github.com/zarf-dev/zarf/src/pkg/packager/filters"
"github.com/zarf-dev/zarf/src/pkg/packager/sources"
"github.com/zarf-dev/zarf/src/pkg/utils"
"github.com/zarf-dev/zarf/src/types"
)

Expand Down Expand Up @@ -188,26 +189,48 @@ var packageInspectCmd = &cobra.Command{
}
},
RunE: func(cmd *cobra.Command, args []string) error {
packageSource, err := choosePackage(args)
src, err := choosePackage(args)
if err != nil {
return err
}
pkgConfig.PkgOpts.PackageSource = packageSource
src, err := identifyAndFallbackToClusterSource()
if err != nil {
return err

loadOpt := packager2.LoadOptions{
Source: src,
Shasum: pkgConfig.PkgOpts.Shasum,
PublicKeyPath: pkgConfig.PkgOpts.PublicKeyPath,
SkipSignatureValidation: pkgConfig.PkgOpts.SkipSignatureValidation,
}
pkgClient, err := packager.New(&pkgConfig, packager.WithSource(src))

pkgPaths, err := packager2.LoadPackage(cmd.Context(), loadOpt)
if err != nil {
return err
}
defer pkgClient.ClearTempPaths()
if err := pkgClient.Inspect(cmd.Context()); err != nil {
defer os.RemoveAll(pkgPaths.Base)

inspectOpt := packager2.ZarfInspectOptions{
PackagePaths: pkgPaths,
ListImages: pkgConfig.InspectOpts.ListImages,
ViewSBOM: pkgConfig.InspectOpts.ViewSBOM,
SBOMOutputDir: pkgConfig.InspectOpts.SBOMOutputDir,
}

if pkgConfig.InspectOpts.ListImages {
output, err := packager2.InspectList(cmd.Context(), inspectOpt)
if err != nil {
return fmt.Errorf("failed to inspect package: %w", err)
}
for _, image := range output {
fmt.Fprintln(os.Stdout, "-", image)
}
}

output, err := packager2.Inspect(cmd.Context(), inspectOpt)
if err != nil {
return fmt.Errorf("failed to inspect package: %w", err)
}
utils.ColorPrintYAML(output, nil, false)
return nil
},
ValidArgsFunction: getPackageCompletionArgs,
}

var packageListCmd = &cobra.Command{
Expand Down
104 changes: 104 additions & 0 deletions src/internal/packager2/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package packager2 contains functions for inspecting packages.
package packager2

import (
"context"
"fmt"

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/zarf-dev/zarf/src/api/v1alpha1"
"github.com/zarf-dev/zarf/src/internal/packager/sbom"
"github.com/zarf-dev/zarf/src/pkg/layout"
)

// ZarfInspectOptions tracks the user-defined preferences during a package inspection.
type ZarfInspectOptions struct {
// PackagePaths
schristoff marked this conversation as resolved.
Show resolved Hide resolved
PackagePaths *layout.PackagePaths
// View SBOM contents while inspecting the package
ViewSBOM bool
// Location to output an SBOM into after package inspection
SBOMOutputDir string
// ListImages will list the images in the package
ListImages bool
}

// Inspect list the contents of a package.
func Inspect(ctx context.Context, options ZarfInspectOptions) (v1alpha1.ZarfPackage, error) {
var err error
pkg, err := getPackageMetadata(ctx, options.PackagePaths)
if err != nil {
return pkg, err
}

if getSBOM(options.ViewSBOM, options.SBOMOutputDir) {
err = handleSBOMOptions(options.PackagePaths, pkg, options.ViewSBOM, options.SBOMOutputDir)
if err != nil {
return pkg, err
}
return pkg, nil
}
return pkg, nil
}

// InspectList lists the images in a component action
func InspectList(ctx context.Context, options ZarfInspectOptions) ([]string, error) {
var imageList []string
pkg, err := getPackageMetadata(ctx, options.PackagePaths)
if err != nil {
return nil, err
}
// Only list images if we have have components
if len(pkg.Components) > 0 {
for _, component := range pkg.Components {
imageList = append(imageList, component.Images...)
}
if len(imageList) > 0 {
imageList = helpers.Unique(imageList)
return imageList, nil
}
return nil, fmt.Errorf("failed listing images: list of images found in components: %d", len(imageList))
}

return imageList, err
}

func getPackageMetadata(_ context.Context, layout *layout.PackagePaths) (v1alpha1.ZarfPackage, error) {
schristoff marked this conversation as resolved.
Show resolved Hide resolved
pkg, _, err := layout.ReadZarfYAML()
if err != nil {
return pkg, err
}
return pkg, nil
}

func handleSBOMOptions(layout *layout.PackagePaths, pkg v1alpha1.ZarfPackage, viewSBOM bool, SBOMOutputDir string) error {
if SBOMOutputDir != "" {
out, err := layout.SBOMs.OutputSBOMFiles(SBOMOutputDir, pkg.Metadata.Name)
if err != nil {
return err
}
if viewSBOM {
err := sbom.ViewSBOMFiles(out)
if err != nil {
return err
}
}
} else if viewSBOM {
err := sbom.ViewSBOMFiles(layout.SBOMs.Path)
if err != nil {
return err
}
return err
}
return nil
}

func getSBOM(viewSBOM bool, SBOMOutputDir string) bool {
if viewSBOM || SBOMOutputDir != "" {
return true
}
return false
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 5 additions & 5 deletions src/pkg/packager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Packager struct {
variableConfig *variables.VariableConfig
state *types.ZarfState
cluster *cluster.Cluster
layout *layout.PackagePaths
schristoff marked this conversation as resolved.
Show resolved Hide resolved
Layout *layout.PackagePaths
hpaModified bool
source sources.PackageSource
}
Expand All @@ -61,7 +61,7 @@ func WithCluster(cluster *cluster.Cluster) Modifier {
// This temp directory is used as the destination where p.source loads the package.
func WithTemp(base string) Modifier {
return func(p *Packager) {
p.layout = layout.New(base)
p.Layout = layout.New(base)
}
}

Expand Down Expand Up @@ -104,13 +104,13 @@ func New(cfg *types.PackagerConfig, mods ...Modifier) (*Packager, error) {
}

// If the temp directory is not set, set it to the default
if pkgr.layout == nil {
if pkgr.Layout == nil {
dir, err := utils.MakeTempDir(config.CommonOptions.TempDirectory)
if err != nil {
return nil, fmt.Errorf("unable to create package temp paths: %w", err)
}
message.Debug("Using temporary directory:", dir)
pkgr.layout = layout.New(dir)
pkgr.Layout = layout.New(dir)
}

return pkgr, nil
Expand All @@ -119,7 +119,7 @@ func New(cfg *types.PackagerConfig, mods ...Modifier) (*Packager, error) {
// ClearTempPaths removes the temp directory and any files within it.
func (p *Packager) ClearTempPaths() {
// Remove the temp directory, but don't throw an error if it fails
_ = os.RemoveAll(p.layout.Base)
_ = os.RemoveAll(p.Layout.Base)
_ = os.RemoveAll(layout.SBOMDir)
}

Expand Down
8 changes: 4 additions & 4 deletions src/pkg/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func (p *Packager) Create(ctx context.Context) error {

pc := creator.NewPackageCreator(p.cfg.CreateOpts, cwd)

if err := helpers.CreatePathAndCopy(layout.ZarfYAML, p.layout.ZarfYAML); err != nil {
if err := helpers.CreatePathAndCopy(layout.ZarfYAML, p.Layout.ZarfYAML); err != nil {
return err
}

pkg, warnings, err := pc.LoadPackageDefinition(ctx, p.layout)
pkg, warnings, err := pc.LoadPackageDefinition(ctx, p.Layout)
if err != nil {
return err
}
Expand All @@ -45,7 +45,7 @@ func (p *Packager) Create(ctx context.Context) error {
return fmt.Errorf("package creation canceled")
}

if err := pc.Assemble(ctx, p.layout, p.cfg.Pkg.Components, p.cfg.Pkg.Metadata.Architecture); err != nil {
if err := pc.Assemble(ctx, p.Layout, p.cfg.Pkg.Components, p.cfg.Pkg.Metadata.Architecture); err != nil {
return err
}

Expand All @@ -54,5 +54,5 @@ func (p *Packager) Create(ctx context.Context) error {
return err
}

return pc.Output(ctx, p.layout, &p.cfg.Pkg)
return pc.Output(ctx, p.Layout, &p.cfg.Pkg)
}
14 changes: 7 additions & 7 deletions src/pkg/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ func (p *Packager) Deploy(ctx context.Context) error {
warnings := []string{}
if isInteractive {
filter := filters.Empty()
pkg, loadWarnings, err := p.source.LoadPackage(ctx, p.layout, filter, true)
pkg, loadWarnings, err := p.source.LoadPackage(ctx, p.Layout, filter, true)
if err != nil {
return fmt.Errorf("unable to load the package: %w", err)
}
p.cfg.Pkg = pkg
warnings = append(warnings, loadWarnings...)
} else {
pkg, loadWarnings, err := p.source.LoadPackage(ctx, p.layout, deployFilter, true)
pkg, loadWarnings, err := p.source.LoadPackage(ctx, p.Layout, deployFilter, true)
if err != nil {
return fmt.Errorf("unable to load the package: %w", err)
}
Expand All @@ -93,7 +93,7 @@ func (p *Packager) Deploy(ctx context.Context) error {
}
warnings = append(warnings, validateWarnings...)

sbomViewFiles, sbomWarnings, err := p.layout.SBOMs.StageSBOMViewFiles()
sbomViewFiles, sbomWarnings, err := p.Layout.SBOMs.StageSBOMViewFiles()
if err != nil {
return err
}
Expand Down Expand Up @@ -294,7 +294,7 @@ func (p *Packager) deployInitComponent(ctx context.Context, component v1alpha1.Z

// Before deploying the seed registry, start the injector
if isSeedRegistry {
err := p.cluster.StartInjection(ctx, p.layout.Base, p.layout.Images.Base, component.Images)
err := p.cluster.StartInjection(ctx, p.Layout.Base, p.Layout.Images.Base, component.Images)
if err != nil {
return nil, err
}
Expand All @@ -320,7 +320,7 @@ func (p *Packager) deployInitComponent(ctx context.Context, component v1alpha1.Z
// Deploy a Zarf Component.
func (p *Packager) deployComponent(ctx context.Context, component v1alpha1.ZarfComponent, noImgChecksum bool, noImgPush bool) ([]types.InstalledChart, error) {
// Toggles for general deploy operations
componentPath := p.layout.Components.Dirs[component.Name]
componentPath := p.Layout.Components.Dirs[component.Name]

// All components now require a name
message.HeaderInfof("📦 %s COMPONENT", strings.ToUpper(component.Name))
Expand Down Expand Up @@ -438,7 +438,7 @@ func (p *Packager) processComponentFiles(component v1alpha1.ZarfComponent, pkgLo
}

// Replace temp target directory and home directory
file.Target = strings.Replace(file.Target, "###ZARF_TEMP###", p.layout.Base, 1)
file.Target = strings.Replace(file.Target, "###ZARF_TEMP###", p.Layout.Base, 1)
file.Target = config.GetAbsHomePath(file.Target)

fileList := []string{}
Expand Down Expand Up @@ -577,7 +577,7 @@ func (p *Packager) pushImagesToRegistry(ctx context.Context, componentImages []s
imageList := helpers.Unique(combinedImageList)

pushCfg := images.PushConfig{
SourceDirectory: p.layout.Images.Base,
SourceDirectory: p.Layout.Images.Base,
ImageList: imageList,
RegInfo: p.state.RegistryInfo,
NoChecksum: noImgChecksum,
Expand Down
6 changes: 3 additions & 3 deletions src/pkg/packager/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func (p *Packager) DevDeploy(ctx context.Context) error {

pc := creator.NewPackageCreator(p.cfg.CreateOpts, cwd)

if err := helpers.CreatePathAndCopy(layout.ZarfYAML, p.layout.ZarfYAML); err != nil {
if err := helpers.CreatePathAndCopy(layout.ZarfYAML, p.Layout.ZarfYAML); err != nil {
return err
}

p.cfg.Pkg, _, err = pc.LoadPackageDefinition(ctx, p.layout)
p.cfg.Pkg, _, err = pc.LoadPackageDefinition(ctx, p.Layout)
if err != nil {
return err
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func (p *Packager) DevDeploy(ctx context.Context) error {
}
}

if err := pc.Assemble(ctx, p.layout, p.cfg.Pkg.Components, p.cfg.Pkg.Metadata.Architecture); err != nil {
if err := pc.Assemble(ctx, p.Layout, p.cfg.Pkg.Components, p.cfg.Pkg.Metadata.Architecture); err != nil {
return err
}

Expand Down
8 changes: 4 additions & 4 deletions src/pkg/packager/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ import (
func (p *Packager) Inspect(ctx context.Context) error {
wantSBOM := p.cfg.InspectOpts.ViewSBOM || p.cfg.InspectOpts.SBOMOutputDir != ""

pkg, _, err := p.source.LoadPackageMetadata(ctx, p.layout, wantSBOM, true)
pkg, _, err := p.source.LoadPackageMetadata(ctx, p.Layout, wantSBOM, true)
if err != nil {
return err
}
p.cfg.Pkg = pkg

if p.cfg.InspectOpts.ListImages {
if p.cfg.InspectOpts.ListImages && len(p.cfg.Pkg.Components) > 0 {
imageList := []string{}
for _, component := range p.cfg.Pkg.Components {
imageList = append(imageList, component.Images...)
Expand All @@ -37,10 +37,10 @@ func (p *Packager) Inspect(ctx context.Context) error {
utils.ColorPrintYAML(p.cfg.Pkg, nil, false)
}

sbomDir := p.layout.SBOMs.Path
sbomDir := p.Layout.SBOMs.Path

if p.cfg.InspectOpts.SBOMOutputDir != "" {
out, err := p.layout.SBOMs.OutputSBOMFiles(p.cfg.InspectOpts.SBOMOutputDir, p.cfg.Pkg.Metadata.Name)
out, err := p.Layout.SBOMs.OutputSBOMFiles(p.cfg.InspectOpts.SBOMOutputDir, p.cfg.Pkg.Metadata.Name)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions src/pkg/packager/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func (p *Packager) Mirror(ctx context.Context) error {
filters.BySelectState(p.cfg.PkgOpts.OptionalComponents),
)

pkg, warnings, err := p.source.LoadPackage(ctx, p.layout, filter, true)
pkg, warnings, err := p.source.LoadPackage(ctx, p.Layout, filter, true)
if err != nil {
return fmt.Errorf("unable to load the package: %w", err)
}
p.cfg.Pkg = pkg

sbomViewFiles, sbomWarnings, err := p.layout.SBOMs.StageSBOMViewFiles()
sbomViewFiles, sbomWarnings, err := p.Layout.SBOMs.StageSBOMViewFiles()
if err != nil {
return err
}
Expand All @@ -56,7 +56,7 @@ func (p *Packager) Mirror(ctx context.Context) error {

// mirrorComponent mirrors a Zarf Component.
func (p *Packager) mirrorComponent(ctx context.Context, component v1alpha1.ZarfComponent) error {
componentPaths := p.layout.Components.Dirs[component.Name]
componentPaths := p.Layout.Components.Dirs[component.Name]

// All components now require a name
message.HeaderInfof("📦 %s COMPONENT", strings.ToUpper(component.Name))
Expand Down
Loading
Loading