Skip to content

Commit

Permalink
Merge pull request #238 from cameronbraid/keepOldVersions
Browse files Browse the repository at this point in the history
fix: when keeping old versions include version name in release, move to new field to target specific repo/chart
  • Loading branch information
jstrachan authored Jan 19, 2021
2 parents a85c655 + 87f5d61 commit 111546c
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 10 deletions.
5 changes: 5 additions & 0 deletions pkg/apis/promote/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ type HelmRule struct {
type HelmfileRule struct {
// Path to the helmfile to modify
Path string `json:"path"`

// Namespace if specified the given namespace is used in the `helmfile.yml` file when using Environments in the
// 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"`
}

// KptRule specifies to fetch the apps resource via kpt : https://googlecontainertools.github.io/kpt/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ releases:
job: dbmigrator
- chart: dev/myapp
version: 1.2.3
name: myapp
name: myapp-1-2-3
namespace: jx
templates: {}
renderedvalues: {}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ releases:
job: dbmigrator
- chart: dev/myapp
version: 1.2.3
name: myapp
name: myapp-1-2-3
namespace: jx
- chart: dev/myapp
version: 1.2.4
name: myapp
name: myapp-1-2-4
namespace: jx
templates: {}
renderedvalues: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: promote.jenkins-x.io/v1alpha1
kind: Promote
spec:
helmfileRule:
path: helmfile.yaml
keepOldVersions:
- dev/myapp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
repositories:
- name: yourorg
url: https://yourorg.example.com/charts
releases:
- name: dbmigrator
labels:
job: dbmigrator
chart: ./dbmigrator
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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: {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ repositories:
releases:
- chart: dev/myapp
version: 1.2.3
name: myapp
name: myapp-1-2-3
templates: {}
renderedvalues: {}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ repositories:
releases:
- chart: dev/myapp
version: 1.2.3
name: myapp
name: myapp-1-2-3
- chart: dev/myapp
version: 1.2.4
name: myapp
name: myapp-1-2-4
templates: {}
renderedvalues: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: promote.jenkins-x.io/v1alpha1
kind: Promote
spec:
helmfileRule:
path: helmfiles/jx/helmfile.yaml
keepOldVersions:
- dev/myapp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
filepath: ""
helmfiles:
- path: helmfiles/nginx/helmfile.yaml
environment: {}
templates: {}
missingFileHandler: ""
renderedvalues: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
filepath: ""
helmfiles:
- path: helmfiles/nginx/helmfile.yaml
- path: helmfiles/jx/helmfile.yaml
templates: {}
renderedvalues: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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: {}
28 changes: 24 additions & 4 deletions pkg/rules/helmfile/helmfile_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/yaml2s"
Expand Down Expand Up @@ -118,14 +119,16 @@ 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)

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
}
found := false
if !r.Config.Spec.HelmfileRule.KeepOldReleases {
if !keepOldReleases {
for i := range helmfile.Releases {
release := &helmfile.Releases[i]
if release.Name == app || release.Name == details.Name {
Expand All @@ -140,8 +143,12 @@ func modifyHelmfileApps(r *rules.PromoteRule, helmfile *state.HelmState, promote
if promoteNs != helmfile.OverrideNamespace {
ns = promoteNs
}
newReleaseName := details.LocalName
if keepOldReleases {
newReleaseName = fmt.Sprintf("%s-%s", details.LocalName, strings.Replace(version, ".", "-", -1))
}
helmfile.Releases = append(helmfile.Releases, state.ReleaseSpec{
Name: details.LocalName,
Name: newReleaseName,
Chart: details.Name,
Namespace: ns,
Version: version,
Expand All @@ -150,7 +157,7 @@ func modifyHelmfileApps(r *rules.PromoteRule, helmfile *state.HelmState, promote
return nil
}
found := false
if !r.Config.Spec.HelmfileRule.KeepOldReleases {
if !keepOldReleases {
for i := range helmfile.Releases {
release := &helmfile.Releases[i]
if (release.Name == app || release.Name == details.Name) && (release.Namespace == promoteNs || isRemoteEnv) {
Expand All @@ -162,8 +169,12 @@ 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))
}
helmfile.Releases = append(helmfile.Releases, state.ReleaseSpec{
Name: details.LocalName,
Name: newReleaseName,
Chart: details.Name,
Version: version,
Namespace: promoteNs,
Expand Down Expand Up @@ -218,3 +229,12 @@ func defaultPrefix(appsConfig *state.HelmState, d *envctx.ChartDetails, defaultP
}
d.SetPrefix(prefix)
}

func contains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}

0 comments on commit 111546c

Please sign in to comment.