diff --git a/internal/app/cli.go b/internal/app/cli.go index 9067bc09..c947670c 100644 --- a/internal/app/cli.go +++ b/internal/app/cli.go @@ -154,7 +154,7 @@ func (c *cli) parse() { log.Fatal("this version of Helmsman does not work with helm releases older than 3.0.0") } - kubectlVersion := strings.TrimSpace(strings.SplitN(getKubectlClientVersion(), ": ", 2)[1]) + kubectlVersion := getKubectlVersion() log.Verbose("kubectl client version: " + kubectlVersion) if len(c.files) == 0 { diff --git a/internal/app/helm_helpers.go b/internal/app/helm_helpers.go index 469b01bf..d32cc526 100644 --- a/internal/app/helm_helpers.go +++ b/internal/app/helm_helpers.go @@ -69,32 +69,22 @@ func getChartInfo(chartName, chartVersion string) (*chartInfo, error) { // getHelmClientVersion returns Helm client Version func getHelmVersion() string { - cmd := helmCmd([]string{"version", "--short", "-c"}, "Checking Helm version") + cmd := helmCmd([]string{"version", "--short", "--client"}, "Checking Helm version") res, err := cmd.Exec() if err != nil { log.Fatalf("While checking helm version: %v", err) } - return res.output + version := strings.TrimSpace(res.output) + if !strings.HasPrefix(version, "v") { + version = strings.SplitN(version, ":", 2)[1] + } + return version } func checkHelmVersion(constraint string) bool { - helmVersion := strings.TrimSpace(getHelmVersion()) - extractedHelmVersion := helmVersion - if !strings.HasPrefix(helmVersion, "v") { - extractedHelmVersion = strings.TrimSpace(strings.Split(helmVersion, ":")[1]) - } - v, err := semver.NewVersion(extractedHelmVersion) - if err != nil { - return false - } - - jsonConstraint, err := semver.NewConstraint(constraint) - if err != nil { - return false - } - return jsonConstraint.Check(v) + return checkVersion(getHelmVersion(), constraint) } // helmPluginExists returns true if the plugin is present in the environment and false otherwise. diff --git a/internal/app/kube_helpers.go b/internal/app/kube_helpers.go index 002ca56f..64f021de 100644 --- a/internal/app/kube_helpers.go +++ b/internal/app/kube_helpers.go @@ -312,26 +312,44 @@ func getReleaseContext(releaseName, namespace, storageBackend string) string { return rctx } -// getKubectlClientVersion returns kubectl client version -func getKubectlClientVersion() string { - cmd := kubectl([]string{"version", "--client", "--short"}, "Checking kubectl version") +// getKubectlVersion returns kubectl client version +func getKubectlVersion() string { + cmd := kubectl([]string{"version", "--short", "--client"}, "Checking kubectl version") res, err := cmd.Exec() if err != nil { log.Fatalf("While checking kubectl version: %v", err) } - return res.output + version := strings.TrimSpace(res.output) + if !strings.HasPrefix(version, "v") { + version = strings.SplitN(version, ":", 2)[1] + } + return version +} + +func checkKubectlVersion(constraint string) bool { + return checkVersion(getKubectlVersion(), constraint) } // getKubeDryRunFlag returns kubectl dry-run flag if helmsman --dry-run flag is enabled +// TODO: this should be cleanup once 1.18 is old enough func (c *cli) getKubeDryRunFlag(action string) string { + var flag string if c.dryRun { + flag = "--dry-run" + recent := checkKubectlVersion(">=v1.18.0") switch action { case "apply": - return "--server-dry-run" + if recent { + flag += "=server" + } else { + flag = "--server-dry-run" + } default: - return "--dry-run" + if recent { + flag += "=client" + } } } - return "" + return flag } diff --git a/internal/app/utils.go b/internal/app/utils.go index 55867345..08950cd5 100644 --- a/internal/app/utils.go +++ b/internal/app/utils.go @@ -16,6 +16,7 @@ import ( "strings" "time" + "github.com/Masterminds/semver" "github.com/Praqma/helmsman/internal/aws" "github.com/Praqma/helmsman/internal/azure" "github.com/Praqma/helmsman/internal/gcs" @@ -468,6 +469,19 @@ func isValidFile(filePath string, allowedFileTypes []string) error { return nil } +func checkVersion(version, constraint string) bool { + v, err := semver.NewVersion(version) + if err != nil { + return false + } + + jsonConstraint, err := semver.NewConstraint(constraint) + if err != nil { + return false + } + return jsonConstraint.Check(v) +} + // notify MSTeams sends a JSON formatted message to MSTeams channel over a webhook url // It takes the content of the message (what changes helmsman is going to do or have done separated by \n) // and the webhook URL as well as a flag specifying if this is a failure message or not