Skip to content

Commit

Permalink
Add ignore-already-exists flag to skip chartmuseum 409 error
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrevilain committed Dec 1, 2020
1 parent a0a1e31 commit d954108
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
```
Expand Down
49 changes: 28 additions & 21 deletions cmd/helmpush/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,25 @@ import (

type (
pushCmd struct {
chartName string
chartVersion string
repoName string
username string
password string
accessToken string
authHeader string
contextPath string
forceUpload bool
useHTTP bool
checkHelmVersion bool
caFile string
certFile string
keyFile string
insecureSkipVerify bool
keyring string
dependencyUpdate bool
out io.Writer
chartName string
chartVersion string
repoName string
username string
password string
accessToken string
authHeader string
contextPath string
forceUpload bool
ignoreAlreadyExistsError bool
useHTTP bool
checkHelmVersion bool
caFile string
certFile string
keyFile string
insecureSkipVerify bool
keyring string
dependencyUpdate bool
out io.Writer
}

config struct {
Expand Down Expand Up @@ -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`)

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d954108

Please sign in to comment.