From e7959cf9904ef92559ae9077250c9857d6a519df Mon Sep 17 00:00:00 2001 From: Cameron Braid Date: Fri, 22 Jan 2021 12:47:39 +1100 Subject: [PATCH] fix: configure helmfile promotion per env --- docs/config.md | 6 +- go.sum | 1 + pkg/apis/promote/v1alpha1/types.go | 4 - pkg/promoteconfig/helpers.go | 2 + pkg/rules/factory/factory_test.go | 3 + .../.jx/promote.yaml | 6 -- .../helmfile.yaml | 8 -- .../helmfile.yaml.1.expected | 17 ----- .../helmfile.yaml.2.expected | 21 ------ .../.jx/promote.yaml | 7 -- .../helmfile.yaml | 8 -- .../helmfile.yaml.1.expected | 17 ----- .../helmfile.yaml.2.expected | 21 ------ .../.jx/promote.yaml | 6 -- .../helmfiles/jx/helmfile.yaml.2.expected | 14 ---- .../.jx/promote.yaml | 7 -- .../helmfile.yaml | 7 -- .../helmfile.yaml.expected | 6 -- .../helmfiles/jx/helmfile.yaml.1.expected | 11 --- .../helmfiles/nginx/helmfile.yaml | 21 ------ .../helmfile.yaml | 0 .../helmfile.yaml.expected | 0 .../helmfiles/jx/helmfile.yaml.1.expected | 2 +- .../helmfiles/jx/helmfile.yaml.2.expected | 4 +- .../helmfiles/jx/promote.yaml | 5 ++ .../helmfiles/nginx/helmfile.yaml | 0 pkg/rules/helmfile/helmfile_rule.go | 75 +++++++++++++++---- 27 files changed, 78 insertions(+), 201 deletions(-) delete mode 100644 pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/.jx/promote.yaml delete mode 100644 pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml delete mode 100644 pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml.1.expected delete mode 100644 pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml.2.expected delete mode 100644 pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/.jx/promote.yaml delete mode 100644 pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml delete mode 100644 pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml.1.expected delete mode 100644 pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml.2.expected delete mode 100644 pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/.jx/promote.yaml delete mode 100644 pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfiles/jx/helmfile.yaml.2.expected delete mode 100644 pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/.jx/promote.yaml delete mode 100644 pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfile.yaml delete mode 100644 pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfile.yaml.expected delete mode 100644 pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfiles/jx/helmfile.yaml.1.expected delete mode 100644 pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfiles/nginx/helmfile.yaml rename pkg/rules/factory/test_data/{helmfile-nested-explicit-keep-old-releases => helmfile-per-env-keep-old-version}/helmfile.yaml (100%) rename pkg/rules/factory/test_data/{helmfile-nested-explicit-keep-old-releases => helmfile-per-env-keep-old-version}/helmfile.yaml.expected (100%) rename pkg/rules/factory/test_data/{helmfile-nested-explicit-keep-old-releases => helmfile-per-env-keep-old-version}/helmfiles/jx/helmfile.yaml.1.expected (90%) rename pkg/rules/factory/test_data/{helmfile-nested-explicit-keep-old-versions => helmfile-per-env-keep-old-version}/helmfiles/jx/helmfile.yaml.2.expected (84%) create mode 100644 pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/jx/promote.yaml rename pkg/rules/factory/test_data/{helmfile-nested-explicit-keep-old-releases => helmfile-per-env-keep-old-version}/helmfiles/nginx/helmfile.yaml (100%) diff --git a/docs/config.md b/docs/config.md index 92131b3..6db51b3 100644 --- a/docs/config.md +++ b/docs/config.md @@ -286,13 +286,13 @@ same cluster using the same git repository URL as the dev environment

-keepOldReleases
+KeepOldVersions
-bool +[]string -

KeepOldReleases if specified will cause the old releases to be retailed in the helfile

+

KeepOldVersions if specified will cause the named repo/release releases to be retailed in the helmfile

