Skip to content

Commit

Permalink
Merge branch 'main' into schristoff_addSupport
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Rodriguez authored May 7, 2024
2 parents b806783 + bf69112 commit ec51a9c
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 76 deletions.
1 change: 1 addition & 0 deletions site/src/content/docs/commands/zarf_package_inspect.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ zarf package inspect [ PACKAGE_SOURCE ] [flags]

```
-h, --help help for inspect
--list-images List images in the package (prints to stdout)
-s, --sbom View SBOM contents while inspecting the package
--sbom-out string Specify an output directory for the SBOMs from the inspected Zarf package
```
Expand Down
1 change: 1 addition & 0 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ func bindInspectFlags(_ *viper.Viper) {
inspectFlags := packageInspectCmd.Flags()
inspectFlags.BoolVarP(&pkgConfig.InspectOpts.ViewSBOM, "sbom", "s", false, lang.CmdPackageInspectFlagSbom)
inspectFlags.StringVar(&pkgConfig.InspectOpts.SBOMOutputDir, "sbom-out", "", lang.CmdPackageInspectFlagSbomOut)
inspectFlags.BoolVar(&pkgConfig.InspectOpts.ListImages, "list-images", false, lang.CmdPackageInspectFlagListImages)
}

func bindRemoveFlags(v *viper.Viper) {
Expand Down
7 changes: 4 additions & 3 deletions src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,10 @@ $ zarf package mirror-resources <your-package.tar.zst> \
CmdPackageMirrorFlagComponents = "Comma-separated list of components to mirror. This list will be respected regardless of a component's 'required' or 'default' status. Globbing component names with '*' and deselecting components with a leading '-' are also supported."
CmdPackageMirrorFlagNoChecksum = "Turns off the addition of a checksum to image tags (as would be used by the Zarf Agent) while mirroring images."

CmdPackageInspectFlagSbom = "View SBOM contents while inspecting the package"
CmdPackageInspectFlagSbomOut = "Specify an output directory for the SBOMs from the inspected Zarf package"
CmdPackageInspectErr = "Failed to inspect package: %s"
CmdPackageInspectFlagSbom = "View SBOM contents while inspecting the package"
CmdPackageInspectFlagSbomOut = "Specify an output directory for the SBOMs from the inspected Zarf package"
CmdPackageInspectFlagListImages = "List images in the package (prints to stdout)"
CmdPackageInspectErr = "Failed to inspect package: %s"

CmdPackageRemoveShort = "Removes a Zarf package that has been deployed already (runs offline)"
CmdPackageRemoveFlagConfirm = "REQUIRED. Confirm the removal action to prevent accidental deletions"
Expand Down
48 changes: 0 additions & 48 deletions src/pkg/packager/creator/differential.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
package creator

import (
"fmt"
"os"

"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/internal/packager/git"
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/packager/sources"
"github.com/defenseunicorns/zarf/src/pkg/transform"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/types"
"github.com/go-git/go-git/v5/plumbing"
)

// loadDifferentialData sets any images and repos from the existing reference package in the DifferentialData and returns it.
Expand Down Expand Up @@ -58,47 +54,3 @@ func loadDifferentialData(diffPkgPath string) (diffData *types.DifferentialData,
DifferentialPackageVersion: diffPkg.Metadata.Version,
}, nil
}

