Skip to content

Commit

Permalink
Use dry-run=server to enable lookups
Browse files Browse the repository at this point in the history
Helm PR helm/helm#9426 enables support for executing lookups during dry run. This PR is to make use of this new support in helm-diff.
Backwards compatibility for older versions of helm is maintained by checking the helm version before setting the flag

Addresses issue: databus23#449

Signed-off-by: MichaelMorris <michael.morris@est.tech>
(cherry picked from commit 3be20a8)
  • Loading branch information
MichaelMorrisEst authored and bdwyertech committed May 16, 2023
1 parent 119b9e0 commit 081c9b1
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions cmd/helm3.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,53 @@ import (
)

var (
helmVersionRE = regexp.MustCompile(`Version:\s*"([^"]+)"`)
minHelmVersion = semver.MustParse("v3.1.0-rc.1")
helmVersionRE = regexp.MustCompile(`Version:\s*"([^"]+)"`)
minHelmVersion = semver.MustParse("v3.1.0-rc.1")
minHelmVersionWithDryRunLookupSupport = semver.MustParse("v3.13.0")
)

func compatibleHelm3Version() error {
func getHelmVersion() (*semver.Version, error) {
cmd := exec.Command(os.Getenv("HELM_BIN"), "version")
debugPrint("Executing %s", strings.Join(cmd.Args, " "))
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Failed to run `%s version`: %v", os.Getenv("HELM_BIN"), err)
return nil, fmt.Errorf("Failed to run `%s version`: %v", os.Getenv("HELM_BIN"), err)
}
versionOutput := string(output)

matches := helmVersionRE.FindStringSubmatch(versionOutput)
if matches == nil {
return fmt.Errorf("Failed to find version in output %#v", versionOutput)
return nil, fmt.Errorf("Failed to find version in output %#v", versionOutput)
}
helmVersion, err := semver.NewVersion(matches[1])
if err != nil {
return fmt.Errorf("Failed to parse version %#v: %v", matches[1], err)
return nil, fmt.Errorf("Failed to parse version %#v: %v", matches[1], err)
}

return helmVersion, nil
}

func isHelmVersionAtLeast(versionToCompareTo *semver.Version) (bool, error) {
helmVersion, err := getHelmVersion()

if err != nil {
return false, err
}
if helmVersion.LessThan(versionToCompareTo) {
return false, nil
}
return true, nil
}

if minHelmVersion.GreaterThan(helmVersion) {
func compatibleHelm3Version() error {
if isCompatible, err := isHelmVersionAtLeast(minHelmVersion); err != nil {
return err
} else if !isCompatible {
return fmt.Errorf("helm diff upgrade requires at least helm version %s", minHelmVersion.String())
}
return nil

}

func getRelease(release, namespace string) ([]byte, error) {
args := []string{"get", "manifest", release}
if namespace != "" {
Expand Down Expand Up @@ -177,7 +196,11 @@ func (d *diffCmd) template(isUpgrade bool) ([]byte, error) {
flags = append(flags, "--install")
}

flags = append(flags, "--dry-run")
if useDryRunService, err := isHelmVersionAtLeast(minHelmVersionWithDryRunLookupSupport); err == nil && useDryRunService {
flags = append(flags, "--dry-run=server")
} else {
flags = append(flags, "--dry-run")
}
subcmd = "upgrade"
filter = func(s []byte) []byte {
return extractManifestFromHelmUpgradeDryRunOutput(s, d.noHooks)
Expand All @@ -199,6 +222,10 @@ func (d *diffCmd) template(isUpgrade bool) ([]byte, error) {
flags = append(flags, "--kube-version", d.kubeVersion)
}

if useDryRunService, err := isHelmVersionAtLeast(minHelmVersionWithDryRunLookupSupport); err == nil && useDryRunService {
flags = append(flags, "--dry-run=server")
}

subcmd = "template"

filter = func(s []byte) []byte {
Expand Down

0 comments on commit 081c9b1

Please sign in to comment.