Skip to content

Commit

Permalink
Set Helm max history to limit secret creation
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Laine <[email protected]>
  • Loading branch information
phillebaba committed Nov 21, 2024
1 parent 2b43c91 commit 21dddee
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/internal/packager/helm/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
26 changes: 23 additions & 3 deletions src/test/e2e/25_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 10 {
_, _, 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, 6, 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)
Expand Down Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions src/test/packages/25-helm-release-history/chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v2
name: chart
description: A Helm chart for Kubernetes
type: application
version: 0.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: test
data:
foo: bar
12 changes: 12 additions & 0 deletions src/test/packages/25-helm-release-history/zarf.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 21dddee

Please sign in to comment.