From 7aa3224cc75dc5ace059fe0897cc4ee307a11f77 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Thu, 21 Nov 2024 17:35:01 +0100 Subject: [PATCH] Set Helm max history to limit secret creation (#3249) Signed-off-by: Philip Laine --- src/internal/packager/helm/chart.go | 6 +++++ src/test/e2e/25_helm_test.go | 26 ++++++++++++++++--- .../25-helm-release-history/chart/Chart.yaml | 5 ++++ .../chart/templates/configmap.yaml | 6 +++++ .../25-helm-release-history/zarf.yaml | 12 +++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 src/test/packages/25-helm-release-history/chart/Chart.yaml create mode 100644 src/test/packages/25-helm-release-history/chart/templates/configmap.yaml create mode 100644 src/test/packages/25-helm-release-history/zarf.yaml diff --git a/src/internal/packager/helm/chart.go b/src/internal/packager/helm/chart.go index ac79a0fc1d..59454057bf 100644 --- a/src/internal/packager/helm/chart.go +++ b/src/internal/packager/helm/chart.go @@ -34,6 +34,9 @@ import ( "github.com/zarf-dev/zarf/src/types" ) +// Use same default as Helm CLI does. +const maxHelmHistory = 10 + // InstallOrUpgradeChart performs a helm install of the given chart. func (h *Helm) InstallOrUpgradeChart(ctx context.Context) (types.ConnectStrings, string, error) { l := logger.From(ctx) @@ -349,6 +352,8 @@ func (h *Helm) upgradeChart(ctx context.Context, lastRelease *release.Release, p // Post-processing our manifests to apply vars and run zarf helm logic in cluster client.PostRenderer = postRender + client.MaxHistory = maxHelmHistory + loadedChart, chartValues, err := h.loadChartData() if err != nil { return nil, fmt.Errorf("unable to load chart data: %w", err) @@ -365,6 +370,7 @@ func (h *Helm) rollbackChart(name string, version int) error { client.Wait = true client.Timeout = h.timeout client.Version = version + client.MaxHistory = maxHelmHistory return client.Run(name) } diff --git a/src/test/e2e/25_helm_test.go b/src/test/e2e/25_helm_test.go index 421b62ed0a..a741c000ac 100644 --- a/src/test/e2e/25_helm_test.go +++ b/src/test/e2e/25_helm_test.go @@ -8,18 +8,37 @@ import ( "fmt" "os/exec" "path/filepath" + "strings" "testing" "github.com/stretchr/testify/require" ) -var helmChartsPkg string +func TestHelmReleaseHistory(t *testing.T) { + outputPath := t.TempDir() + localTgzChartPath := filepath.Join("src", "test", "packages", "25-helm-release-history") + _, _, err := e2e.Zarf(t, "package", "create", localTgzChartPath, "-o", outputPath, "--confirm") + require.NoError(t, err) + + packagePath := filepath.Join(outputPath, fmt.Sprintf("zarf-package-helm-release-history-%s-0.0.1.tar.zst", e2e.Arch)) + for range 20 { + _, _, err = e2e.Zarf(t, "package", "deploy", packagePath, "--confirm") + require.NoError(t, err) + } + + stdout, err := exec.Command("helm", "history", "-n", "helm-release-history", "chart").Output() + require.NoError(t, err) + out := strings.TrimSpace(string(stdout)) + count := len(strings.Split(string(out), "\n")) + require.Equal(t, 11, count) + + _, _, err = e2e.Zarf(t, "package", "remove", packagePath, "--confirm") + require.NoError(t, err) +} func TestHelm(t *testing.T) { t.Log("E2E: Helm chart") - helmChartsPkg = filepath.Join("build", fmt.Sprintf("zarf-package-helm-charts-%s-0.0.1.tar.zst", e2e.Arch)) - testHelmUninstallRollback(t) testHelmAdoption(t) @@ -74,6 +93,7 @@ func testHelmChartsExample(t *testing.T) { require.NoError(t, err, stdOut, stdErr) // Deploy the example package. + helmChartsPkg := filepath.Join("build", fmt.Sprintf("zarf-package-helm-charts-%s-0.0.1.tar.zst", e2e.Arch)) stdOut, stdErr, err = e2e.Zarf(t, "package", "deploy", helmChartsPkg, "--confirm") require.NoError(t, err, stdOut, stdErr) require.Contains(t, string(stdErr), "registryOverrides", "registry overrides was not saved to build data") diff --git a/src/test/packages/25-helm-release-history/chart/Chart.yaml b/src/test/packages/25-helm-release-history/chart/Chart.yaml new file mode 100644 index 0000000000..d74e703bb8 --- /dev/null +++ b/src/test/packages/25-helm-release-history/chart/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v2 +name: chart +description: A Helm chart for Kubernetes +type: application +version: 0.1.0 diff --git a/src/test/packages/25-helm-release-history/chart/templates/configmap.yaml b/src/test/packages/25-helm-release-history/chart/templates/configmap.yaml new file mode 100644 index 0000000000..153ef97f50 --- /dev/null +++ b/src/test/packages/25-helm-release-history/chart/templates/configmap.yaml @@ -0,0 +1,6 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: test +data: + foo: bar diff --git a/src/test/packages/25-helm-release-history/zarf.yaml b/src/test/packages/25-helm-release-history/zarf.yaml new file mode 100644 index 0000000000..07df74d18c --- /dev/null +++ b/src/test/packages/25-helm-release-history/zarf.yaml @@ -0,0 +1,12 @@ +kind: ZarfPackageConfig +metadata: + name: helm-release-history + version: 0.0.1 +components: + - name: helm-release-history + required: true + charts: + - name: chart + namespace: helm-release-history + version: v0.1.0 + localPath: chart