From 77af716ab244172462a16cb676d4adf4aead3455 Mon Sep 17 00:00:00 2001 From: Alejandro Pedraza Date: Tue, 18 Feb 2020 11:19:58 -0500 Subject: [PATCH] bin/helm-build automatically updates version in values.yaml (#4058) * bin/helm-build automatically updates version in values.yaml Have the Helm charts building script (`bin/helm-build`) update the linkerd version in the `values.yaml` files according to the tagged version, thus removing the need of doing this manually on every release. This is akin to the update we do in `version.go` at CLI build time. Note that `shellcheck` is issuing some warnings about this script, but that's on code that was already there, so that will be handled in an followup PR. --- BUILD.md | 4 ++++ bin/helm-build | 20 +++++++++++++++++++- charts/linkerd2-cni/values.yaml | 2 +- charts/linkerd2/values.yaml | 2 +- pkg/charts/charts.go | 11 +++++++++++ pkg/charts/cni/values.go | 2 +- pkg/charts/linkerd2/values.go | 2 +- 7 files changed, 38 insertions(+), 5 deletions(-) diff --git a/BUILD.md b/BUILD.md index d76d3ed8d..b5c453952 100644 --- a/BUILD.md +++ b/BUILD.md @@ -394,6 +394,10 @@ chart consists of the Linkerd proxy specification, which is used by the proxy injector to inject the proxy container. Both charts depend on the partials subchart which can be found in the [`charts/partials`](charts/partials) folder. +Note that the `charts/linkerd2/values.yaml` file contains a placeholder +`{version}` that you need to replace with an appropriate string (like +`edge-20.2.2`) before proceeding. + During development, please use the [`bin/helm`](bin/helm) wrapper script to invoke the Helm commands. For example, diff --git a/bin/helm-build b/bin/helm-build index 401895781..e5679f1c7 100755 --- a/bin/helm-build +++ b/bin/helm-build @@ -2,8 +2,18 @@ set -e +setValues() { + sed -i "s/$1/$2/" charts/linkerd2/values.yaml + sed -i "s/$1/$2/" charts/linkerd2-cni/values.yaml +} + +showErr() { + printf "Error on exit:\n Exit code: %d\n Failed command: \"%s\"\n" $? "$BASH_COMMAND" + setValues $fullVersion "{version}" +} + # trap the last failed command -trap 'printf "Error on exit:\n Exit code: $?\n Failed command: \"$BASH_COMMAND\"\n"' ERR +trap 'showErr' ERR bindir=$( cd "${BASH_SOURCE[0]%/*}" && pwd ) rootdir=$( cd "$bindir"/.. && pwd ) @@ -27,10 +37,18 @@ if [ "$1" = package ]; then echo 'Version tag is malformed' exit 1 fi + fullVersion=${BASH_REMATCH[0]} repo=${BASH_REMATCH[1]} version=${BASH_REMATCH[2]} + + # set version in Values files + setValues "{version}" $fullVersion + "$bindir"/helm --version $version --app-version $tag -d "$rootdir"/target/helm package "$rootdir"/charts/linkerd2 "$bindir"/helm --version $version --app-version $tag -d "$rootdir"/target/helm package "$rootdir"/charts/linkerd2-cni mv "$rootdir"/target/helm/index-pre.yaml "$rootdir"/target/helm/index-pre-$version.yaml "$bindir"/helm repo index --url "https://helm.linkerd.io/$repo/" --merge "$rootdir"/target/helm/index-pre-$version.yaml "$rootdir"/target/helm + + # restore version in Values files + setValues $fullVersion "{version}" fi diff --git a/charts/linkerd2-cni/values.yaml b/charts/linkerd2-cni/values.yaml index adcc92b11..31ebb294d 100644 --- a/charts/linkerd2-cni/values.yaml +++ b/charts/linkerd2-cni/values.yaml @@ -6,7 +6,7 @@ ignoreInboundPorts: "" ignoreOutboundPorts: "" createdByAnnotation: linkerd.io/created-by cniPluginImage: "gcr.io/linkerd-io/cni-plugin" -cniPluginVersion: stable-2.7.0 +cniPluginVersion: {version} logLevel: info portsToRedirect: "" proxyUID: 2102 diff --git a/charts/linkerd2/values.yaml b/charts/linkerd2/values.yaml index f8b609205..a0048698f 100644 --- a/charts/linkerd2/values.yaml +++ b/charts/linkerd2/values.yaml @@ -11,7 +11,7 @@ global: controlPlaneTracing: false # control plane version. See Proxy section for proxy version - linkerdVersion: &linkerd_version stable-2.7.0 + linkerdVersion: &linkerd_version {version} namespace: linkerd diff --git a/pkg/charts/charts.go b/pkg/charts/charts.go index 8fe289f51..ef20038fd 100644 --- a/pkg/charts/charts.go +++ b/pkg/charts/charts.go @@ -3,14 +3,18 @@ package charts import ( "bytes" "path" + "strings" "github.com/linkerd/linkerd2/pkg/charts/static" + "github.com/linkerd/linkerd2/pkg/version" "k8s.io/helm/pkg/chartutil" helmChart "k8s.io/helm/pkg/proto/hapi/chart" "k8s.io/helm/pkg/renderutil" "k8s.io/helm/pkg/timeconv" ) +const versionPlaceholder = "{version}" + // Chart holds the necessary info to render a Helm chart type Chart struct { Name string @@ -122,3 +126,10 @@ func FilesReader(dir string, files []*chartutil.BufferedFile) error { } return nil } + +// InsertVersion returns the chart values file contents passed in +// with the version placeholder replaced with the current version +func InsertVersion(data []byte) []byte { + dataWithVersion := strings.Replace(string(data), versionPlaceholder, version.Version, 1) + return []byte(dataWithVersion) +} diff --git a/pkg/charts/cni/values.go b/pkg/charts/cni/values.go index 27583e52b..0d7d2400c 100644 --- a/pkg/charts/cni/values.go +++ b/pkg/charts/cni/values.go @@ -57,7 +57,7 @@ func readDefaults(chartDir string) (*Values, error) { return nil, err } values := Values{} - if err := yaml.Unmarshal(file.Data, &values); err != nil { + if err := yaml.Unmarshal(charts.InsertVersion(file.Data), &values); err != nil { return nil, err } return &values, nil diff --git a/pkg/charts/linkerd2/values.go b/pkg/charts/linkerd2/values.go index 8e5675c7a..0c4ccc08f 100644 --- a/pkg/charts/linkerd2/values.go +++ b/pkg/charts/linkerd2/values.go @@ -250,7 +250,7 @@ func readDefaults(chartDir string, ha bool) (*Values, error) { values := Values{} for _, valuesFile := range valuesFiles { var v Values - if err := yaml.Unmarshal(valuesFile.Data, &v); err != nil { + if err := yaml.Unmarshal(charts.InsertVersion(valuesFile.Data), &v); err != nil { return nil, err }