diff --git a/go.sum b/go.sum index b272975..d8acdcd 100644 --- a/go.sum +++ b/go.sum @@ -761,6 +761,7 @@ github.com/jenkins-x/jx-api/v4 v4.0.23 h1:GpgytvBXbHVGaeqsRLGl9f1v8vpxMsTWlZNPyh github.com/jenkins-x/jx-api/v4 v4.0.23/go.mod h1:IC88X+24Nmexuj7lRNRgzfrchzlo0qysfwkICpqDmMQ= github.com/jenkins-x/jx-gitops v0.0.530 h1:qIPmrZrg8McYFEMYn0cNfqVKYr78gAxN1LAeFxZdUDk= github.com/jenkins-x/jx-gitops v0.0.530/go.mod h1:GONXzTOpVG0e51/clom1j8MdOWHBhh67NvboIzfbANI= +github.com/jenkins-x/jx-gitops v0.0.531 h1:0UEuvAf40TBmA3lQOWJ4iFUP8re/qygs17Pgpc69+yM= github.com/jenkins-x/jx-gitops v0.0.531/go.mod h1:GONXzTOpVG0e51/clom1j8MdOWHBhh67NvboIzfbANI= github.com/jenkins-x/jx-helpers/v3 v3.0.64 h1:2tKXtb3B20hZzB+pnhptU/Zb5HzFm2G4/CJLZLZRnTE= github.com/jenkins-x/jx-helpers/v3 v3.0.64/go.mod h1:J3nlpCzSkrVayft16okG3bX9BtWdC7sFWBseKUyC+F8= diff --git a/pkg/apis/promote/v1alpha1/types.go b/pkg/apis/promote/v1alpha1/types.go index ec5b601..0440c6a 100644 --- a/pkg/apis/promote/v1alpha1/types.go +++ b/pkg/apis/promote/v1alpha1/types.go @@ -53,10 +53,6 @@ type HelmfileRule struct { // same cluster using the same git repository URL as the dev environment Namespace string `json:"namespace"` - // KeepOldReleases if specified will cause the old releases to be retailed in the helfile - // Deprecated : use KeepOldVersions - KeepOldReleases bool `json:"keepOldReleases"` - // KeepOldVersions if specified is a list of release names and if the release name is in this list then the old versions are kept KeepOldVersions []string `json:"keepOldVersions"` } diff --git a/pkg/promoteconfig/helpers.go b/pkg/promoteconfig/helpers.go index b8cb15e..f6190c3 100644 --- a/pkg/promoteconfig/helpers.go +++ b/pkg/promoteconfig/helpers.go @@ -48,6 +48,7 @@ func Discover(dir, promoteNamespace string) (*v1alpha1.Promote, string, error) { if err != nil { return nil, "", errors.Wrapf(err, "failed to find helmfile") } + config = &v1alpha1.Promote{ ObjectMeta: metav1.ObjectMeta{ Name: "generated", @@ -59,6 +60,7 @@ func Discover(dir, promoteNamespace string) (*v1alpha1.Promote, string, error) { }, }, } + return config, "", nil } diff --git a/pkg/rules/factory/factory_test.go b/pkg/rules/factory/factory_test.go index 9babb49..27e9d2d 100644 --- a/pkg/rules/factory/factory_test.go +++ b/pkg/rules/factory/factory_test.go @@ -35,6 +35,9 @@ func TestRuleFactory(t *testing.T) { if name == "jenkins-x-versions" { continue } + if name != "helmfile-per-env-keep-old-version" { + continue + } dir := filepath.Join(tmpDir, name) diff --git a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/.jx/promote.yaml b/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/.jx/promote.yaml deleted file mode 100644 index 8a77d3e..0000000 --- a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/.jx/promote.yaml +++ /dev/null @@ -1,6 +0,0 @@ -apiVersion: promote.jenkins-x.io/v1alpha1 -kind: Promote -spec: - helmfileRule: - path: helmfile.yaml - keepOldReleases: true \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml b/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml deleted file mode 100644 index 1ecd7d6..0000000 --- a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml +++ /dev/null @@ -1,8 +0,0 @@ -repositories: -- name: yourorg - url: https://yourorg.example.com/charts -releases: -- name: dbmigrator - labels: - job: dbmigrator - chart: ./dbmigrator diff --git a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml.1.expected b/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml.1.expected deleted file mode 100644 index 315b8a5..0000000 --- a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml.1.expected +++ /dev/null @@ -1,17 +0,0 @@ -filepath: "" -repositories: -- name: yourorg - url: https://yourorg.example.com/charts -- name: dev - url: http://chartmuseum-jx.34.78.195.22.nip.io -releases: -- chart: ./dbmigrator - name: dbmigrator - labels: - job: dbmigrator -- chart: dev/myapp - version: 1.2.3 - name: myapp-1-2-3 - namespace: jx -templates: {} -renderedvalues: {} \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml.2.expected b/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml.2.expected deleted file mode 100644 index 19ec75b..0000000 --- a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-releases/helmfile.yaml.2.expected +++ /dev/null @@ -1,21 +0,0 @@ -filepath: "" -repositories: -- name: yourorg - url: https://yourorg.example.com/charts -- name: dev - url: http://chartmuseum-jx.34.78.195.22.nip.io -releases: -- chart: ./dbmigrator - name: dbmigrator - labels: - job: dbmigrator -- chart: dev/myapp - version: 1.2.3 - name: myapp-1-2-3 - namespace: jx -- chart: dev/myapp - version: 1.2.4 - name: myapp-1-2-4 - namespace: jx -templates: {} -renderedvalues: {} \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/.jx/promote.yaml b/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/.jx/promote.yaml deleted file mode 100644 index f0192f2..0000000 --- a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/.jx/promote.yaml +++ /dev/null @@ -1,7 +0,0 @@ -apiVersion: promote.jenkins-x.io/v1alpha1 -kind: Promote -spec: - helmfileRule: - path: helmfile.yaml - keepOldVersions: - - dev/myapp \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml b/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml deleted file mode 100644 index 1ecd7d6..0000000 --- a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml +++ /dev/null @@ -1,8 +0,0 @@ -repositories: -- name: yourorg - url: https://yourorg.example.com/charts -releases: -- name: dbmigrator - labels: - job: dbmigrator - chart: ./dbmigrator diff --git a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml.1.expected b/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml.1.expected deleted file mode 100644 index 315b8a5..0000000 --- a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml.1.expected +++ /dev/null @@ -1,17 +0,0 @@ -filepath: "" -repositories: -- name: yourorg - url: https://yourorg.example.com/charts -- name: dev - url: http://chartmuseum-jx.34.78.195.22.nip.io -releases: -- chart: ./dbmigrator - name: dbmigrator - labels: - job: dbmigrator -- chart: dev/myapp - version: 1.2.3 - name: myapp-1-2-3 - namespace: jx -templates: {} -renderedvalues: {} \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml.2.expected b/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml.2.expected deleted file mode 100644 index 19ec75b..0000000 --- a/pkg/rules/factory/test_data/helmfile-explicit-keep-old-versions/helmfile.yaml.2.expected +++ /dev/null @@ -1,21 +0,0 @@ -filepath: "" -repositories: -- name: yourorg - url: https://yourorg.example.com/charts -- name: dev - url: http://chartmuseum-jx.34.78.195.22.nip.io -releases: -- chart: ./dbmigrator - name: dbmigrator - labels: - job: dbmigrator -- chart: dev/myapp - version: 1.2.3 - name: myapp-1-2-3 - namespace: jx -- chart: dev/myapp - version: 1.2.4 - name: myapp-1-2-4 - namespace: jx -templates: {} -renderedvalues: {} \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/.jx/promote.yaml b/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/.jx/promote.yaml deleted file mode 100644 index 1133e62..0000000 --- a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/.jx/promote.yaml +++ /dev/null @@ -1,6 +0,0 @@ -apiVersion: promote.jenkins-x.io/v1alpha1 -kind: Promote -spec: - helmfileRule: - path: helmfiles/jx/helmfile.yaml - keepOldReleases: true \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfiles/jx/helmfile.yaml.2.expected b/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfiles/jx/helmfile.yaml.2.expected deleted file mode 100644 index 1863be8..0000000 --- a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfiles/jx/helmfile.yaml.2.expected +++ /dev/null @@ -1,14 +0,0 @@ -filepath: "" -namespace: jx -repositories: -- name: dev - url: http://chartmuseum-jx.34.78.195.22.nip.io -releases: -- chart: dev/myapp - version: 1.2.3 - name: myapp-1-2-3 -- chart: dev/myapp - version: 1.2.4 - name: myapp-1-2-4 -templates: {} -renderedvalues: {} \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/.jx/promote.yaml b/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/.jx/promote.yaml deleted file mode 100644 index 1c65c55..0000000 --- a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/.jx/promote.yaml +++ /dev/null @@ -1,7 +0,0 @@ -apiVersion: promote.jenkins-x.io/v1alpha1 -kind: Promote -spec: - helmfileRule: - path: helmfiles/jx/helmfile.yaml - keepOldVersions: - - dev/myapp \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfile.yaml b/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfile.yaml deleted file mode 100644 index 0451eec..0000000 --- a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfile.yaml +++ /dev/null @@ -1,7 +0,0 @@ -filepath: "" -helmfiles: -- path: helmfiles/nginx/helmfile.yaml - environment: {} -templates: {} -missingFileHandler: "" -renderedvalues: {} diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfile.yaml.expected b/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfile.yaml.expected deleted file mode 100644 index 4334299..0000000 --- a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfile.yaml.expected +++ /dev/null @@ -1,6 +0,0 @@ -filepath: "" -helmfiles: -- path: helmfiles/nginx/helmfile.yaml -- path: helmfiles/jx/helmfile.yaml -templates: {} -renderedvalues: {} \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfiles/jx/helmfile.yaml.1.expected b/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfiles/jx/helmfile.yaml.1.expected deleted file mode 100644 index d84ba2b..0000000 --- a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfiles/jx/helmfile.yaml.1.expected +++ /dev/null @@ -1,11 +0,0 @@ -filepath: "" -namespace: jx -repositories: -- name: dev - url: http://chartmuseum-jx.34.78.195.22.nip.io -releases: -- chart: dev/myapp - version: 1.2.3 - name: myapp-1-2-3 -templates: {} -renderedvalues: {} \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfiles/nginx/helmfile.yaml b/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfiles/nginx/helmfile.yaml deleted file mode 100644 index b2ef2a1..0000000 --- a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfiles/nginx/helmfile.yaml +++ /dev/null @@ -1,21 +0,0 @@ -filepath: "" -environments: - default: - values: - - ../../jx-values.yaml - - ../../versionStream/src/fake-secrets.yaml.gotmpl -namespace: nginx -repositories: -- name: stable - url: https://charts.helm.sh/stable -releases: -- chart: stable/nginx-ingress - version: 1.39.1 - name: nginx-ingress - values: - - ../../versionStream/charts/stable/nginx-ingress/values.yaml.gotmpl - forceNamespace: "" - skipDeps: null -templates: {} -missingFileHandler: "" -renderedvalues: {} diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfile.yaml b/pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfile.yaml similarity index 100% rename from pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfile.yaml rename to pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfile.yaml diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfile.yaml.expected b/pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfile.yaml.expected similarity index 100% rename from pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfile.yaml.expected rename to pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfile.yaml.expected diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfiles/jx/helmfile.yaml.1.expected b/pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/jx/helmfile.yaml.1.expected similarity index 90% rename from pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfiles/jx/helmfile.yaml.1.expected rename to pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/jx/helmfile.yaml.1.expected index d84ba2b..6eb5142 100644 --- a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfiles/jx/helmfile.yaml.1.expected +++ b/pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/jx/helmfile.yaml.1.expected @@ -6,6 +6,6 @@ repositories: releases: - chart: dev/myapp version: 1.2.3 - name: myapp-1-2-3 + name: myapp-1.2.3 templates: {} renderedvalues: {} \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfiles/jx/helmfile.yaml.2.expected b/pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/jx/helmfile.yaml.2.expected similarity index 84% rename from pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfiles/jx/helmfile.yaml.2.expected rename to pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/jx/helmfile.yaml.2.expected index 1863be8..9f5b5cb 100644 --- a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-versions/helmfiles/jx/helmfile.yaml.2.expected +++ b/pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/jx/helmfile.yaml.2.expected @@ -6,9 +6,9 @@ repositories: releases: - chart: dev/myapp version: 1.2.3 - name: myapp-1-2-3 + name: myapp-1.2.3 - chart: dev/myapp version: 1.2.4 - name: myapp-1-2-4 + name: myapp-1.2.4 templates: {} renderedvalues: {} \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/jx/promote.yaml b/pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/jx/promote.yaml new file mode 100644 index 0000000..1a2ce6f --- /dev/null +++ b/pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/jx/promote.yaml @@ -0,0 +1,5 @@ +apiVersion: promote.jenkins-x.io/v1alpha1 +kind: HelmfilePromote +spec: + keepOldVersions: + - dev/myapp \ No newline at end of file diff --git a/pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfiles/nginx/helmfile.yaml b/pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/nginx/helmfile.yaml similarity index 100% rename from pkg/rules/factory/test_data/helmfile-nested-explicit-keep-old-releases/helmfiles/nginx/helmfile.yaml rename to pkg/rules/factory/test_data/helmfile-per-env-keep-old-version/helmfiles/nginx/helmfile.yaml diff --git a/pkg/rules/helmfile/helmfile_rule.go b/pkg/rules/helmfile/helmfile_rule.go index 974df35..388d831 100644 --- a/pkg/rules/helmfile/helmfile_rule.go +++ b/pkg/rules/helmfile/helmfile_rule.go @@ -2,9 +2,9 @@ package helmfile import ( "fmt" + "io/ioutil" "os" "path/filepath" - "strings" "github.com/jenkins-x/jx-helpers/v3/pkg/files" "github.com/jenkins-x/jx-helpers/v3/pkg/yaml2s" @@ -13,9 +13,11 @@ import ( "github.com/jenkins-x/jx-promote/pkg/rules" "github.com/pkg/errors" "github.com/roboll/helmfile/pkg/state" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + kyaml "sigs.k8s.io/yaml" ) -// HelmfileRule uses a jx-apps.yml file +// Rule uses a helmfile.yaml file func Rule(r *rules.PromoteRule) error { config := r.Config if config.Spec.HelmfileRule == nil { @@ -26,7 +28,21 @@ func Rule(r *rules.PromoteRule) error { rule.Path = "helmfile.yaml" } - err := modifyHelmfile(r, rule, filepath.Join(r.Dir, rule.Path), rule.Namespace) + envHelmfileRulePath := filepath.Join(r.Dir, "helmfiles", rule.Namespace, "promote.yaml") + exists, err := files.FileExists(envHelmfileRulePath) + if err != nil { + return errors.Wrapf(err, "failed to check if file exists %s", envHelmfileRulePath) + } + if exists { + // environment specific HelmfileRule + envHemlfileRule, err := LoadHelmfilePromote(envHelmfileRulePath) + if err != nil { + return errors.Wrapf(err, "failed load %s", envHelmfileRulePath) + } + config.Spec.HelmfileRule.KeepOldVersions = envHemlfileRule.Spec.KeepOldVersions + } + + err = modifyHelmfile(r, rule, filepath.Join(r.Dir, rule.Path), rule.Namespace) if err != nil { return errors.Wrapf(err, "failed to modify chart files in dir %s", r.Dir) } @@ -119,16 +135,20 @@ func modifyHelmfileApps(r *rules.PromoteRule, helmfile *state.HelmState, promote isRemoteEnv := r.DevEnvContext.DevEnv.Spec.RemoteCluster - keepOldReleases := r.Config.Spec.HelmfileRule.KeepOldReleases || contains(r.Config.Spec.HelmfileRule.KeepOldVersions, details.Name) + keepOldVersions := contains(r.Config.Spec.HelmfileRule.KeepOldVersions, details.Name) if nestedHelmfile { - // for nested helmfiles we assume we don't need to specify a namespace on each chart - // as all the charts will use the same namespace - if promoteNs != "" && helmfile.OverrideNamespace == "" { - helmfile.OverrideNamespace = promoteNs + if len(helmfile.Releases) == 0 { + // for nested helmfiles when adding the first release, set it up as the override + // then when future releases are added they can omit the namespace if their namespace matches this override + // if different namespaces are required for releases, manual edits should be done to + // set the namespace of EVERY release and make OverrideNamespace blank + if promoteNs != "" && helmfile.OverrideNamespace == "" { + helmfile.OverrideNamespace = promoteNs + } } found := false - if !keepOldReleases { + if !keepOldVersions { for i := range helmfile.Releases { release := &helmfile.Releases[i] if release.Name == app || release.Name == details.Name { @@ -144,8 +164,8 @@ func modifyHelmfileApps(r *rules.PromoteRule, helmfile *state.HelmState, promote ns = promoteNs } newReleaseName := details.LocalName - if keepOldReleases { - newReleaseName = fmt.Sprintf("%s-%s", details.LocalName, strings.Replace(version, ".", "-", -1)) + if keepOldVersions { + newReleaseName = fmt.Sprintf("%s-%s", details.LocalName, version) } helmfile.Releases = append(helmfile.Releases, state.ReleaseSpec{ Name: newReleaseName, @@ -157,7 +177,7 @@ func modifyHelmfileApps(r *rules.PromoteRule, helmfile *state.HelmState, promote return nil } found := false - if !keepOldReleases { + if !keepOldVersions { for i := range helmfile.Releases { release := &helmfile.Releases[i] if (release.Name == app || release.Name == details.Name) && (release.Namespace == promoteNs || isRemoteEnv) { @@ -170,8 +190,8 @@ func modifyHelmfileApps(r *rules.PromoteRule, helmfile *state.HelmState, promote if !found { newReleaseName := details.LocalName - if keepOldReleases { - newReleaseName = fmt.Sprintf("%s-%s", details.LocalName, strings.Replace(version, ".", "-", -1)) + if keepOldVersions { + newReleaseName = fmt.Sprintf("%s-%s", details.LocalName, version) } helmfile.Releases = append(helmfile.Releases, state.ReleaseSpec{ Name: newReleaseName, @@ -238,3 +258,30 @@ func contains(arr []string, str string) bool { } return false } + +// HelmfilePromote is for configuring promotion for an environment +type HelmfilePromote struct { + metav1.TypeMeta `json:",inline"` + + Spec HelmfilePromoteSpec `json:"spec"` +} + +// HelmfilePromoteSpec defines the configuration for an environment +type HelmfilePromoteSpec struct { + // keepOldVersions if specified is a list of release names and if the release name is in this list then the old versions are kept + KeepOldVersions []string `json:"keepOldVersions,omitempty"` +} + +// LoadHelmfilePromote loads a HelmfilePromote from a specific YAML file +func LoadHelmfilePromote(fileName string) (*HelmfilePromote, error) { + data, err := ioutil.ReadFile(fileName) + if err != nil { + return nil, errors.Wrapf(err, "failed to read file %s", fileName) + } + config := &HelmfilePromote{} + err = kyaml.Unmarshal(data, config) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal YAML file %s due to %s", fileName, err) + } + return config, nil +}