diff --git a/README.md b/README.md index 46aa4d61..08b28d2a 100644 --- a/README.md +++ b/README.md @@ -74,6 +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) + cleanup-on-fail: false #dedicated default key for helm flag --cleanup-on-fail # additional and global args passed to helm args: - "--set k=v" @@ -157,6 +158,8 @@ releases: installed: true # restores previous state in case of failed release atomic: true + # when true, cleans up any new resources created during a failed release + cleanup-on-fail: false # name of the tiller namespace tillerNamespace: vault # if true, will use the helm-tiller plugin diff --git a/pkg/state/state.go b/pkg/state/state.go index fbc22584..a1df6a34 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -101,6 +101,8 @@ type HelmSpec struct { Force bool `yaml:"force"` // Atomic, when set to true, restore previous state in case of a failed install/upgrade attempt Atomic bool `yaml:"atomic"` + // CleanupOnFail, when set to true, the --cleanup-on-fail helm flag is passed to the upgrade command + CleanupOnFail bool `yaml:"cleanup-on-fail,omitempty"` TLS bool `yaml:"tls"` TLSCACert string `yaml:"tlsCACert,omitempty"` @@ -139,6 +141,8 @@ type ReleaseSpec struct { Installed *bool `yaml:"installed,omitempty"` // Atomic, when set to true, restore previous state in case of a failed install/upgrade attempt 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:"cleanup-on-fail,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". @@ -1546,6 +1550,10 @@ func (st *HelmState) flagsForUpgrade(helm helmexec.Interface, release *ReleaseSp flags = append(flags, "--atomic") } + if release.CleanupOnFail != nil && *release.CleanupOnFail || release.CleanupOnFail == nil && st.HelmDefaults.CleanupOnFail { + flags = append(flags, "--cleanup-on-fail") + } + flags = st.appendConnectionFlags(flags, release) var err error diff --git a/pkg/state/state_test.go b/pkg/state/state_test.go index 3d00d201..86891bd0 100644 --- a/pkg/state/state_test.go +++ b/pkg/state/state_test.go @@ -448,6 +448,58 @@ func TestHelmState_flagsForUpgrade(t *testing.T) { "--namespace", "test-namespace", }, }, + { + name: "cleanup-on-fail", + defaults: HelmSpec{ + CleanupOnFail: false, + }, + release: &ReleaseSpec{ + Chart: "test/chart", + Version: "0.1", + CleanupOnFail: &enable, + Name: "test-charts", + Namespace: "test-namespace", + }, + want: []string{ + "--version", "0.1", + "--cleanup-on-fail", + "--namespace", "test-namespace", + }, + }, + { + name: "cleanup-on-fail-override-default", + defaults: HelmSpec{ + CleanupOnFail: true, + }, + release: &ReleaseSpec{ + Chart: "test/chart", + Version: "0.1", + CleanupOnFail: &disable, + Name: "test-charts", + Namespace: "test-namespace", + }, + want: []string{ + "--version", "0.1", + "--namespace", "test-namespace", + }, + }, + { + name: "cleanup-on-fail-from-default", + defaults: HelmSpec{ + CleanupOnFail: true, + }, + release: &ReleaseSpec{ + Chart: "test/chart", + Version: "0.1", + Name: "test-charts", + Namespace: "test-namespace", + }, + want: []string{ + "--version", "0.1", + "--cleanup-on-fail", + "--namespace", "test-namespace", + }, + }, { name: "tiller", defaults: HelmSpec{},