Skip to content

Commit

Permalink
Merge pull request #959 from jenkins-x-plugins/replace_chart
Browse files Browse the repository at this point in the history
feat: replace chart
  • Loading branch information
jenkins-x-bot authored May 17, 2024
2 parents 388f91e + 94a2b5e commit a5ce2c7
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 53 deletions.
8 changes: 1 addition & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/jenkins-x-plugins/jx-charter v0.0.28
github.com/jenkins-x/go-scm v1.14.30
github.com/jenkins-x/jx-api/v4 v4.7.4
github.com/jenkins-x/jx-helpers/v3 v3.4.12
github.com/jenkins-x/jx-helpers/v3 v3.7.0
github.com/jenkins-x/jx-kube-client/v3 v3.0.4
github.com/jenkins-x/jx-logging/v3 v3.0.9
github.com/jenkins-x/lighthouse-client v0.0.1109
Expand Down Expand Up @@ -168,7 +168,6 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/itchyny/gojq v0.9.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jenkins-x/gen-crd-api-reference-docs v0.1.7 // indirect
github.com/jenkins-x/logrus-stackdriver-formatter v0.2.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down Expand Up @@ -220,7 +219,6 @@ require (
github.com/rickar/props v0.0.0-20170718221555-0b06aeb2f037 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday v1.6.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
Expand All @@ -229,7 +227,6 @@ require (
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/src-d/gcfg v1.4.0 // indirect
github.com/tatsushid/go-prettytable v0.0.0-20141013043238-ed2d14c29939 // indirect
github.com/ulikunitz/xz v0.5.8 // indirect
github.com/variantdev/chartify v0.12.0 // indirect
Expand Down Expand Up @@ -271,15 +268,12 @@ require (
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/robfig/cron.v2 v2.0.0-20150107220207-be2e0b0deed5 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/src-d/go-git.v4 v4.13.1 // indirect
gopkg.in/urfave/cli.v1 v1.20.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.25.2 // indirect
k8s.io/cli-runtime v0.28.4 // indirect
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01 // indirect
k8s.io/klog v1.0.0 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
Expand Down
15 changes: 2 additions & 13 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -889,17 +889,15 @@ github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869/go.mod h1:cJ6Cj
github.com/jenkins-x-plugins/jx-charter v0.0.28 h1:Pwtcm2L8+xmtboPBIvTsc3jPLEwZuYnzDtBQurdVuv0=
github.com/jenkins-x-plugins/jx-charter v0.0.28/go.mod h1:oWhIpQIRc08qikMIH7VF08kQM/onYtU9teWq8eCSaJE=
github.com/jenkins-x/gen-crd-api-reference-docs v0.1.6/go.mod h1:a4dzSf/nNLMMMqultm6IlV/04xq26uICEYPkSTOahWQ=
github.com/jenkins-x/gen-crd-api-reference-docs v0.1.7 h1:k6+0411dvhy9ygbCcpKqoFgDwqoaathYevQwOuBtEh4=
github.com/jenkins-x/gen-crd-api-reference-docs v0.1.7/go.mod h1:1ACCcPZWVWTfq6hCJ5m1HSHswQa+T2iiAaJ8dbuTFS4=
github.com/jenkins-x/go-scm v1.10.10/go.mod h1:z7xTO9/VzqW3xEbEMH2z5cpOGrZ8+nOHOWfU1ngFGxs=
github.com/jenkins-x/go-scm v1.14.30 h1:UWhpY1906IQn4FtFo5CAuhpZtsHLnIjYlKinmDkBvXo=
github.com/jenkins-x/go-scm v1.14.30/go.mod h1:9MGEHpudIt+JypKTf4zUHpiRggl5piIb9xcLu+1FrbU=
github.com/jenkins-x/jx-api/v4 v4.1.5/go.mod h1:l11kHlFy40UGu9pdhCRxDiJcEgRubSVzybWB2jXjLcs=
github.com/jenkins-x/jx-api/v4 v4.7.4 h1:b5/UunAUkqIFx2ACTR0EsU6VRlwveOZQtRNQP4kTJWk=
github.com/jenkins-x/jx-api/v4 v4.7.4/go.mod h1:WIuWzQkBOIlEdyPIcdqGzvtJDPUmlEkpPjjyHFOFle0=
github.com/jenkins-x/jx-helpers/v3 v3.0.127/go.mod h1:0U5fcXnqSv5ugx+XMZ2rYT+VU3o+pyJeXJEiNMAVeSU=
github.com/jenkins-x/jx-helpers/v3 v3.4.12 h1:Avx1/KxyFxIhvpOr7J6Dn0WvcZmsm1i7ZFXmRd/WhZc=
github.com/jenkins-x/jx-helpers/v3 v3.4.12/go.mod h1:/2n7uMXbbLp1V9fG1fnf7ngdu3vGQFSMYnglY4oKx7c=
github.com/jenkins-x/jx-helpers/v3 v3.7.0 h1:vrcE9G2xuRZFy3R4ikoQ3gNh5erJv0QHQAABEWo/9tY=
github.com/jenkins-x/jx-helpers/v3 v3.7.0/go.mod h1:3VlqQ/FtxJo/pElOvYDFQ5lwuICC8iltpSpFy77imq0=
github.com/jenkins-x/jx-kube-client/v3 v3.0.2/go.mod h1:C/mKnCT5wvolX61eLKJVBNev9sqnkGNpi4skTQ1Gr3Q=
github.com/jenkins-x/jx-kube-client/v3 v3.0.4 h1:/MbqKke3q9/JYrXCHCyqDXiyD0ybnouK7fIfCPCJ/Ck=
github.com/jenkins-x/jx-kube-client/v3 v3.0.4/go.mod h1:suKBiBs7DKHzOEyhcsr5QhFs/Wcm0agLFMoYlIf3bPA=
Expand Down Expand Up @@ -1309,7 +1307,6 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
Expand Down Expand Up @@ -1380,7 +1377,6 @@ github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DM
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns=
github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
github.com/stoewer/go-strcase v1.2.0/go.mod h1:IBiWB2sKIp3wVVQ3Y035++gc+knqhUQag1KpM8ahLw8=
github.com/streadway/amqp v0.0.0-20190404075320-75d898a42a94/go.mod h1:AZpEONHx3DKn8O/DFsRAY58/XVQiIPMTMB1SddzLXVw=
Expand Down Expand Up @@ -2140,7 +2136,6 @@ gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI
gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/src-d/go-billy.v4 v4.3.2/go.mod h1:nDjArDMp+XMs1aFAESLRjfGSgfvoYN0hDfzEk0GjC98=
gopkg.in/src-d/go-git-fixtures.v3 v3.5.0/go.mod h1:dLBcvytrw/TYZsNTWCnkNF2DSIlzWYqTe3rJR56Ac7g=
gopkg.in/src-d/go-git.v4 v4.13.1 h1:SRtFyV8Kxc0UP7aCHcijOMQGPxHSmMOPrzulQWolkYE=
gopkg.in/src-d/go-git.v4 v4.13.1/go.mod h1:nx5NYcxdKxq5fpltdHnPa2Exj4Sx0EclMWZQbYDu2z8=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
Expand Down Expand Up @@ -2212,19 +2207,13 @@ k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8
k8s.io/gengo v0.0.0-20201113003025-83324d819ded/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/gengo v0.0.0-20201214224949-b6c5ce23f027/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/gengo v0.0.0-20230306165830-ab3349d207d4/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01 h1:pWEwq4Asjm4vjW7vcsmijwBhOr1/shsbSYiWXmNGlks=
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
k8s.io/klog v0.2.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/klog v0.3.1/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
k8s.io/klog/v2 v2.4.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
k8s.io/klog/v2 v2.8.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec=
k8s.io/klog/v2 v2.80.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/klog/v2 v2.90.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
k8s.io/klog/v2 v2.110.1 h1:U/Af64HJf7FcwMcXyKm2RPM22WZzyR7OSpYj5tg3cL0=
k8s.io/klog/v2 v2.110.1/go.mod h1:YGtd1984u+GgbuZ7e08/yBuAfKLSO0+uR1Fhi6ExXjo=
Expand Down
113 changes: 80 additions & 33 deletions pkg/cmd/helmfile/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,40 +470,9 @@ func (o *Options) resolveHelmfile(helmState *state.HelmState, helmfile helmfiles

// lets look for an override version label
if stringhelpers.StringArrayIndex(ignoreRepositories, repository) < 0 && !IsLabelValue(&release, helmhelpers.VersionLabel, helmhelpers.LockLabelValue) {
// first try and match using the prefix and release name as we might have a version stream folder that uses helm alias
versionProperties, err := o.Options.Resolver.StableVersion(versionstream.KindChart, prefix+"/"+release.Name)
err = o.updateRelease(helmState, prefix, &release, fullChartName, repository, helmfile)
if err != nil {
return errors.Wrapf(err, "failed to find version number for chart %s", release.Name)
}

// lets fall back to using the full chart name
if versionProperties.Version == "" {
versionProperties, err = o.Options.Resolver.StableVersion(versionstream.KindChart, fullChartName)
if err != nil {
return errors.Wrapf(err, "failed to find version number for chart %s", fullChartName)
}
}

version := versionProperties.Version

if release.Version == "" && version == "" {
log.Logger().Debugf("could not find version for chart %s so using latest found in helm repository %s", fullChartName, repository)
}

versionChanged := false
if release.Version == "" {
release.Version = version
versionChanged = true
} else if o.UpdateMode && release.Version != version && version != "" {
release.Version = version
versionChanged = true
}
if versionChanged {
log.Logger().Debugf("resolved chart %s version %s", fullChartName, version)
}

if release.Namespace == "" && helmState.OverrideNamespace == "" && versionProperties.Namespace != "" {
release.Namespace = versionProperties.Namespace
return err
}
}
}
Expand Down Expand Up @@ -578,6 +547,84 @@ func (o *Options) resolveHelmfile(helmState *state.HelmState, helmfile helmfiles
return nil
}

func (o *Options) updateRelease(helmState *state.HelmState, prefix string, release *state.ReleaseSpec, fullChartName, repository string, helmfile helmfiles.Helmfile) error {
// first try and match using the prefix and release name as we might have a version stream folder that uses helm alias
versionProperties, err := o.Options.Resolver.StableVersion(versionstream.KindChart, prefix+"/"+release.Name)
if err != nil {
return errors.Wrapf(err, "failed to find version number for chart %s", release.Name)
}
if versionProperties.ReplacementChart != "" && (release.Version == "" || o.UpdateMode) {
release.Name = versionProperties.ReplacementChart
if versionProperties.ReplacementChartPrefix != "" {
prefix = versionProperties.ReplacementChartPrefix
newChart := fmt.Sprintf("%s/%s", prefix, versionProperties.ReplacementChart)
// Checking that replacement chart doesn't already exist in helmfile
for i := range helmState.Releases {
existingRelease := helmState.Releases[i]
if existingRelease.Chart == newChart {
log.Logger().Warningf("Can't replace %s with %s since %s already exist in helmfile. You should probably remove %s from %s yourself.", release.Chart, newChart, newChart, release.Chart, helmfile.Filepath)
return nil
}
}
release.Chart = newChart

// let's make sure we have the repository
found := false
for k := range helmState.Repositories {
repo := helmState.Repositories[k]
if repo.Name == versionProperties.ReplacementChartPrefix {
found = true
break
}
}
if !found {
repository, err = versionstreamer.MatchRepositoryPrefix(o.prefixes, versionProperties.ReplacementChartPrefix)
if err != nil {
return err
}
helmState.Repositories = append(helmState.Repositories, state.RepositorySpec{
Name: versionProperties.ReplacementChartPrefix,
URL: repository,
})
}
}
log.Logger().Debugf("replacing chart %s with %s", fullChartName, release.Chart)
return o.updateRelease(helmState, prefix, release, release.Chart, repository, helmfile)
}

// Only falling back if the file for the alias exists, but doesn't contain version. Weird...
// lets fall back to using the full chart name
if versionProperties.Version == "" {
versionProperties, err = o.Options.Resolver.StableVersion(versionstream.KindChart, fullChartName)
if err != nil {
return errors.Wrapf(err, "failed to find version number for chart %s", fullChartName)
}
}

version := versionProperties.Version

if release.Version == "" && version == "" {
log.Logger().Debugf("could not find version for chart %s so using latest found in helm repository %s", fullChartName, repository)
}

versionChanged := false
if release.Version == "" {
release.Version = version
versionChanged = true
} else if o.UpdateMode && release.Version != version && version != "" {
release.Version = version
versionChanged = true
}
if versionChanged {
log.Logger().Debugf("resolved chart %s version %s", fullChartName, version)
}

if release.Namespace == "" && helmState.OverrideNamespace == "" && versionProperties.Namespace != "" {
release.Namespace = versionProperties.Namespace
}
return nil
}

// IsLabelValue returns true if the release is labelled with the given label with a value
func IsLabelValue(release *state.ReleaseSpec, label, value string) bool {
answer := false
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/helmfile/resolve/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func TestStepHelmfileResolve(t *testing.T) {
folder: "helmfile_multi_subfolder",
namespaces: []string{"kuberhealthy", "secret-infra"},
},
{
folder: "replace-chart",
namespaces: []string{"secret-infra"},
},
}

// lets find the helm binary on the $PATH or download a plugin if inside CI/CD
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
filepath: ""
helmfiles:
- path: helmfiles/secret-infra/helmfile.yaml
templates: {}
renderedvalues: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
filepath: ""
environments:
default:
values:
- jx-values.yaml
namespace: secret-infra
repositories:
- name: wave-k8s
url: https://wave-k8s.github.io/wave/
releases:
- chart: wave-k8s/wave
version: 3.0.0
name: wave
values:
- jx-values.yaml
templates: {}
renderedvalues: {}
7 changes: 7 additions & 0 deletions pkg/cmd/helmfile/resolve/testdata/replace-chart/helmfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
releases:
- chart: jxgh/pusher-wave
name: pusher-wave
version: 1.2.3
repositories:
- name: jxgh
url: https://jenkins-x-charts.github.io/repo
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: core.jenkins-x.io/v4beta1
kind: Requirements
spec:
autoUpdate:
enabled: false
schedule: ""
cluster:
clusterName: mycluster
project: myproject
provider: gke
ingress:
domain: ""
externalDNS: false
namespaceSubDomain: ""
vault: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
replacementChart: wave
replacementChartPrefix: wave-k8s
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
repositories:
- prefix: wave-k8s
urls:
- https://wave-k8s.github.io/wave/
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gitUrl: https://github.com/wave-k8s/wave
namespace: secret-infra
version: 3.0.0

0 comments on commit a5ce2c7

Please sign in to comment.