Skip to content

Commit

Permalink
Support the upgrade flag --reset-then-reuse-values added in Helm v3.1…
Browse files Browse the repository at this point in the history
…4.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
  • Loading branch information
trk9001 authored Oct 23, 2024
1 parent 8294164 commit 196c9f7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Flags:
--repo string specify the chart repository url to locate the requested chart
--reset-values reset the values to the ones built into the chart and merge in any new values
--reuse-values reuse the last release's values and merge in any new values. If '--reset-values' is specified, this is ignored
--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
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
--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)
--set-string stringArray set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
Expand Down Expand Up @@ -198,6 +199,7 @@ Flags:
--repo string specify the chart repository url to locate the requested chart
--reset-values reset the values to the ones built into the chart and merge in any new values
--reuse-values reuse the last release's values and merge in any new values. If '--reset-values' is specified, this is ignored
--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
--set stringArray set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
--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)
--set-string stringArray set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
Expand Down
31 changes: 26 additions & 5 deletions cmd/helm3.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import (
var (
helmVersionRE = regexp.MustCompile(`Version:\s*"([^"]+)"`)
minHelmVersion = semver.MustParse("v3.1.0-rc.1")
// See https://github.com/helm/helm/pull/9426
// See https://github.com/helm/helm/pull/9426.
minHelmVersionWithDryRunLookupSupport = semver.MustParse("v3.13.0")
// The --reset-then-reuse-values flag for `helm upgrade` was added in
// https://github.com/helm/helm/pull/9653 and released as part of Helm v3.14.0.
minHelmVersionWithResetThenReuseValues = semver.MustParse("v3.14.0")
)

func getHelmVersion() (*semver.Version, error) {
Expand Down Expand Up @@ -132,15 +135,29 @@ func (d *diffCmd) template(isUpgrade bool) ([]byte, error) {
// Let's simulate that in helm-diff.
// See https://medium.com/@kcatstack/understand-helm-upgrade-flags-reset-values-reuse-values-6e58ac8f127e
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
if (d.reuseValues || shouldDefaultReusingValues) && !d.resetValues && d.clusterAccessAllowed() {
if (d.reuseValues || d.resetThenReuseValues || shouldDefaultReusingValues) && !d.resetValues && d.clusterAccessAllowed() {
tmpfile, err := os.CreateTemp("", "existing-values")
if err != nil {
return nil, err
}
defer func() {
_ = os.Remove(tmpfile.Name())
}()
if err := d.writeExistingValues(tmpfile); err != nil {
// In the presence of --reuse-values (or --reset-values), --reset-then-reuse-values is ignored.
if d.resetThenReuseValues && !d.reuseValues {
var supported bool
supported, err = isHelmVersionAtLeast(minHelmVersionWithResetThenReuseValues)
if err != nil {
return nil, err
}
if !supported {
return nil, fmt.Errorf("Using --reset-then-reuse-values requires at least helm version %s", minHelmVersionWithResetThenReuseValues.String())
}
err = d.writeExistingValues(tmpfile, false)
} else {
err = d.writeExistingValues(tmpfile, true)
}
if err != nil {
return nil, err
}
flags = append(flags, "--values", tmpfile.Name())
Expand Down Expand Up @@ -308,8 +325,12 @@ func (d *diffCmd) template(isUpgrade bool) ([]byte, error) {
return filter(out), err
}

func (d *diffCmd) writeExistingValues(f *os.File) error {
cmd := exec.Command(os.Getenv("HELM_BIN"), "get", "values", d.release, "--all", "--output", "yaml")
func (d *diffCmd) writeExistingValues(f *os.File, all bool) error {
args := []string{"get", "values", d.release, "--output", "yaml"}
if all {
args = append(args, "--all")
}
cmd := exec.Command(os.Getenv("HELM_BIN"), args...)
debugPrint("Executing %s", strings.Join(cmd.Args, " "))
defer func() {
_ = f.Close()
Expand Down
2 changes: 2 additions & 0 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type diffCmd struct {
fileValues []string
reuseValues bool
resetValues bool
resetThenReuseValues bool
allowUnreleased bool
noHooks bool
includeTests bool
Expand Down Expand Up @@ -242,6 +243,7 @@ func newChartCommand() *cobra.Command {
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)")
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")
f.BoolVar(&diff.resetValues, "reset-values", false, "reset the values to the ones built into the chart and merge in any new values")
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")
f.BoolVar(&diff.allowUnreleased, "allow-unreleased", false, "enables diffing of releases that are not yet deployed via Helm")
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")
f.BoolVar(&diff.noHooks, "no-hooks", false, "disable diffing of hooks")
Expand Down

0 comments on commit 196c9f7

Please sign in to comment.