Skip to content

Commit

Permalink
fix: --server-dry-run was deprecated in kubectl 1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Davim committed May 14, 2021
1 parent 60282a7 commit 167a2d6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion internal/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 7 additions & 17 deletions internal/app/helm_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 25 additions & 7 deletions internal/app/kube_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
14 changes: 14 additions & 0 deletions internal/app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 167a2d6

Please sign in to comment.