Skip to content

Commit 196c9f7

Browse files
authored
Support the upgrade flag --reset-then-reuse-values added in Helm v3.14.0 (#634)
* Allow fetching only user-supplied existing values * Add support for the --reset-then-reuse-values flag This flag was added in Helm v3.14.0; for details see helm/helm#9653. * Document --reset-then-reuse-values in the README * Add a comment referencing the new flag's origin * Move min Helm version check to narrower scope * Fix unintended name shadowing
1 parent 8294164 commit 196c9f7

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ Flags:
103103
--repo string specify the chart repository url to locate the requested chart
104104
--reset-values reset the values to the ones built into the chart and merge in any new values
105105
--reuse-values reuse the last release's values and merge in any new values. If '--reset-values' is specified, this is ignored
106+
--reset-then-reuse-values reset the values to the ones built into the chart, apply the last release's values and merge in any new values. If '--reset-values' or '--reuse-values' is specified, this is ignored
106107
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
107108
--set-file stringArray set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)
108109
--set-string stringArray set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
@@ -198,6 +199,7 @@ Flags:
198199
--repo string specify the chart repository url to locate the requested chart
199200
--reset-values reset the values to the ones built into the chart and merge in any new values
200201
--reuse-values reuse the last release's values and merge in any new values. If '--reset-values' is specified, this is ignored
202+
--reset-then-reuse-values reset the values to the ones built into the chart, apply the last release's values and merge in any new values. If '--reset-values' or '--reuse-values' is specified, this is ignored
201203
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
202204
--set-file stringArray set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)
203205
--set-string stringArray set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)

cmd/helm3.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ import (
1616
var (
1717
helmVersionRE = regexp.MustCompile(`Version:\s*"([^"]+)"`)
1818
minHelmVersion = semver.MustParse("v3.1.0-rc.1")
19-
// See https://github.com/helm/helm/pull/9426
19+
// See https://github.com/helm/helm/pull/9426.
2020
minHelmVersionWithDryRunLookupSupport = semver.MustParse("v3.13.0")
21+
// The --reset-then-reuse-values flag for `helm upgrade` was added in
22+
// https://github.com/helm/helm/pull/9653 and released as part of Helm v3.14.0.
23+
minHelmVersionWithResetThenReuseValues = semver.MustParse("v3.14.0")
2124
)
2225

2326
func getHelmVersion() (*semver.Version, error) {
@@ -132,15 +135,29 @@ func (d *diffCmd) template(isUpgrade bool) ([]byte, error) {
132135
// Let's simulate that in helm-diff.
133136
// See https://medium.com/@kcatstack/understand-helm-upgrade-flags-reset-values-reuse-values-6e58ac8f127e
134137
shouldDefaultReusingValues := isUpgrade && len(d.values) == 0 && len(d.stringValues) == 0 && len(d.stringLiteralValues) == 0 && len(d.jsonValues) == 0 && len(d.valueFiles) == 0 && len(d.fileValues) == 0
135-
if (d.reuseValues || shouldDefaultReusingValues) && !d.resetValues && d.clusterAccessAllowed() {
138+
if (d.reuseValues || d.resetThenReuseValues || shouldDefaultReusingValues) && !d.resetValues && d.clusterAccessAllowed() {
136139
tmpfile, err := os.CreateTemp("", "existing-values")
137140
if err != nil {
138141
return nil, err
139142
}
140143
defer func() {
141144
_ = os.Remove(tmpfile.Name())
142145
}()
143-
if err := d.writeExistingValues(tmpfile); err != nil {
146+
// In the presence of --reuse-values (or --reset-values), --reset-then-reuse-values is ignored.
147+
if d.resetThenReuseValues && !d.reuseValues {
148+
var supported bool
149+
supported, err = isHelmVersionAtLeast(minHelmVersionWithResetThenReuseValues)
150+
if err != nil {
151+
return nil, err
152+
}
153+
if !supported {
154+
return nil, fmt.Errorf("Using --reset-then-reuse-values requires at least helm version %s", minHelmVersionWithResetThenReuseValues.String())
155+
}
156+
err = d.writeExistingValues(tmpfile, false)
157+
} else {
158+
err = d.writeExistingValues(tmpfile, true)
159+
}
160+
if err != nil {
144161
return nil, err
145162
}
146163
flags = append(flags, "--values", tmpfile.Name())
@@ -308,8 +325,12 @@ func (d *diffCmd) template(isUpgrade bool) ([]byte, error) {
308325
return filter(out), err
309326
}
310327

311-
func (d *diffCmd) writeExistingValues(f *os.File) error {
312-
cmd := exec.Command(os.Getenv("HELM_BIN"), "get", "values", d.release, "--all", "--output", "yaml")
328+
func (d *diffCmd) writeExistingValues(f *os.File, all bool) error {
329+
args := []string{"get", "values", d.release, "--output", "yaml"}
330+
if all {
331+
args = append(args, "--all")
332+
}
333+
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
313334
debugPrint("Executing %s", strings.Join(cmd.Args, " "))
314335
defer func() {
315336
_ = f.Close()

cmd/upgrade.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type diffCmd struct {
5656
fileValues []string
5757
reuseValues bool
5858
resetValues bool
59+
resetThenReuseValues bool
5960
allowUnreleased bool
6061
noHooks bool
6162
includeTests bool
@@ -242,6 +243,7 @@ func newChartCommand() *cobra.Command {
242243
f.StringArrayVar(&diff.fileValues, "set-file", []string{}, "set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)")
243244
f.BoolVar(&diff.reuseValues, "reuse-values", false, "reuse the last release's values and merge in any new values. If '--reset-values' is specified, this is ignored")
244245
f.BoolVar(&diff.resetValues, "reset-values", false, "reset the values to the ones built into the chart and merge in any new values")
246+
f.BoolVar(&diff.resetThenReuseValues, "reset-then-reuse-values", false, "reset the values to the ones built into the chart, apply the last release's values and merge in any new values. If '--reset-values' or '--reuse-values' is specified, this is ignored")
245247
f.BoolVar(&diff.allowUnreleased, "allow-unreleased", false, "enables diffing of releases that are not yet deployed via Helm")
246248
f.BoolVar(&diff.install, "install", false, "enables diffing of releases that are not yet deployed via Helm (equivalent to --allow-unreleased, added to match \"helm upgrade --install\" command")
247249
f.BoolVar(&diff.noHooks, "no-hooks", false, "disable diffing of hooks")

0 commit comments

Comments
 (0)