From ad480acff907013d933a3ac82df88ca7e25133cb Mon Sep 17 00:00:00 2001 From: Micah Nagel Date: Tue, 22 Oct 2024 10:02:07 -0600 Subject: [PATCH] fix: support registry override for dev deploy (yolo) (#3064) Signed-off-by: Micah Nagel --- src/cmd/dev.go | 11 +++++++++-- src/config/lang/english.go | 2 +- src/pkg/packager/dev.go | 19 +++++++++++++++++++ src/test/e2e/99_yolo_test.go | 10 +++++++++- .../99-registry-url/registry-url.yaml | 8 ++++++++ src/test/packages/99-registry-url/zarf.yaml | 16 ++++++++++++++++ src/types/runtime.go | 2 ++ 7 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/test/packages/99-registry-url/registry-url.yaml create mode 100644 src/test/packages/99-registry-url/zarf.yaml diff --git a/src/cmd/dev.go b/src/cmd/dev.go index 34e0f76776..8c5f690055 100644 --- a/src/cmd/dev.go +++ b/src/cmd/dev.go @@ -32,6 +32,8 @@ import ( var extractPath string +var defaultRegistry = fmt.Sprintf("%s:%d", helpers.IPV4Localhost, types.ZarfInClusterContainerRegistryNodePort) + var devCmd = &cobra.Command{ Use: "dev", Aliases: []string{"prepare", "prep"}, @@ -349,8 +351,7 @@ func init() { // skip searching cosign artifacts in find images devFindImagesCmd.Flags().BoolVar(&pkgConfig.FindImagesOpts.SkipCosign, "skip-cosign", false, lang.CmdDevFlagFindImagesSkipCosign) - defaultRegistry := fmt.Sprintf("%s:%d", helpers.IPV4Localhost, types.ZarfInClusterContainerRegistryNodePort) - devFindImagesCmd.Flags().StringVar(&pkgConfig.FindImagesOpts.RegistryURL, "registry-url", defaultRegistry, lang.CmdDevFlagFindImagesRegistry) + devFindImagesCmd.Flags().StringVar(&pkgConfig.FindImagesOpts.RegistryURL, "registry-url", defaultRegistry, lang.CmdDevFlagRegistry) devLintCmd.Flags().StringToStringVar(&pkgConfig.CreateOpts.SetVariables, "set", v.GetStringMapString(common.VPkgCreateSet), lang.CmdPackageCreateFlagSet) devLintCmd.Flags().StringVarP(&pkgConfig.CreateOpts.Flavor, "flavor", "f", v.GetString(common.VPkgCreateFlavor), lang.CmdPackageCreateFlagFlavor) @@ -364,6 +365,12 @@ func bindDevDeployFlags(v *viper.Viper) { devDeployFlags.StringToStringVar(&pkgConfig.CreateOpts.RegistryOverrides, "registry-override", v.GetStringMapString(common.VPkgCreateRegistryOverride), lang.CmdPackageCreateFlagRegistryOverride) devDeployFlags.StringVarP(&pkgConfig.CreateOpts.Flavor, "flavor", "f", v.GetString(common.VPkgCreateFlavor), lang.CmdPackageCreateFlagFlavor) + devDeployFlags.StringVar(&pkgConfig.DeployOpts.RegistryURL, "registry-url", defaultRegistry, lang.CmdDevFlagRegistry) + err := devDeployFlags.MarkHidden("registry-url") + if err != nil { + message.Debug("Unable to mark dev-deploy flag as hidden", "error", err) + } + devDeployFlags.StringToStringVar(&pkgConfig.PkgOpts.SetVariables, "deploy-set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdPackageDeployFlagSet) // Always require adopt-existing-resources flag (no viper) diff --git a/src/config/lang/english.go b/src/config/lang/english.go index c78e57a443..6d19dd57f0 100644 --- a/src/config/lang/english.go +++ b/src/config/lang/english.go @@ -354,7 +354,7 @@ $ zarf package pull oci://ghcr.io/defenseunicorns/packages/dos-games:1.0.0 -a sk CmdDevFlagRepoChartPath = `If git repos hold helm charts, often found with gitops tools, specify the chart path, e.g. "/" or "/chart"` CmdDevFlagGitAccount = "User or organization name for the git account that the repos are created under." CmdDevFlagKubeVersion = "Override the default helm template KubeVersion when performing a package chart template" - CmdDevFlagFindImagesRegistry = "Override the ###ZARF_REGISTRY### value" + CmdDevFlagRegistry = "Override the ###ZARF_REGISTRY### value" CmdDevFlagFindImagesWhy = "Prints the source manifest for the specified image" CmdDevFlagFindImagesSkipCosign = "Skip searching for cosign artifacts related to discovered images" diff --git a/src/pkg/packager/dev.go b/src/pkg/packager/dev.go index de7af6f4af..1985edb655 100644 --- a/src/pkg/packager/dev.go +++ b/src/pkg/packager/dev.go @@ -16,6 +16,7 @@ import ( "github.com/zarf-dev/zarf/src/pkg/message" "github.com/zarf-dev/zarf/src/pkg/packager/creator" "github.com/zarf-dev/zarf/src/pkg/packager/filters" + "github.com/zarf-dev/zarf/src/types" ) // DevDeploy creates + deploys a package in one shot @@ -76,6 +77,24 @@ func (p *Packager) DevDeploy(ctx context.Context) error { if !p.cfg.CreateOpts.NoYOLO { p.cfg.Pkg.Metadata.YOLO = true + + // Set default builtin values so they exist in case any helm charts rely on them + registryInfo := types.RegistryInfo{Address: p.cfg.DeployOpts.RegistryURL} + if err := registryInfo.FillInEmptyValues(); err != nil { + return err + } + gitServer := types.GitServerInfo{} + if err = gitServer.FillInEmptyValues(); err != nil { + return err + } + artifactServer := types.ArtifactServerInfo{} + artifactServer.FillInEmptyValues() + + p.state = &types.ZarfState{ + RegistryInfo: registryInfo, + GitServer: gitServer, + ArtifactServer: artifactServer, + } } else { p.hpaModified = false // Reset registry HPA scale down whether an error occurs or not diff --git a/src/test/e2e/99_yolo_test.go b/src/test/e2e/99_yolo_test.go index 03281df1a7..b9203ed000 100644 --- a/src/test/e2e/99_yolo_test.go +++ b/src/test/e2e/99_yolo_test.go @@ -53,9 +53,17 @@ func TestDevDeploy(t *testing.T) { return } + // Generic test of dev deploy stdOut, stdErr, err := e2e.Zarf(t, "dev", "deploy", "examples/dos-games") require.NoError(t, err, stdOut, stdErr) - stdOut, stdErr, err = e2e.Zarf(t, "package", "remove", "dos-games", "--confirm") + stdOut, stdErr, err = e2e.Zarf(t, "tools", "kubectl", "delete", "namespace", "dos-games") + require.NoError(t, err, stdOut, stdErr) + + // Special test of hidden registry-url flag + stdOut, stdErr, err = e2e.Zarf(t, "dev", "deploy", "src/test/packages/99-registry-url", "--registry-url", "ghcr.io") + require.NoError(t, err, stdOut, stdErr) + + stdOut, stdErr, err = e2e.Zarf(t, "tools", "kubectl", "delete", "namespace", "registry-url") require.NoError(t, err, stdOut, stdErr) } diff --git a/src/test/packages/99-registry-url/registry-url.yaml b/src/test/packages/99-registry-url/registry-url.yaml new file mode 100644 index 0000000000..1332cf9fde --- /dev/null +++ b/src/test/packages/99-registry-url/registry-url.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +kind: Pod +metadata: + name: registry-url +spec: + containers: + - name: podinfo + image: "###ZARF_REGISTRY###/stefanprodan/podinfo:6.4.0" diff --git a/src/test/packages/99-registry-url/zarf.yaml b/src/test/packages/99-registry-url/zarf.yaml new file mode 100644 index 0000000000..1e4aebc4dc --- /dev/null +++ b/src/test/packages/99-registry-url/zarf.yaml @@ -0,0 +1,16 @@ +kind: ZarfPackageConfig +metadata: + name: registry-url + description: Deploys a simple pod with the special ZARF_REGISTRY value + +components: + - name: registry-url + required: true + manifests: + - name: registry-url + namespace: registry-url + noWait: true + files: + - registry-url.yaml + images: + - ghcr.io/stefanprodan/podinfo:6.4.0 diff --git a/src/types/runtime.go b/src/types/runtime.go index 03ad9375fd..aad1bd000c 100644 --- a/src/types/runtime.go +++ b/src/types/runtime.go @@ -78,6 +78,8 @@ type ZarfDeployOptions struct { Timeout time.Duration // [Library Only] A map of component names to chart names containing Helm Chart values to override values on deploy ValuesOverridesMap map[string]map[string]map[string]interface{} + // [Dev Deploy Only] Manual override for ###ZARF_REGISTRY### + RegistryURL string } // ZarfMirrorOptions tracks the user-defined preferences during a package mirror.