From 482976990de7db2ae667537fc0b5252bdd7be90e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 11 Apr 2022 09:52:03 +0800 Subject: [PATCH] Fix: reuse chart values in vela install (#3617) Signed-off-by: Jianbo Sun (cherry picked from commit 5bf0dd045f4df010ffb56da770145082c5dd5296) Co-authored-by: Jianbo Sun --- pkg/utils/helm/helm_helper.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/utils/helm/helm_helper.go b/pkg/utils/helm/helm_helper.go index 58e2e9446..2953be6b0 100644 --- a/pkg/utils/helm/helm_helper.go +++ b/pkg/utils/helm/helm_helper.go @@ -32,8 +32,10 @@ import ( "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart/loader" + "helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/kube" "helm.sh/helm/v3/pkg/release" + relutil "helm.sh/helm/v3/pkg/releaseutil" "helm.sh/helm/v3/pkg/repo" "helm.sh/helm/v3/pkg/storage" "helm.sh/helm/v3/pkg/storage/driver" @@ -118,7 +120,7 @@ func (h *Helper) UpgradeChart(ch *chart.Chart, releaseName, namespace string, va var newRelease *release.Release timeoutInMinutes := 18 releases, err := histClient.Run(releaseName) - if err != nil { + if err != nil || len(releases) == 0 { if errors.Is(err, driver.ErrReleaseNotFound) { // fresh install install := action.NewInstall(cfg) @@ -139,6 +141,20 @@ func (h *Helper) UpgradeChart(ch *chart.Chart, releaseName, namespace string, va r.Info.Status == release.StatusPendingRollback { return nil, fmt.Errorf("previous installation (e.g., using vela install or helm upgrade) is still in progress. Please try again in %d minutes", timeoutInMinutes) } + + } + + // merge un-existing values into the values as user-input, because the helm chart upgrade didn't handle the new default values in the chart. + if config.ReuseValues { + // sort will sort the release by revision from old to new + relutil.SortByRevision(releases) + rel := releases[len(releases)-1] + // merge new chart values into old values, the values of old chart has the high priority + mergedWithNewValues := chartutil.CoalesceTables(rel.Chart.Values, ch.Values) + // merge the chart with the released chart config but follow the old config + mergeWithConfigs := chartutil.CoalesceTables(rel.Config, mergedWithNewValues) + // merge new values as the user input, follow the new user input for --set + values = chartutil.CoalesceTables(values, mergeWithConfigs) } // overwrite existing installation