From 4b83bf8bc584aa16668858b1e5136b05252e5854 Mon Sep 17 00:00:00 2001 From: Mateusz Kubaczyk Date: Mon, 21 Sep 2020 09:06:35 +0200 Subject: [PATCH] Add --force-update to helm repo add command (breaking change in 3.3.2 helm) --- internal/app/cli.go | 12 ++---------- internal/app/helm_helpers.go | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/internal/app/cli.go b/internal/app/cli.go index 92d2de6e..79c75b30 100644 --- a/internal/app/cli.go +++ b/internal/app/cli.go @@ -6,7 +6,6 @@ import ( "os" "strings" - version "github.com/hashicorp/go-version" "github.com/imdario/mergo" "github.com/joho/godotenv" ) @@ -144,15 +143,8 @@ func (c *cli) parse() { c.parallel = 1 } - helmVersion := strings.TrimSpace(getHelmVersion()) - extractedHelmVersion := helmVersion - if !strings.HasPrefix(helmVersion, "v") { - extractedHelmVersion = strings.TrimSpace(strings.Split(helmVersion, ":")[1]) - } - log.Verbose("Helm client version: " + extractedHelmVersion) - v1, _ := version.NewVersion(extractedHelmVersion) - jsonConstraint, _ := version.NewConstraint(">=3.0.0") - if !jsonConstraint.Check(v1) { + log.Verbose("Helm client version: " + strings.TrimSpace(getHelmVersion())) + if checkHelmVersion("<3.0.0") { log.Fatal("this version of Helmsman does not work with helm releases older than 3.0.0") } diff --git a/internal/app/helm_helpers.go b/internal/app/helm_helpers.go index 98680ecc..7d2b4a10 100644 --- a/internal/app/helm_helpers.go +++ b/internal/app/helm_helpers.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/hashicorp/go-version" "net/url" "strings" @@ -57,6 +58,20 @@ func getHelmVersion() string { return result.output } +func checkHelmVersion(constraint string) bool { + helmVersion := strings.TrimSpace(getHelmVersion()) + extractedHelmVersion := helmVersion + if !strings.HasPrefix(helmVersion, "v") { + extractedHelmVersion = strings.TrimSpace(strings.Split(helmVersion, ":")[1]) + } + v, _ := version.NewVersion(extractedHelmVersion) + jsonConstraint, _ := version.NewConstraint(constraint) + if jsonConstraint.Check(v) { + return true + } + return false +} + // helmPluginExists returns true if the plugin is present in the environment and false otherwise. // It takes as input the plugin's name to check if it is recognizable or not. e.g. diff func helmPluginExists(plugin string) bool { @@ -132,7 +147,11 @@ func addHelmRepos(repos map[string]string) error { repoLink = u.String() } - cmd := helmCmd(concat([]string{"repo", "add", repoName, repoLink}, basicAuthArgs), "Adding helm repository [ "+repoName+" ]") + repoAddFlags := "" + if checkHelmVersion(">=3.3.2") { + repoAddFlags += "--force-update" + } + cmd := helmCmd(concat([]string{"repo", "add", repoAddFlags, repoName, repoLink}, basicAuthArgs), "Adding helm repository [ "+repoName+" ]") // check current repository against existing repositories map in order to make sure it's missing and needs to be added if existingRepoUrl, ok := existingRepos[repoName]; ok { if repoLink == existingRepoUrl {