Add the cleanup-on-fail flag to upgrade command (#969)

* Adding the cleanup-on-fail flag to upgrade command

* Adding tests and making the flag optional

* Updating readme with new flag details

* go fmt
This commit is contained in:
Harshal Shah 2019-11-29 00:47:19 +01:00 committed by KUOKA Yusuke
parent 25599eae89
commit 04379cee80
3 changed files with 63 additions and 0 deletions

View File

@ -74,6 +74,7 @@ helmDefaults:
tillerNamespace: tiller-namespace #dedicated default key for tiller-namespace tillerNamespace: tiller-namespace #dedicated default key for tiller-namespace
tillerless: false #dedicated default key for tillerless tillerless: false #dedicated default key for tillerless
kubeContext: kube-context #dedicated default key for kube-context (--kube-context) 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 # additional and global args passed to helm
args: args:
- "--set k=v" - "--set k=v"
@ -157,6 +158,8 @@ releases:
installed: true installed: true
# restores previous state in case of failed release # restores previous state in case of failed release
atomic: true atomic: true
# when true, cleans up any new resources created during a failed release
cleanup-on-fail: false
# name of the tiller namespace # name of the tiller namespace
tillerNamespace: vault tillerNamespace: vault
# if true, will use the helm-tiller plugin # if true, will use the helm-tiller plugin

View File

@ -101,6 +101,8 @@ type HelmSpec struct {
Force bool `yaml:"force"` Force bool `yaml:"force"`
// Atomic, when set to true, restore previous state in case of a failed install/upgrade attempt // Atomic, when set to true, restore previous state in case of a failed install/upgrade attempt
Atomic bool `yaml:"atomic"` 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"` TLS bool `yaml:"tls"`
TLSCACert string `yaml:"tlsCACert,omitempty"` TLSCACert string `yaml:"tlsCACert,omitempty"`
@ -139,6 +141,8 @@ type ReleaseSpec struct {
Installed *bool `yaml:"installed,omitempty"` Installed *bool `yaml:"installed,omitempty"`
// Atomic, when set to true, restore previous state in case of a failed install/upgrade attempt // Atomic, when set to true, restore previous state in case of a failed install/upgrade attempt
Atomic *bool `yaml:"atomic,omitempty"` 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. // 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". // The default value for MissingFileHandler is "Error".
@ -1546,6 +1550,10 @@ func (st *HelmState) flagsForUpgrade(helm helmexec.Interface, release *ReleaseSp
flags = append(flags, "--atomic") 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) flags = st.appendConnectionFlags(flags, release)
var err error var err error

View File

@ -448,6 +448,58 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
"--namespace", "test-namespace", "--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", name: "tiller",
defaults: HelmSpec{}, defaults: HelmSpec{},