Skip to content

Commit

Permalink
1322 component tarballs (#1331)
Browse files Browse the repository at this point in the history
Merging into a feature branch for OCI package stuffs
  • Loading branch information
YrrepNoj authored Feb 13, 2023
1 parent fd140a5 commit 42308a2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ zarf tools archiver decompress {ARCHIVE} {DESTINATION} [flags]
## Options

```
-h, --help help for decompress
--decompress-all Decompress all layers in the archive
-h, --help help for decompress
```

## Options inherited from parent commands
Expand Down
21 changes: 21 additions & 0 deletions src/cmd/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package cmd

import (
"os"
"path/filepath"
"strings"

"github.com/anchore/syft/cmd/syft/cli"
"github.com/defenseunicorns/zarf/src/config"
Expand All @@ -22,6 +24,7 @@ import (
)

var subAltNames []string
var decompressLayers bool

var toolsCmd = &cobra.Command{
Use: "tools",
Expand Down Expand Up @@ -64,6 +67,23 @@ var archiverDecompressCmd = &cobra.Command{
if err != nil {
message.Fatal(err, lang.CmdToolsArchiverDecompressErr)
}

// Decompress component layers in the destination path
if decompressLayers {
layersDir := filepath.Join(destinationPath, "components")

files, err := os.ReadDir(layersDir)
if err != nil {
message.Fatalf(err, "failed to read the layers of components")
}
for _, file := range files {
if strings.HasSuffix(file.Name(), "tar.zst") {
if err := archiver.Unarchive(filepath.Join(layersDir, file.Name()), layersDir); err != nil {
message.Fatalf(err, "failed to decompress the component layer")
}
}
}
}
},
}

Expand Down Expand Up @@ -173,6 +193,7 @@ func init() {

archiverCmd.AddCommand(archiverCompressCmd)
archiverCmd.AddCommand(archiverDecompressCmd)
archiverDecompressCmd.Flags().BoolVar(&decompressLayers, "decompress-all", false, "Decompress all layers in the archive")

cranePlatformOptions := config.GetCraneOptions(false)

Expand Down
18 changes: 18 additions & 0 deletions src/pkg/packager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ func (p *Packager) loadZarfPkg() error {
return fmt.Errorf("unable to read the zarf.yaml in %s: %w", p.tmp.Base, err)
}

// Get a list of paths for the components of the package
components, err := os.ReadDir(p.tmp.Components)
if err != nil {
return fmt.Errorf("unable to get a list of components... %w", err)
}
for _, component := range components {
// If the components are compressed tarballs, un-compress them
componentPath := filepath.Join(p.tmp.Components, component.Name())
if !component.IsDir() && strings.HasSuffix(component.Name(), ".tar.zst") {
if err := archiver.Unarchive(componentPath, p.tmp.Components); err != nil {
return fmt.Errorf("unable to extract the component: %w", err)
}

// After extracting the component, remove the compressed tarball to release disk space
_ = os.Remove(filepath.Join(p.tmp.Components, component.Name()))
}
}

// If SBOM files exist, temporarily place them in the deploy directory
if err := sbom.OutputSBOMFiles(p.tmp, config.ZarfSBOMDir, ""); err != nil {
// Don't stop the deployment, let the user decide if they want to continue the deployment
Expand Down
17 changes: 17 additions & 0 deletions src/pkg/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,23 @@ func (p *Packager) Create(baseDir string) error {
sbom.Catalog(componentSBOMs, imgList, p.tmp.Images, p.tmp.Sboms)
}

// Process the component directories into compressed tarballs
// NOTE: This is purposefully being done after the SBOM cataloging
for _, component := range p.cfg.Pkg.Components {
// Make the component a tar.zst archive
componentPaths, _ := p.createComponentPaths(component)
componentName := fmt.Sprintf("%s.%s", component.Name, "tar.zst")
componentTarPath := filepath.Join(p.tmp.Components, componentName)
if err := archiver.Archive([]string{componentPaths.Base}, componentTarPath); err != nil {
return fmt.Errorf("unable to create package: %w", err)
}

// Remove the deflated component directory
if err := os.RemoveAll(componentPaths.Base); err != nil {
message.Debugf("unable to remove the component directory (%s): %s", componentPaths.Base, err.Error())
}
}

// In case the directory was changed, reset to prevent breaking relative target paths
if originalDir != "" {
_ = os.Chdir(originalDir)
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/04_create_templating_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestCreateTemplating(t *testing.T) {
stdOut, stdErr, err := e2e.execZarfCommand("package", "create", "examples/package-variables", "--set", "CONFIG_MAP=simple-configmap.yaml", "--set", "ACTION=template", "--confirm", "--zarf-cache", cachePath)
require.NoError(t, err, stdOut, stdErr)

stdOut, stdErr, err = e2e.execZarfCommand("t", "archiver", "decompress", pkgName, decompressPath)
stdOut, stdErr, err = e2e.execZarfCommand("t", "archiver", "decompress", pkgName, decompressPath, "--decompress-all", "-l=trace")
require.NoError(t, err, stdOut, stdErr)

// Check that the configmap exists and is readable
Expand Down
2 changes: 1 addition & 1 deletion src/test/e2e/22_git_and_flux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func testRemovingTagsOnCreate(t *testing.T) {

// Extract the built package so we can inspect the repositories that are included
extractedDirPath := "tmp-extraction"
stdOut, stdErr, err := e2e.execZarfCommand("tools", "archiver", "decompress", testPackagePath, extractedDirPath, "-l=trace")
stdOut, stdErr, err := e2e.execZarfCommand("tools", "archiver", "decompress", testPackagePath, extractedDirPath, "-l=trace", "--decompress-all")
defer e2e.cleanFiles(extractedDirPath)
require.NoError(t, err, stdOut, stdErr)

Expand Down

0 comments on commit 42308a2

Please sign in to comment.