Skip to content

Commit

Permalink
Add --zarf-cache flag to package create for customizing image cache l…
Browse files Browse the repository at this point in the history
…ocation (#446)
  • Loading branch information
UncleGedd authored Apr 21, 2022
1 parent 0880709 commit 5a38917
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 22 deletions.
17 changes: 17 additions & 0 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmd

import (
"fmt"
"github.com/defenseunicorns/zarf/src/internal/message"
"path/filepath"
"regexp"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/zarf/src/config"
Expand All @@ -11,6 +14,7 @@ import (

var insecureDeploy bool
var shasum string
var zarfImageCache string

var packageCmd = &cobra.Command{
Use: "package",
Expand All @@ -23,6 +27,9 @@ var packageCreateCmd = &cobra.Command{
Aliases: []string{"c"},
Short: "Create an update package to push to the gitops server (runs online)",
Run: func(cmd *cobra.Command, args []string) {
if cmd.Flag("zarf-cache").Changed && cachePathClean(zarfImageCache) {
config.SetImageCachePath(zarfImageCache)
}
packager.Create()
},
}
Expand Down Expand Up @@ -68,13 +75,23 @@ func choosePackage(args []string) string {
return path
}

func cachePathClean(cachePath string) bool {
var isCleanPath = regexp.MustCompile(`^[a-zA-Z0-9\_\-\/\.\~]+$`).MatchString
if !isCleanPath(cachePath) {
message.Warn(fmt.Sprintf("Invalid characters in Zarf cache path, defaulting to ~/%s", config.ZarfDefaultImageCachePath))
return false
}
return true
}

func init() {
rootCmd.AddCommand(packageCmd)
packageCmd.AddCommand(packageCreateCmd)
packageCmd.AddCommand(packageDeployCmd)
packageCmd.AddCommand(packageInspectCmd)

packageCreateCmd.Flags().BoolVar(&config.DeployOptions.Confirm, "confirm", false, "Confirm package creation without prompting")
packageCreateCmd.Flags().StringVar(&zarfImageCache, "zarf-cache", config.ZarfDefaultImageCachePath, "Specify the location of the Zarf image cache")
packageDeployCmd.Flags().BoolVar(&config.DeployOptions.Confirm, "confirm", false, "Confirm package deployment without prompting")
packageDeployCmd.Flags().StringVar(&config.DeployOptions.Components, "components", "", "Comma-separated list of components to install. Adding this flag will skip the init prompts for which components to install")
packageDeployCmd.Flags().BoolVar(&insecureDeploy, "insecure", false, "Skip shasum validation of remote package. Required if deploying a remote package and `--shasum` is not provided")
Expand Down
1 change: 0 additions & 1 deletion src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func init() {
// Re-add the original help function
originalHelp(c, s)
})

rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
rootCmd.PersistentFlags().StringVarP(&zarfLogLevel, "log-level", "l", "", "Log level when running Zarf. Valid options are: warn, info, debug, trace")
rootCmd.PersistentFlags().StringVarP(&arch, "architecture", "a", "", "Architecture for OCI images")
Expand Down
25 changes: 21 additions & 4 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ const (
ZarfConnectAnnotationDescription = "zarf.dev/connect-description"
ZarfConnectAnnotationUrl = "zarf.dev/connect-url"

ZarfManagedByLabel = "app.kubernetes.io/managed-by"
ZarfCleanupScriptsPath = "/opt/zarf"
ZarfManagedByLabel = "app.kubernetes.io/managed-by"
ZarfCleanupScriptsPath = "/opt/zarf"
ZarfDefaultImageCachePath = ".zarf-image-cache"
)

var (
Expand All @@ -49,8 +50,9 @@ var (
ZarfSeedPort string

// Private vars
active types.ZarfPackage
state types.ZarfState
zarfImageCachePath = ZarfDefaultImageCachePath
active types.ZarfPackage
state types.ZarfState
)

func IsZarfInitConfig() bool {
Expand Down Expand Up @@ -181,3 +183,18 @@ func BuildConfig(path string) error {

return utils.WriteYaml(path, active, 0400)
}

func SetImageCachePath(cachePath string) {
zarfImageCachePath = cachePath
}

func GetImageCachePath() string {
homePath, _ := os.UserHomeDir()
if zarfImageCachePath == ZarfDefaultImageCachePath {
return fmt.Sprintf("%s/%s", homePath, zarfImageCachePath)
}
if string(zarfImageCachePath[0]) == "~" {
return fmt.Sprintf("%s/%s", homePath, zarfImageCachePath[len("~/"):])
}
return zarfImageCachePath
}
13 changes: 0 additions & 13 deletions src/internal/images/common.go

This file was deleted.

3 changes: 2 additions & 1 deletion src/internal/images/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func PullAll(buildImageList []string, imageTarballPath string) {
if err != nil {
spinner.Fatalf(err, "Unable to pull the image %s", src)
}
img = cache.Image(img, cache.NewFilesystemCache(cachePath))
imageCachePath := config.GetImageCachePath()
img = cache.Image(img, cache.NewFilesystemCache(imageCachePath))
imageMap[src] = img
}

Expand Down
9 changes: 6 additions & 3 deletions src/test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ func getCLIName() string {
// doAllTheOtherStuff...
// }
func (e2e *ZarfE2ETest) cleanupAfterTest(t *testing.T) {
// Use Zarf to perform chart uninstallation
output, err := e2e.execZarfCommand("destroy", "--confirm", "--remove-components", "-l=trace")
require.NoError(t, err, output)
// Check if cluster is running and use Zarf to perform chart uninstallation
err := clusters.TryValidateClusterIsRunning()
if err == nil {
output, err := e2e.execZarfCommand("destroy", "--confirm", "--remove-components", "-l=trace")
require.NoError(t, err, output)
}

// Remove files created for the test
for _, filePath := range e2e.filesToRemove {
Expand Down
23 changes: 23 additions & 0 deletions src/test/e2e/e2e_zarf_create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package test

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestE2eZarfCreate(t *testing.T) {
defer e2e.cleanupAfterTest(t)

// run `zarf create` with a specified image cache location
imageCachePath := "/tmp/.image_cache-location"
output, err := e2e.execZarfCommand("package", "create", "--confirm", "--zarf-cache", imageCachePath)
require.NoError(t, err, output)

files, err := ioutil.ReadDir(imageCachePath)
require.NoError(t, err, "Error when reading image cache path")
assert.Greater(t, len(files), 1)
e2e.filesToRemove = append(e2e.filesToRemove, imageCachePath)
}
17 changes: 17 additions & 0 deletions src/test/e2e/zarf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
kind: ZarfInitConfig
metadata:
name: init
description: "Used for Zarf e2e tests"
seed: library/registry:2.7.1
components:
- name: container-registry-seed
required: true
import:
path: ../../../packages/zarf-registry
- name: container-registry
required: true
import:
path: ../../../packages/zarf-registry
- name: k3s
import:
path: ../../../packages/distros/k3s

0 comments on commit 5a38917

Please sign in to comment.