// removeCopiesFromComponents removes any images and repos already present in the reference package components.
func removeCopiesFromComponents(components []types.ZarfComponent, loadedDiffData *types.DifferentialData) (diffComponents []types.ZarfComponent, err error) {
for _, component := range components {
newImageList := []string{}
newRepoList := []string{}

for _, img := range component.Images {
imgRef, err := transform.ParseImageRef(img)
if err != nil {
return nil, fmt.Errorf("unable to parse image ref %s: %s", img, err.Error())
}

imgTag := imgRef.TagOrDigest
includeImage := imgTag == ":latest" || imgTag == ":stable" || imgTag == ":nightly"
if includeImage || !loadedDiffData.DifferentialImages[img] {
newImageList = append(newImageList, img)
}
}

for _, repoURL := range component.Repos {
_, refPlain, err := transform.GitURLSplitRef(repoURL)
if err != nil {
return nil, err
}

var ref plumbing.ReferenceName
if refPlain != "" {
ref = git.ParseRef(refPlain)
}

includeRepo := ref == "" || (!ref.IsTag() && !plumbing.IsHash(refPlain))
if includeRepo || !loadedDiffData.DifferentialRepos[repoURL] {
newRepoList = append(newRepoList, repoURL)
}
}

component.Images = newImageList
component.Repos = newRepoList
diffComponents = append(diffComponents, component)
}

return diffComponents, nil
}
4 changes: 3 additions & 1 deletion src/pkg/packager/creator/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/layout"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/packager/actions"
"github.com/defenseunicorns/zarf/src/pkg/packager/filters"
"github.com/defenseunicorns/zarf/src/pkg/packager/sources"
"github.com/defenseunicorns/zarf/src/pkg/transform"
"github.com/defenseunicorns/zarf/src/pkg/utils"
Expand Down Expand Up @@ -110,7 +111,8 @@ func (pc *PackageCreator) LoadPackageDefinition(dst *layout.PackagePaths) (pkg t
return types.ZarfPackage{}, nil, errors.New(lang.PkgCreateErrDifferentialNoVersion)
}

pkg.Components, err = removeCopiesFromComponents(pkg.Components, diffData)
filter := filters.ByDifferentialData(diffData)
pkg.Components, err = filter.Apply(pkg)
if err != nil {
return types.ZarfPackage{}, nil, err
}
Expand Down
63 changes: 63 additions & 0 deletions src/pkg/packager/filters/diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

package filters

import (
"fmt"

"github.com/defenseunicorns/zarf/src/internal/packager/git"
"github.com/defenseunicorns/zarf/src/pkg/transform"
"github.com/defenseunicorns/zarf/src/types"
"github.com/go-git/go-git/v5/plumbing"
)

// ByDifferentialData filters any images and repos already present in the reference package components.
func ByDifferentialData(diffData *types.DifferentialData) ComponentFilterStrategy {
return &differentialDataFilter{
diffData: diffData,
}
}

type differentialDataFilter struct {
diffData *types.DifferentialData
}

