From c2e8926f21976769d4e9b074b541e4f122e23438 Mon Sep 17 00:00:00 2001 From: Alexandre Vilain Date: Tue, 1 Dec 2020 20:54:00 +0100 Subject: [PATCH] Add ignore-already-exists flag to skip chartmuseum 409 error --- README.md | 8 ++++++++ cmd/helmpush/main.go | 13 ++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a31a499..0c3da2d 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,14 @@ Pushing mychart-0.3.2.tgz to chartmuseum... Done. ``` +If the `--force`/`-f` option is not set and the chart already exists you will get an error: +``` +$ helm push mychart/ chartmuseum +Pushing mychart-0.1.0.tgz to chartmuseum... +Error: 409: mychart-0.1.0.tgz already exists +``` +For some environnements, like in CI/CD pipelines, this error should not stop the whole pipeline and can be skipped using the `--ignore-already-exists` option. + ### Pushing directly to URL If the second argument provided resembles a URL, you are not required to add the repo prior to push: ``` diff --git a/cmd/helmpush/main.go b/cmd/helmpush/main.go index 40801ab..906006b 100644 --- a/cmd/helmpush/main.go +++ b/cmd/helmpush/main.go @@ -40,6 +40,7 @@ type ( authHeader string contextPath string forceUpload bool + ignoreAlreadyExistsError bool useHTTP bool checkHelmVersion bool caFile string @@ -121,6 +122,7 @@ func newPushCmd(args []string) *cobra.Command { f.StringVar(&p.keyring, "keyring", defaultKeyring(), "location of a public keyring") f.BoolVarP(&p.insecureSkipVerify, "insecure", "", false, "Connect to server with an insecure way by skipping certificate verification [$HELM_REPO_INSECURE]") f.BoolVarP(&p.forceUpload, "force", "f", false, "Force upload even if chart version exists") + f.BoolVarP(&p.ignoreAlreadyExistsError, "ignore-already-exists", "", false, "Skip the already exisits error if chart version exists") f.BoolVarP(&p.dependencyUpdate, "dependency-update", "d", false, `update dependencies from "requirements.yaml" to dir "charts/" before packaging`) f.BoolVarP(&p.checkHelmVersion, "check-helm-version", "", false, `outputs either "2" or "3" indicating the current Helm major version`) @@ -328,7 +330,7 @@ func (p *pushCmd) push() error { return err } - return handlePushResponse(resp) + return p.handlePushResponse(resp) } func (p *pushCmd) download(fileURL string) error { @@ -384,13 +386,18 @@ func (p *pushCmd) download(fileURL string) error { return handleDownloadResponse(resp) } -func handlePushResponse(resp *http.Response) error { +func (p *pushCmd) handlePushResponse(resp *http.Response) error { if resp.StatusCode != 201 { b, err := ioutil.ReadAll(resp.Body) if err != nil { return err } - return getChartmuseumError(b, resp.StatusCode) + err = getChartmuseumError(b, resp.StatusCode) + if p.ignoreAlreadyExistsError && resp.StatusCode == 409 { + fmt.Printf("Got the following error: %s \nSkipping.\n", err) + return nil + } + return err } fmt.Println("Done.") return nil