Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the upgrade flag --reset-then-reuse-values added in Helm v3.14.0 #634

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 23 additions & 5 deletions cmd/helm3.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ var (
helmVersionRE = regexp.MustCompile(`Version:\s*"([^"]+)"`)
minHelmVersion = semver.MustParse("v3.1.0-rc.1")
// See https://github.com/helm/helm/pull/9426
minHelmVersionWithDryRunLookupSupport = semver.MustParse("v3.13.0")
minHelmVersionWithDryRunLookupSupport = semver.MustParse("v3.13.0")
minHelmVersionWithResetThenReuseValues = semver.MustParse("v3.14.0")
trk9001 marked this conversation as resolved.
Show resolved Hide resolved
)

func getHelmVersion() (*semver.Version, error) {
Expand Down Expand Up @@ -132,15 +133,28 @@ 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
}
resetThenReuseValuesIsSupported, err := isHelmVersionAtLeast(minHelmVersionWithResetThenReuseValues)
trk9001 marked this conversation as resolved.
Show resolved Hide resolved
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 {
if !resetThenReuseValuesIsSupported {
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 +322,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 {
yxxhero marked this conversation as resolved.
Show resolved Hide resolved
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
Loading