Support for createNamespace (#1226)

- createNamespace is a new attribute that can be added to helmDefaults
  or an individual release to enforce the creation of a release namespace
  during sync if the namespace does not exist. This leverages helm's
  (3.2+) --create-namespace flag for the install/upgrade command. If
  running helm < 3.2, the createNamespace attribute has no effect.

Resolves #891
Resolves #1140
This commit is contained in:
Craig Dunford
2020-04-26 10:41:40 +09:00
committed by GitHub
parent b1190508b2
commit eeb61e6174
9 changed files with 270 additions and 26 deletions
+10
View File
@@ -114,6 +114,8 @@ type HelmSpec struct {
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"`
// CreateNamespace, when set to true (default), --create-namespace is passed to helm3 on install/upgrade (ignored for helm2)
CreateNamespace *bool `yaml:"createNamespace,omitempty"`
TLS bool `yaml:"tls"`
TLSCACert string `yaml:"tlsCACert,omitempty"`
@@ -158,6 +160,8 @@ type ReleaseSpec struct {
HistoryMax *int `yaml:"historyMax,omitempty"`
// Condition, when set, evaluate the mapping specified in this string to a boolean which decides whether or not to process the release
Condition string `yaml:"condition,omitempty"`
// CreateNamespace, when set to true (default), --create-namespace is passed to helm3 on install (ignored for helm2)
CreateNamespace *bool `yaml:"createNamespace,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".
@@ -1635,6 +1639,12 @@ func (st *HelmState) flagsForUpgrade(helm helmexec.Interface, release *ReleaseSp
flags = append(flags, "--cleanup-on-fail")
}
if helm.IsVersionAtLeast(3, 2) &&
(release.CreateNamespace != nil && *release.CreateNamespace ||
release.CreateNamespace == nil && (st.HelmDefaults.CreateNamespace == nil || *st.HelmDefaults.CreateNamespace)) {
flags = append(flags, "--create-namespace")
}
flags = st.appendConnectionFlags(flags, release)
var err error
+101 -2
View File
@@ -163,6 +163,7 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
tests := []struct {
name string
version *helmexec.Version
defaults HelmSpec
release *ReleaseSpec
want []string
@@ -573,6 +574,101 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
"--tls-ca-cert", "ca.pem",
},
},
{
name: "create-namespace-default-helm3.2",
defaults: HelmSpec{
Verify: false,
},
version: &helmexec.Version{
Major: 3,
Minor: 2,
Patch: 0,
},
release: &ReleaseSpec{
Chart: "test/chart",
Version: "0.1",
Verify: &disable,
Name: "test-charts",
Namespace: "test-namespace",
},
want: []string{
"--version", "0.1",
"--create-namespace",
"--namespace", "test-namespace",
},
},
{
name: "create-namespace-disabled-helm3.2",
defaults: HelmSpec{
Verify: false,
CreateNamespace: &disable,
},
version: &helmexec.Version{
Major: 3,
Minor: 2,
Patch: 0,
},
release: &ReleaseSpec{
Chart: "test/chart",
Version: "0.1",
Verify: &disable,
Name: "test-charts",
Namespace: "test-namespace",
},
want: []string{
"--version", "0.1",
"--namespace", "test-namespace",
},
},
{
name: "create-namespace-release-override-enabled-helm3.2",
defaults: HelmSpec{
Verify: false,
CreateNamespace: &disable,
},
version: &helmexec.Version{
Major: 3,
Minor: 2,
Patch: 0,
},
release: &ReleaseSpec{
Chart: "test/chart",
Version: "0.1",
Verify: &disable,
Name: "test-charts",
Namespace: "test-namespace",
CreateNamespace: &enable,
},
want: []string{
"--version", "0.1",
"--create-namespace",
"--namespace", "test-namespace",
},
},
{
name: "create-namespace-release-override-disabled-helm3.2",
defaults: HelmSpec{
Verify: false,
CreateNamespace: &enable,
},
version: &helmexec.Version{
Major: 3,
Minor: 2,
Patch: 0,
},
release: &ReleaseSpec{
Chart: "test/chart",
Version: "0.1",
Verify: &disable,
Name: "test-charts",
Namespace: "test-namespace",
CreateNamespace: &disable,
},
want: []string{
"--version", "0.1",
"--namespace", "test-namespace",
},
},
}
for i := range tests {
tt := tests[i]
@@ -584,10 +680,13 @@ func TestHelmState_flagsForUpgrade(t *testing.T) {
HelmDefaults: tt.defaults,
valsRuntime: valsRuntime,
}
helm := helmexec.New("helm", logger, "default", &mockRunner{})
helm := &exectest.Helm{
Version: tt.version,
}
args, err := state.flagsForUpgrade(helm, tt.release, 0)
if err != nil {
t.Errorf("unexpected error flagsForUpgade: %v", err)
t.Errorf("unexpected error flagsForUpgrade: %v", err)
}
if !reflect.DeepEqual(args, tt.want) {
t.Errorf("flagsForUpgrade returned = %v, want %v", args, tt.want)