diff --git a/pkg/utils/helm/helm_helper.go b/pkg/utils/helm/helm_helper.go index d47899c09..d871426fd 100644 --- a/pkg/utils/helm/helm_helper.go +++ b/pkg/utils/helm/helm_helper.go @@ -140,20 +140,16 @@ 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. + // the new default values <= the old custom values <= the new custom values 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) + values = chartutil.CoalesceTables(values, rel.Config) } // overwrite existing installation @@ -161,7 +157,8 @@ func (h *Helper) UpgradeChart(ch *chart.Chart, releaseName, namespace string, va install.Namespace = namespace install.Wait = config.Wait install.Timeout = time.Duration(timeoutInMinutes) * time.Minute - install.ReuseValues = config.ReuseValues + // use the new default value set. + install.ReuseValues = false newRelease, err = install.Run(releaseName, ch, values) } // check if install/upgrade worked diff --git a/pkg/utils/helm/helm_helper_test.go b/pkg/utils/helm/helm_helper_test.go index f32f4142f..ad70b2a8a 100644 --- a/pkg/utils/helm/helm_helper_test.go +++ b/pkg/utils/helm/helm_helper_test.go @@ -49,7 +49,10 @@ var _ = Describe("Test helm helper", func() { helper := NewHelper() chart, err := helper.LoadCharts("./testdata/autoscalertrait-0.1.0.tgz", nil) Expect(err).Should(BeNil()) - release, err := helper.UpgradeChart(chart, "autoscalertrait", "default", nil, UpgradeChartOptions{ + release, err := helper.UpgradeChart(chart, "autoscalertrait", "default", map[string]interface{}{ + "replicaCount": 2, + "image.tag": "0.1.0", + }, UpgradeChartOptions{ Config: cfg, Detail: false, Logging: util.IOStreams{Out: os.Stdout, ErrOut: os.Stderr}, @@ -58,6 +61,42 @@ var _ = Describe("Test helm helper", func() { crds := GetCRDFromChart(release.Chart) Expect(cmp.Diff(len(crds), 1)).Should(BeEmpty()) Expect(err).Should(BeNil()) + deployments := GetDeploymentsFromManifest(release.Manifest) + Expect(cmp.Diff(len(deployments), 1)).Should(BeEmpty()) + Expect(cmp.Diff(*deployments[0].Spec.Replicas, int32(2))).Should(BeEmpty()) + containers := deployments[0].Spec.Template.Spec.Containers + Expect(cmp.Diff(len(containers), 1)).Should(BeEmpty()) + + // add new default value + Expect(cmp.Diff(containers[0].Image, "ghcr.io/oam-dev/catalog/autoscalertrait:0.1.0")).Should(BeEmpty()) + + chartNew, err := helper.LoadCharts("./testdata/autoscalertrait-0.2.0.tgz", nil) + Expect(err).Should(BeNil()) + + // the new custom values should override the last release custom values + releaseNew, err := helper.UpgradeChart(chartNew, "autoscalertrait", "default", map[string]interface{}{ + "image.tag": "0.2.0", + }, UpgradeChartOptions{ + Config: cfg, + Detail: false, + ReuseValues: true, + Logging: util.IOStreams{Out: os.Stdout, ErrOut: os.Stderr}, + Wait: false, + }) + Expect(err).Should(BeNil()) + deployments = GetDeploymentsFromManifest(releaseNew.Manifest) + Expect(cmp.Diff(len(deployments), 1)).Should(BeEmpty()) + // keep the custom values + Expect(cmp.Diff(*deployments[0].Spec.Replicas, int32(2))).Should(BeEmpty()) + containers = deployments[0].Spec.Template.Spec.Containers + Expect(cmp.Diff(len(containers), 1)).Should(BeEmpty()) + + // change the default value + Expect(cmp.Diff(containers[0].Image, "ghcr.io/oam-dev/catalog/autoscalertrait:0.2.0")).Should(BeEmpty()) + + // add new default value + Expect(cmp.Diff(len(containers[0].Env), 1)).Should(BeEmpty()) + Expect(cmp.Diff(containers[0].Env[0].Name, "env1")).Should(BeEmpty()) }) It("Test UninstallRelease", func() { diff --git a/pkg/utils/helm/testdata/autoscalertrait-0.1.0.tgz b/pkg/utils/helm/testdata/autoscalertrait-0.1.0.tgz index 7c2412ac5..3d912de2b 100644 Binary files a/pkg/utils/helm/testdata/autoscalertrait-0.1.0.tgz and b/pkg/utils/helm/testdata/autoscalertrait-0.1.0.tgz differ diff --git a/pkg/utils/helm/testdata/autoscalertrait-0.2.0.tgz b/pkg/utils/helm/testdata/autoscalertrait-0.2.0.tgz new file mode 100644 index 000000000..fe77cdf9d Binary files /dev/null and b/pkg/utils/helm/testdata/autoscalertrait-0.2.0.tgz differ