func (f *differentialDataFilter) Apply(pkg types.ZarfPackage) ([]types.ZarfComponent, error) {
diffComponents := []types.ZarfComponent{}
for _, component := range pkg.Components {
filteredImages := []string{}
for _, img := range component.Images {
imgRef, err := transform.ParseImageRef(img)
if err != nil {
return nil, fmt.Errorf("unable to parse image ref %s: %w", img, err)
}
imgTag := imgRef.TagOrDigest
includeImage := imgTag == ":latest" || imgTag == ":stable" || imgTag == ":nightly"
if includeImage || !f.diffData.DifferentialImages[img] {
filteredImages = append(filteredImages, img)
}
}
component.Images = filteredImages

filteredRepos := []string{}
for _, repoURL := range component.Repos {
_, refPlain, err := transform.GitURLSplitRef(repoURL)
if err != nil {
return nil, err
}
var ref plumbing.ReferenceName
if refPlain != "" {
ref = git.ParseRef(refPlain)
}
includeRepo := ref == "" || (!ref.IsTag() && !plumbing.IsHash(refPlain))
if includeRepo || !f.diffData.DifferentialRepos[repoURL] {
filteredRepos = append(filteredRepos, repoURL)
}
}
component.Repos = filteredRepos

diffComponents = append(diffComponents, component)
}
return diffComponents, nil
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

package creator
package filters

import (
"testing"
Expand All @@ -10,25 +10,27 @@ import (
"github.com/stretchr/testify/require"
)

func TestRemoveCopiesFromComponents(t *testing.T) {
components := []types.ZarfComponent{
{
Images: []string{
"example.com/include-image-tag:latest",
"example.com/image-with-tag:v1",
"example.com/diff-image-with-tag:v1",
"example.com/image-with-digest@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"example.com/diff-image-with-digest@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"example.com/image-with-tag-and-digest:v1@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"example.com/diff-image-with-tag-and-digest:v1@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
Repos: []string{
"https://example.com/no-ref.git",
"https://example.com/branch.git@refs/heads/main",
"https://example.com/tag.git@v1",
"https://example.com/diff-tag.git@v1",
"https://example.com/commit.git@524980951ff16e19dc25232e9aea8fd693989ba6",
"https://example.com/diff-commit.git@524980951ff16e19dc25232e9aea8fd693989ba6",
func TestCopyFilter(t *testing.T) {
pkg := types.ZarfPackage{
Components: []types.ZarfComponent{
{
Images: []string{
"example.com/include-image-tag:latest",
"example.com/image-with-tag:v1",
"example.com/diff-image-with-tag:v1",
"example.com/image-with-digest@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"example.com/diff-image-with-digest@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"example.com/image-with-tag-and-digest:v1@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
"example.com/diff-image-with-tag-and-digest:v1@sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
Repos: []string{
"https://example.com/no-ref.git",
"https://example.com/branch.git@refs/heads/main",
"https://example.com/tag.git@v1",
"https://example.com/diff-tag.git@v1",
"https://example.com/commit.git@524980951ff16e19dc25232e9aea8fd693989ba6",
"https://example.com/diff-commit.git@524980951ff16e19dc25232e9aea8fd693989ba6",
},
},
},
}
Expand All @@ -46,7 +48,9 @@ func TestRemoveCopiesFromComponents(t *testing.T) {
"https://example.com/diff-commit.git@524980951ff16e19dc25232e9aea8fd693989ba6": true,
},
}
diffComponents, err := removeCopiesFromComponents(components, &loadedDiffData)

filter := ByDifferentialData(&loadedDiffData)
diffComponents, err := filter.Apply(pkg)
require.NoError(t, err)

expectedImages := []string{
Expand Down
17 changes: 16 additions & 1 deletion src/pkg/packager/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
package packager

import (
"fmt"
"os"

"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/zarf/src/internal/packager/sbom"
"github.com/defenseunicorns/zarf/src/pkg/utils"
)
Expand All @@ -18,7 +22,18 @@ func (p *Packager) Inspect() (err error) {
return err
}

utils.ColorPrintYAML(p.cfg.Pkg, nil, false)
if p.cfg.InspectOpts.ListImages {
imageList := []string{}
for _, component := range p.cfg.Pkg.Components {
imageList = append(imageList, component.Images...)
}
imageList = helpers.Unique(imageList)
for _, image := range imageList {
fmt.Fprintln(os.Stdout, "-", image)
}
} else {
utils.ColorPrintYAML(p.cfg.Pkg, nil, false)
}

sbomDir := p.layout.SBOMs.Path

Expand Down
4 changes: 4 additions & 0 deletions src/test/e2e/06_create_sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func TestCreateSBOM(t *testing.T) {
_, err = os.ReadFile(filepath.Join(sbomPath, "dos-games", "docker.io_defenseunicorns_zarf-game_multi-tile-dark.json"))
require.NoError(t, err)

stdOut, _, err = e2e.Zarf("package", "inspect", pkgName, "--list-images")
require.NoError(t, err)
require.Equal(t, "- defenseunicorns/zarf-game:multi-tile-dark\n", stdOut)

// Pull the current zarf binary version to find the corresponding init package
version, stdErr, err := e2e.Zarf("version")
require.NoError(t, err, version, stdErr)
Expand Down
8 changes: 6 additions & 2 deletions src/types/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,12 @@ type ZarfPackageOptions struct {

// ZarfInspectOptions tracks the user-defined preferences during a package inspection.
type ZarfInspectOptions struct {
ViewSBOM bool `json:"sbom" jsonschema:"description=View SBOM contents while inspecting the package"`
SBOMOutputDir string `json:"sbomOutput" jsonschema:"description=Location to output an SBOM into after package inspection"`
// 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
}

// ZarfFindImagesOptions tracks the user-defined preferences during a prepare find-images search.
Expand Down

0 comments on commit ec51a9c

Please sign in to comment.