Skip to content

Commit

Permalink
fix: ensure empty helm overrides don't break secrets (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleGedd authored Nov 17, 2023
1 parent 42c7306 commit 2ec0951
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 16 deletions.
20 changes: 12 additions & 8 deletions src/pkg/bundle/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,21 @@ func (b *Bundler) loadChartOverrides(pkg types.BundleZarfPackage) (ZarfOverrideM

// Loop through each path in Overrides.Variables
for _, override := range pkg.Overrides.Variables {
// Set the default value
val := override.Default

// If the variable is set, override the default value, why is this lowercase?
name := strings.ToLower(override.Name)
if setVal, ok := b.cfg.DeployOpts.ZarfPackageVariables[pkg.Name].Set[name]; ok {
val = setVal
var overrideVal interface{}
configFileOverride, existsInConfig := b.cfg.DeployOpts.ZarfPackageVariables[pkg.Name].Set[strings.ToLower(override.Name)]
if override.Default == nil && !existsInConfig {
// no default or config value, use values from underlying chart
continue
} else if existsInConfig {
// if the config value is set, use it
overrideVal = configFileOverride
} else {
// use default value if no config value is set
overrideVal = override.Default
}

// Add the override to the map, or return an error if the path is invalid
if err := addOverrideValue(overrideMap, override.Path, val); err != nil {
if err := addOverrideValue(overrideMap, override.Path, overrideVal); err != nil {
return nil, err
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/test/bundles/07-helm-overrides/uds-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,21 @@ zarf-packages:

overrides:
values:
- path: "podinfo-component/podinfo-chart/replicaCount"
- path: "podinfo-component/unicorn-podinfo/podinfo.replicaCount"
value: 2

variables:
- name: UI_COLOR
path: "podinfo-component/podinfo-chart/ui.color"
path: "podinfo-component/unicorn-podinfo/podinfo.ui.color"
description: "Set the color for podinfo's UI"
default: "blue"

# no default, but set in uds-config.yaml
- name: UI_MSG
path: "podinfo-component/unicorn-podinfo/podinfo.ui.message"
description: "Set the message for podinfo's UI"

# if no default and not set in uds-config.yaml, use the value in the underlying chart's values.yaml
- name: SECRET_VAL
path: "podinfo-component/unicorn-podinfo/testSecret"
description: "testing a secret value"
4 changes: 3 additions & 1 deletion src/test/bundles/07-helm-overrides/uds-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ bundle:
zarf-packages:
helm:
set:
UI_COLOR: green # overrides UI_COLOR in uds-bundle.yaml
# overrides variables in uds-bundle.yaml
UI_COLOR: green
UI_MSG: "Hello Unicorn"
10 changes: 10 additions & 0 deletions src/test/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,13 @@ func (e2e *UDSE2ETest) GetGitRevision() (string, error) {

return strings.TrimSpace(out), nil
}

// HelmDepUpdate runs 'helm dependency update .' on the given path
func (e2e *UDSE2ETest) HelmDepUpdate(t *testing.T, path string) {
cmd := "helm"
args := strings.Split(fmt.Sprintf("dependency update ."), " ")
tmp := exec.PrintCfg()
tmp.Dir = path
_, _, err := exec.CmdWithContext(context.TODO(), tmp, cmd, args...)
require.NoError(t, err)
}
18 changes: 16 additions & 2 deletions src/test/e2e/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func TestBundleWithGitRepo(t *testing.T) {

func TestBundleWithHelmOverrides(t *testing.T) {
deployZarfInit(t)
e2e.HelmDepUpdate(t, "src/test/packages/helm/unicorn-podinfo")
e2e.CreateZarfPkg(t, "src/test/packages/helm")
bundleDir := "src/test/bundles/07-helm-overrides"
bundlePath := filepath.Join(bundleDir, fmt.Sprintf("uds-bundle-helm-overrides-%s-0.0.1.tar.zst", e2e.Arch))
Expand All @@ -216,17 +217,30 @@ func TestBundleWithHelmOverrides(t *testing.T) {
deploy(t, bundlePath)

// check values overrides
cmd := strings.Split("tools kubectl get deployment -n podinfo podinfo-chart -o=jsonpath='{.spec.replicas}'", " ")
cmd := strings.Split("tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.replicas}'", " ")
outputNumReplicas, _, err := e2e.UDS(cmd...)
require.Equal(t, "'2'", outputNumReplicas)
require.NoError(t, err)

// check variables overrides
cmd = strings.Split("tools kubectl get deploy -n podinfo podinfo-chart -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_COLOR\")].value}'", " ")
cmd = strings.Split("tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_COLOR\")].value}'", " ")
outputUIColor, _, err := e2e.UDS(cmd...)
require.Equal(t, "'green'", outputUIColor)
require.NoError(t, err)

// check variables overrides, no default but set in config
cmd = strings.Split("tools kubectl get deploy -n podinfo unicorn-podinfo -o=jsonpath='{.spec.template.spec.containers[0].env[?(@.name==\"PODINFO_UI_MESSAGE\")].value}'", " ")
outputMsg, _, err := e2e.UDS(cmd...)
require.Equal(t, "'Hello Unicorn'", outputMsg)
require.NoError(t, err)

// check variables overrides, no default and not set in config
cmd = strings.Split("tools kubectl get secret test-secret -n podinfo -o jsonpath=\"{.data.test}\"", " ")
secretValue, _, err := e2e.UDS(cmd...)
// expect the value to be from the underlying chart's values.yaml, no overrides
require.Equal(t, "\"dGVzdC1zZWNyZXQ=\"", secretValue)
require.NoError(t, err)

remove(t, bundlePath)
}

Expand Down
23 changes: 23 additions & 0 deletions src/test/packages/helm/unicorn-podinfo/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
6 changes: 6 additions & 0 deletions src/test/packages/helm/unicorn-podinfo/Chart.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
dependencies:
- name: podinfo
repository: https://stefanprodan.github.io/podinfo
version: 6.5.3
digest: sha256:052cb665b3d4b817c8d7c977689f91aabdd704010203d07233d9dff6b1de9865
generated: "2023-11-17T09:56:11.701225-06:00"
29 changes: 29 additions & 0 deletions src/test/packages/helm/unicorn-podinfo/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: v2
name: unicorn-podinfo
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.0.1

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"

dependencies:
- name: podinfo
version: 6.5.3
repository: https://stefanprodan.github.io/podinfo
7 changes: 7 additions & 0 deletions src/test/packages/helm/unicorn-podinfo/templates/secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: test-secret
type: Opaque
data:
test: {{ .Values.testSecret }}
5 changes: 5 additions & 0 deletions src/test/packages/helm/unicorn-podinfo/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
testSecret: "dGVzdC1zZWNyZXQ=" # test-secret
podinfo:
ui:
color: "purple"
message: "Hello from podinfo"
6 changes: 3 additions & 3 deletions src/test/packages/helm/zarf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ components:
images:
- ghcr.io/stefanprodan/podinfo:6.5.3
charts:
- name: podinfo-chart
url: oci://ghcr.io/stefanprodan/charts/podinfo
- name: unicorn-podinfo
localPath: ./unicorn-podinfo
namespace: podinfo
version: 6.5.3
version: 0.0.1

0 comments on commit 2ec0951

Please sign in to comment.