Skip to content

Commit 9b3dfb6

Browse files
AustinAbro321Racer159Noxsios
authored
feat: set variables on find images (#2282)
## Description Currently when you run `zarf prepare find-images` with a package that has variables in it, the variables don't get set, even to their defaults. This can break the helm chart, or make us unable to find images. This PR introduces a `--deploy-set` flag. Additionally we take in the default values to the chart. ## Type of change - [X] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Other (security config, docs update, etc) ## Checklist before merging - [ ] Test, docs, adr added or updated as needed - [x] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow) followed --------- Co-authored-by: Wayne Starr <[email protected]> Co-authored-by: razzle <[email protected]>
1 parent 5efcb35 commit 9b3dfb6

38 files changed

+515
-542
lines changed

.github/actions/k3d/action.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ runs:
77
- run: "curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash"
88
shell: bash
99

10-
- run: k3d cluster delete && k3d cluster create
10+
- run: k3d cluster delete && k3d cluster create --k3s-arg="--disable=traefik@server:0"
1111
shell: bash

docs/2-the-zarf-cli/100-cli-commands/zarf_dev_find-images.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ zarf dev find-images [ PACKAGE ] [flags]
1616
## Options
1717

1818
```
19-
-h, --help help for find-images
20-
--kube-version string Override the default helm template KubeVersion when performing a package chart template
21-
-p, --repo-chart-path string If git repos hold helm charts, often found with gitops tools, specify the chart path, e.g. "/" or "/chart"
22-
--set stringToString Specify package variables to set on the command line (KEY=value). Note, if using a config file, this will be set by [package.create.set]. (default [])
23-
--why string Find the location of the image given as an argument and print it to the console.
19+
--create-set stringToString Specify package variables to set on the command line (KEY=value). Note, if using a config file, this will be set by [package.create.set]. (default [])
20+
--deploy-set stringToString Specify deployment variables to set on the command line (KEY=value) (default [])
21+
-f, --flavor string The flavor of components to include in the resulting package (i.e. have a matching or empty "only.flavor" key)
22+
-h, --help help for find-images
23+
--kube-version string Override the default helm template KubeVersion when performing a package chart template
24+
--registry-url string Override the ###ZARF_REGISTRY### value (default "127.0.0.1:31999")
25+
-p, --repo-chart-path string If git repos hold helm charts, often found with gitops tools, specify the chart path, e.g. "/" or "/chart"
26+
--why string Prints the source manifest for the specified image
2427
```
2528

2629
## Options inherited from parent commands

src/cmd/dev.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/defenseunicorns/zarf/src/pkg/transform"
2222
"github.com/defenseunicorns/zarf/src/pkg/utils"
2323
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
24+
"github.com/defenseunicorns/zarf/src/types"
2425
"github.com/mholt/archiver/v3"
2526
"github.com/spf13/cobra"
2627
"github.com/spf13/viper"
@@ -190,8 +191,11 @@ var devFindImagesCmd = &cobra.Command{
190191

191192
// Ensure uppercase keys from viper
192193
v := common.GetViper()
194+
193195
pkgConfig.CreateOpts.SetVariables = helpers.TransformAndMergeMap(
194196
v.GetStringMapString(common.VPkgCreateSet), pkgConfig.CreateOpts.SetVariables, strings.ToUpper)
197+
pkgConfig.PkgOpts.SetVariables = helpers.TransformAndMergeMap(
198+
v.GetStringMapString(common.VPkgDeploySet), pkgConfig.PkgOpts.SetVariables, strings.ToUpper)
195199

196200
// Configure the packager
197201
pkgClient := packager.NewOrDie(&pkgConfig)
@@ -265,14 +269,23 @@ func init() {
265269
devFindImagesCmd.Flags().StringVarP(&pkgConfig.FindImagesOpts.RepoHelmChartPath, "repo-chart-path", "p", "", lang.CmdDevFlagRepoChartPath)
266270
// use the package create config for this and reset it here to avoid overwriting the config.CreateOptions.SetVariables
267271
devFindImagesCmd.Flags().StringToStringVar(&pkgConfig.CreateOpts.SetVariables, "set", v.GetStringMapString(common.VPkgCreateSet), lang.CmdDevFlagSet)
272+
273+
devFindImagesCmd.Flags().MarkDeprecated("set", "this field is replaced by create-set")
274+
devFindImagesCmd.Flags().MarkHidden("set")
275+
devFindImagesCmd.Flags().StringVarP(&pkgConfig.CreateOpts.Flavor, "flavor", "f", v.GetString(common.VPkgCreateFlavor), lang.CmdPackageCreateFlagFlavor)
276+
devFindImagesCmd.Flags().StringToStringVar(&pkgConfig.CreateOpts.SetVariables, "create-set", v.GetStringMapString(common.VPkgCreateSet), lang.CmdDevFlagSet)
277+
devFindImagesCmd.Flags().StringToStringVar(&pkgConfig.PkgOpts.SetVariables, "deploy-set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdPackageDeployFlagSet)
268278
// allow for the override of the default helm KubeVersion
269279
devFindImagesCmd.Flags().StringVar(&pkgConfig.FindImagesOpts.KubeVersionOverride, "kube-version", "", lang.CmdDevFlagKubeVersion)
270280
// check which manifests are using this particular image
271281
devFindImagesCmd.Flags().StringVar(&pkgConfig.FindImagesOpts.Why, "why", "", lang.CmdDevFlagFindImagesWhy)
272282

283+
defaultRegistry := fmt.Sprintf("%s:%d", helpers.IPV4Localhost, types.ZarfInClusterContainerRegistryNodePort)
284+
devFindImagesCmd.Flags().StringVar(&pkgConfig.FindImagesOpts.RegistryURL, "registry-url", defaultRegistry, lang.CmdDevFlagFindImagesRegistry)
285+
273286
devLintCmd.Flags().StringToStringVar(&pkgConfig.CreateOpts.SetVariables, "set", v.GetStringMapString(common.VPkgCreateSet), lang.CmdPackageCreateFlagSet)
274287
devLintCmd.Flags().StringVarP(&pkgConfig.CreateOpts.Flavor, "flavor", "f", v.GetString(common.VPkgCreateFlavor), lang.CmdPackageCreateFlagFlavor)
275-
devTransformGitLinksCmd.Flags().StringVar(&pkgConfig.InitOpts.GitServer.PushUsername, "git-account", config.ZarfGitPushUser, lang.CmdDevFlagGitAccount)
288+
devTransformGitLinksCmd.Flags().StringVar(&pkgConfig.InitOpts.GitServer.PushUsername, "git-account", types.ZarfGitPushUser, lang.CmdDevFlagGitAccount)
276289
}
277290

278291
func bindDevDeployFlags(v *viper.Viper) {

src/cmd/initialize.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/defenseunicorns/zarf/src/pkg/utils"
2424
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
2525
"github.com/defenseunicorns/zarf/src/pkg/zoci"
26+
"github.com/defenseunicorns/zarf/src/types"
2627

2728
"github.com/spf13/cobra"
2829
)
@@ -181,8 +182,8 @@ func init() {
181182

182183
// Init package variable defaults that are non-zero values
183184
// NOTE: these are not in common.setDefaults so that zarf tools update-creds does not erroneously update values back to the default
184-
v.SetDefault(common.VInitGitPushUser, config.ZarfGitPushUser)
185-
v.SetDefault(common.VInitRegistryPushUser, config.ZarfRegistryPushUser)
185+
v.SetDefault(common.VInitGitPushUser, types.ZarfGitPushUser)
186+
v.SetDefault(common.VInitRegistryPushUser, types.ZarfRegistryPushUser)
186187

187188
// Init package set variable flags
188189
initCmd.Flags().StringToStringVar(&pkgConfig.PkgOpts.SetVariables, "set", v.GetStringMapString(common.VPkgDeploySet), lang.CmdInitFlagSet)

src/cmd/package.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/defenseunicorns/zarf/src/pkg/message"
1616
"github.com/defenseunicorns/zarf/src/pkg/packager/sources"
1717
"github.com/defenseunicorns/zarf/src/pkg/utils"
18+
"github.com/defenseunicorns/zarf/src/types"
1819

1920
"oras.land/oras-go/v2/registry"
2021

@@ -395,8 +396,8 @@ func bindMirrorFlags(v *viper.Viper) {
395396

396397
// Init package variable defaults that are non-zero values
397398
// NOTE: these are not in common.setDefaults so that zarf tools update-creds does not erroneously update values back to the default
398-
v.SetDefault(common.VInitGitPushUser, config.ZarfGitPushUser)
399-
v.SetDefault(common.VInitRegistryPushUser, config.ZarfRegistryPushUser)
399+
v.SetDefault(common.VInitGitPushUser, types.ZarfGitPushUser)
400+
v.SetDefault(common.VInitRegistryPushUser, types.ZarfRegistryPushUser)
400401

401402
// Always require confirm flag (no viper)
402403
mirrorFlags.BoolVar(&config.CommonOptions.Confirm, "confirm", false, lang.CmdPackageDeployFlagConfirm)

src/config/config.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ const (
2626
GithubProject = "defenseunicorns/zarf"
2727

2828
// ZarfMaxChartNameLength limits helm chart name size to account for K8s/helm limits and zarf prefix
29-
ZarfMaxChartNameLength = 40
30-
ZarfGeneratedPasswordLen = 24
31-
ZarfGeneratedSecretLen = 48
29+
ZarfMaxChartNameLength = 40
3230

3331
ZarfAgentHost = "agent-hook.zarf.svc"
3432

@@ -53,16 +51,6 @@ const (
5351
ZarfImagePullSecretName = "private-registry"
5452
ZarfGitServerSecretName = "private-git-server"
5553

56-
ZarfRegistryPushUser = "zarf-push"
57-
ZarfRegistryPullUser = "zarf-pull"
58-
ZarfInClusterContainerRegistryNodePort = 31999
59-
60-
ZarfGitPushUser = "zarf-git-user"
61-
ZarfGitReadUser = "zarf-git-read-user"
62-
63-
ZarfInClusterGitServiceURL = "http://zarf-gitea-http.zarf.svc.cluster.local:3000"
64-
ZarfInClusterArtifactServiceURL = ZarfInClusterGitServiceURL + "/api/packages/" + ZarfGitPushUser
65-
6654
ZarfLoggingUser = "zarf-admin"
6755
)
6856

src/config/lang/english.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,13 @@ $ zarf package pull oci://ghcr.io/defenseunicorns/packages/dos-games:1.0.0 -a sk
379379
"NOTE: This file must not already exist. If no filename is provided, the config will be written to the current working directory as zarf-config.toml."
380380
CmdDevGenerateConfigErr = "Unable to write the config file %s, make sure the file doesn't already exist"
381381

382-
CmdDevFlagExtractPath = `The path inside of an archive to use to calculate the sha256sum (i.e. for use with "files.extractPath")`
383-
CmdDevFlagSet = "Specify package variables to set on the command line (KEY=value). Note, if using a config file, this will be set by [package.create.set]."
384-
CmdDevFlagRepoChartPath = `If git repos hold helm charts, often found with gitops tools, specify the chart path, e.g. "/" or "/chart"`
385-
CmdDevFlagGitAccount = "User or organization name for the git account that the repos are created under."
386-
CmdDevFlagKubeVersion = "Override the default helm template KubeVersion when performing a package chart template"
387-
CmdDevFlagFindImagesWhy = "Find the location of the image given as an argument and print it to the console."
382+
CmdDevFlagExtractPath = `The path inside of an archive to use to calculate the sha256sum (i.e. for use with "files.extractPath")`
383+
CmdDevFlagSet = "Specify package variables to set on the command line (KEY=value). Note, if using a config file, this will be set by [package.create.set]."
384+
CmdDevFlagRepoChartPath = `If git repos hold helm charts, often found with gitops tools, specify the chart path, e.g. "/" or "/chart"`
385+
CmdDevFlagGitAccount = "User or organization name for the git account that the repos are created under."
386+
CmdDevFlagKubeVersion = "Override the default helm template KubeVersion when performing a package chart template"
387+
CmdDevFlagFindImagesRegistry = "Override the ###ZARF_REGISTRY### value"
388+
CmdDevFlagFindImagesWhy = "Prints the source manifest for the specified image"
388389

389390
CmdDevLintShort = "Lints the given package for valid schema and recommended practices"
390391
CmdDevLintLong = "Verifies the package schema, checks if any variables won't be evaluated, and checks for unpinned images/repos/files"

src/extensions/bigbang/bigbang.go

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func Run(YOLO bool, tmpPaths *layout.ComponentPaths, c types.ZarfComponent) (typ
9494
},
9595
path.Join(tmpPaths.Temp, bb),
9696
path.Join(tmpPaths.Temp, bb, "values"),
97+
helm.WithPackageConfig(&types.PackagerConfig{}),
9798
)
9899

99100
// Download the chart from Git and save it to a temporary directory.

src/internal/agent/hooks/argocd-repository.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"encoding/json"
1010
"fmt"
1111

12-
"github.com/defenseunicorns/zarf/src/config"
1312
"github.com/defenseunicorns/zarf/src/config/lang"
1413
"github.com/defenseunicorns/zarf/src/internal/agent/operations"
1514
"github.com/defenseunicorns/zarf/src/internal/agent/state"
@@ -102,7 +101,7 @@ func mutateRepository(r *v1.AdmissionRequest) (result *operations.Result, err er
102101
func populateArgoRepositoryPatchOperations(repoURL string, zarfGitPullPassword string) []operations.PatchOperation {
103102
var patches []operations.PatchOperation
104103
patches = append(patches, operations.ReplacePatchOperation("/data/url", base64.StdEncoding.EncodeToString([]byte(repoURL))))
105-
patches = append(patches, operations.ReplacePatchOperation("/data/username", base64.StdEncoding.EncodeToString([]byte(config.ZarfGitReadUser))))
104+
patches = append(patches, operations.ReplacePatchOperation("/data/username", base64.StdEncoding.EncodeToString([]byte(types.ZarfGitReadUser))))
106105
patches = append(patches, operations.ReplacePatchOperation("/data/password", base64.StdEncoding.EncodeToString([]byte(zarfGitPullPassword))))
107106

108107
return patches

src/internal/packager/helm/chart.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ func (h *Helm) InstallOrUpgradeChart() (types.ConnectStrings, string, error) {
139139
}
140140

141141
// TemplateChart generates a helm template from a given chart.
142-
func (h *Helm) TemplateChart() (string, chartutil.Values, error) {
142+
func (h *Helm) TemplateChart() (manifest string, chartValues chartutil.Values, err error) {
143143
message.Debugf("helm.TemplateChart()")
144144
spinner := message.NewProgressSpinner("Templating helm chart %s", h.chart.Name)
145145
defer spinner.Stop()
146146

147-
err := h.createActionConfig(h.chart.Namespace, spinner)
147+
err = h.createActionConfig(h.chart.Namespace, spinner)
148148

149149
// Setup K8s connection.
150150
if err != nil {
@@ -183,13 +183,18 @@ func (h *Helm) TemplateChart() (string, chartutil.Values, error) {
183183
return "", nil, fmt.Errorf("unable to load chart data: %w", err)
184184
}
185185

186+
client.PostRenderer, err = h.newRenderer()
187+
if err != nil {
188+
return "", nil, fmt.Errorf("unable to create helm renderer: %w", err)
189+
}
190+
186191
// Perform the loadedChart installation.
187192
templatedChart, err := client.Run(loadedChart, chartValues)
188193
if err != nil {
189194
return "", nil, fmt.Errorf("error generating helm chart template: %w", err)
190195
}
191196

192-
manifest := templatedChart.Manifest
197+
manifest = templatedChart.Manifest
193198

194199
for _, hook := range templatedChart.Hooks {
195200
manifest += fmt.Sprintf("\n---\n%s", hook.Manifest)
@@ -243,7 +248,7 @@ func (h *Helm) UpdateReleaseValues(updatedValues map[string]interface{}) error {
243248
// Namespace must be specified.
244249
client.Namespace = h.chart.Namespace
245250

246-
// Post-processing our manifests for reasons....
251+
// Post-processing our manifests to apply vars and run zarf helm logic in cluster
247252
client.PostRenderer = postRender
248253

249254
// Set reuse values to only override the values we are explicitly given
@@ -285,7 +290,7 @@ func (h *Helm) installChart(postRender *renderer) (*release.Release, error) {
285290
// Namespace must be specified.
286291
client.Namespace = h.chart.Namespace
287292

288-
// Post-processing our manifests for reasons....
293+
// Post-processing our manifests to apply vars and run zarf helm logic in cluster
289294
client.PostRenderer = postRender
290295

291296
loadedChart, chartValues, err := h.loadChartData()
@@ -298,10 +303,6 @@ func (h *Helm) installChart(postRender *renderer) (*release.Release, error) {
298303
}
299304

300305
func (h *Helm) upgradeChart(lastRelease *release.Release, postRender *renderer) (*release.Release, error) {
301-
// Print the postRender object piece by piece to not print the htpasswd
302-
message.Debugf("helm.upgradeChart(%#v, %#v, %#v, %#v, %s)", postRender.actionConfig, postRender.connectStrings,
303-
postRender.namespaces, postRender.Helm, fmt.Sprintf("values:template.Values{ registry: \"%s\" }", postRender.values.GetRegistry()))
304-
305306
// Migrate any deprecated APIs (if applicable)
306307
err := h.migrateDeprecatedAPIs(lastRelease)
307308
if err != nil {
@@ -322,7 +323,7 @@ func (h *Helm) upgradeChart(lastRelease *release.Release, postRender *renderer)
322323
// Namespace must be specified.
323324
client.Namespace = h.chart.Namespace
324325

325-
// Post-processing our manifests for reasons....
326+
// Post-processing our manifests to apply vars and run zarf helm logic in cluster
326327
client.PostRenderer = postRender
327328

328329
loadedChart, chartValues, err := h.loadChartData()

src/internal/packager/helm/common.go

+12
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,19 @@ func WithKubeVersion(kubeVersion string) Modifier {
148148
}
149149
}
150150

151+
// WithPackageConfig sets the packager config for the chart
152+
func WithPackageConfig(cfg *types.PackagerConfig) Modifier {
153+
return func(h *Helm) {
154+
h.cfg = cfg
155+
}
156+
}
157+
151158
// StandardName generates a predictable full path for a helm chart for Zarf.
152159
func StandardName(destination string, chart types.ZarfChart) string {
153160
return filepath.Join(destination, chart.Name+"-"+chart.Version)
154161
}
162+
163+
// StandardValuesName generates a predictable full path for the values file for a helm chart for zarf
164+
func StandardValuesName(destination string, chart types.ZarfChart, idx int) string {
165+
return fmt.Sprintf("%s-%d", StandardName(destination, chart), idx)
166+
}

0 commit comments

Comments
 (0)