From ac534a897bfdc67c9eb61ac7750fe6b0910694f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=9B=B8=E8=BD=A9?= <9570112+nauxliu@users.noreply.github.com> Date: Tue, 17 Dec 2019 07:27:42 +0800 Subject: [PATCH] Add historyMax (#1022) Resloves #868 --- README.md | 7 ++++++- pkg/helmexec/context.go | 1 + pkg/helmexec/exec.go | 8 ++++++++ pkg/state/state.go | 12 ++++++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 699e6722..cb473cb1 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ helmDefaults: tillerNamespace: tiller-namespace #dedicated default key for tiller-namespace tillerless: false #dedicated default key for tillerless kubeContext: kube-context #dedicated default key for kube-context (--kube-context) - cleanupOnFail: false #dedicated default key for helm flag --cleanup-on-fail + cleanupOnFail: false #dedicated default key for helm flag --cleanup-on-fail # additional and global args passed to helm args: - "--set k=v" @@ -92,6 +92,9 @@ helmDefaults: tlsCert: "path/to/cert.pem" # path to TLS key file (default "$HELM_HOME/key.pem") tlsKey: "path/to/key.pem" + # limit the maximum number of revisions saved per release. Use 0 for no limit (default 10) + historyMax: 10 + # The desired states of Helm releases. # @@ -176,6 +179,8 @@ releases: # CAUTION: this doesn't work as expected for `tilerless: true`. # See https://github.com/roboll/helmfile/issues/642 kubeContext: kube-context + # limit the maximum number of revisions saved per release. Use 0 for no limit (default 10) + historyMax: 10 # Local chart example - name: grafana # name of this release diff --git a/pkg/helmexec/context.go b/pkg/helmexec/context.go index 5181b738..4dfa7153 100644 --- a/pkg/helmexec/context.go +++ b/pkg/helmexec/context.go @@ -8,6 +8,7 @@ import ( type HelmContext struct { Tillerless bool TillerNamespace string + HistoryMax int WorkerIndex int } diff --git a/pkg/helmexec/exec.go b/pkg/helmexec/exec.go index f990da55..2940fc0d 100644 --- a/pkg/helmexec/exec.go +++ b/pkg/helmexec/exec.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "os" "path/filepath" + "strconv" "strings" "sync" @@ -124,6 +125,13 @@ func (helm *execer) SyncRelease(context HelmContext, name, chart string, flags . helm.logger.Infof("Upgrading release=%v, chart=%v", name, chart) preArgs := context.GetTillerlessArgs(helm) env := context.getTillerlessEnv() + + if helm.IsHelm3() { + flags = append(flags, "--history-max", strconv.Itoa(context.HistoryMax)) + } else { + env["HELM_TILLER_HISTORY_MAX"] = strconv.Itoa(context.HistoryMax) + } + out, err := helm.exec(append(append(preArgs, "upgrade", "--install", "--reset-values", name, chart), flags...), env) helm.write(out) return err diff --git a/pkg/state/state.go b/pkg/state/state.go index cb7a362d..ef8a7a22 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -103,6 +103,8 @@ type HelmSpec struct { Atomic bool `yaml:"atomic"` // CleanupOnFail, when set to true, the --cleanup-on-fail helm flag is passed to the upgrade command CleanupOnFail bool `yaml:"cleanupOnFail,omitempty"` + // HistoryMax, limit the maximum number of revisions saved per release. Use 0 for no limit (default 10) + HistoryMax *int `yaml:"historyMax,omitempty"` TLS bool `yaml:"tls"` TLSCACert string `yaml:"tlsCACert,omitempty"` @@ -143,6 +145,8 @@ type ReleaseSpec struct { Atomic *bool `yaml:"atomic,omitempty"` // CleanupOnFail, when set to true, the --cleanup-on-fail helm flag is passed to the upgrade command CleanupOnFail *bool `yaml:"cleanupOnFail,omitempty"` + // HistoryMax, limit the maximum number of revisions saved per release. Use 0 for no limit (default 10) + HistoryMax *int `yaml:"historyMax,omitempty"` // MissingFileHandler is set to either "Error" or "Warn". "Error" instructs helmfile to fail when unable to find a values or secrets file. When "Warn", it prints the file and continues. // The default value for MissingFileHandler is "Error". @@ -1058,11 +1062,19 @@ func (st *HelmState) createHelmContext(spec *ReleaseSpec, workerIndex int) helme if spec.Tillerless != nil { tillerless = *spec.Tillerless } + historyMax := 10 + if st.HelmDefaults.HistoryMax != nil { + historyMax = *st.HelmDefaults.HistoryMax + } + if spec.HistoryMax != nil { + historyMax = *spec.HistoryMax + } return helmexec.HelmContext{ Tillerless: tillerless, TillerNamespace: namespace, WorkerIndex: workerIndex, + HistoryMax: historyMax, } }