Skip to content
This repository has been archived by the owner on Oct 27, 2023. It is now read-only.

Commit

Permalink
major: Changed repos to use the new Helm 3 way, this breaks the use o…
Browse files Browse the repository at this point in the history
…f repos with Helm 2!
  • Loading branch information
belitre committed Sep 29, 2019
1 parent 2af2f20 commit b068a81
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 179 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ vet:

fmt:
@set -e; \
GO_FMT=$$(git ls-files *.go | grep -v 'vendor/' | xargs gofmt -d); \
GO_FMT=$$(git ls-files *.go | grep -v 'vendor/' | xargs gofmt -d -s); \
if [ -n "$${GO_FMT}" ] ; then \
echo "Please run go fmt"; \
echo "$$GO_FMT"; \
Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Helm push artifactory plugin

__WARNING: THIS VERSION IS FOR HELM 3, FOR HELM 2 PLEASE USE VERSION [v0.4.0](https://github.com/belitre/helm-push-artifactory-plugin/releases/tag/v0.4.0)__

A Helm plugin to push helm charts to artifactory:

* A version for artifactory of helm-push: https://github.com/chartmuseum/helm-push
Expand All @@ -8,24 +10,16 @@ A Helm plugin to push helm charts to artifactory:

## Install

Based on the version in `plugin.yaml`, release binary will be downloaded from GitHub:

```
$ helm plugin install https://github.com/belitre/helm-push-artifactory-plugin
Downloading and installing helm-push-artifactory v0.4.0 ...
https://github.com/belitre/helm-push-artifactory-plugin/releases/download/v0.4.0/helm-push-artifactory_v0.4.0_darwin_amd64.tar.gz
Installed plugin: push-artifactory
```
To install the version for Helm 3 you need to specify the version when installing using helm cli:

You can specify an specific version:
```
$ helm plugin install https://github.com/belitre/helm-push-artifactory-plugin --version v0.3.0
Downloading and installing helm-push-artifactory v0.3.0 ...
https://github.com/belitre/helm-push-artifactory-plugin/releases/download/v0.3.0/helm-push-artifactory_v0.3.0_darwin_amd64.tar.gz
$ helm plugin install https://github.com/belitre/helm-push-artifactory-plugin --version v1.0.0
Downloading and installing helm-push-artifactory v1.0.0 ...
https://github.com/belitre/helm-push-artifactory-plugin/releases/download/v1.0.0/helm-push-artifactory_v1.0.0_darwin_amd64.tar.gz
Installed plugin: push-artifactory
```

You can also download on one of the compressed files from [here](https://github.com/belitre/helm-push-artifactory-plugin/releases) and just extract it in your `$HELM_HOME/plugins/`
You can also download on one of the compressed files from [here](https://github.com/belitre/helm-push-artifactory-plugin/releases/tag/v1.0.0) and just extract it in your `$HELM_HOME/plugins/`

__Important for windows users: I really don't know how to make the `helm plugin install` command work on Windows :D so please just download the zip and extract it on your `$HELM_HOME/plugins/ folder :)__

Expand Down Expand Up @@ -86,6 +80,12 @@ Example:
## Usage
You can override the repositories file using the environment variable `HELM_REPOSITORY_CONFIG`, example:
```
export HELM_REPOSITORY_CONFIG=/home/myuser/my_repositories.yaml
```
Example using URL:
```bash
Expand Down
25 changes: 17 additions & 8 deletions cmd/push/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

"github.com/belitre/helm-push-artifactory-plugin/pkg/artifactory"
"github.com/belitre/helm-push-artifactory-plugin/pkg/helm"
helmrepo "github.com/belitre/helm-push-artifactory-plugin/pkg/repo"
"github.com/belitre/helm-push-artifactory-plugin/pkg/version"
helmpush "github.com/chartmuseum/helm-push/pkg/helm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -55,7 +55,7 @@ func getUsage() string {
return strings.Replace(globalUsage, "%version%", version.GetVersion(), 1)
}

func newPushCmd(args []string) *cobra.Command {
func newPushCmd(args []string) (*cobra.Command, error) {
p := &pushCmd{}
cmd := &cobra.Command{
Use: "helm push-artifactory",
Expand Down Expand Up @@ -87,7 +87,7 @@ func newPushCmd(args []string) *cobra.Command {
f.BoolVarP(&p.insecureSkipVerify, "insecure", "", false, "Connect to server with an insecure way by skipping certificate verification [$HELM_REPO_INSECURE]")
f.BoolVarP(&p.skipReindex, "skip-reindex", "", false, "Avoid trigger reindex in the repository after pushing the chart [$HELM_REPO_SKIP_REINDEX]")
f.Parse(args)
return cmd
return cmd, nil
}

func (p *pushCmd) setFieldsFromEnv() {
Expand Down Expand Up @@ -124,7 +124,7 @@ func (p *pushCmd) setFieldsFromEnv() {
}

func (p *pushCmd) push() error {
var repo *helmpush.Repo
var repo *helmrepo.Repo
var err error

// If the argument looks like a URL, just create a temp repo object
Expand All @@ -133,7 +133,7 @@ func (p *pushCmd) push() error {
// Check valid URL
_, err = url.ParseRequestURI(p.repository)
} else {
repo, err = helmpush.GetRepoByName(p.repository)
repo, err = helmrepo.GetRepoByName(p.repository)
}

if err != nil {
Expand All @@ -151,7 +151,10 @@ func (p *pushCmd) push() error {
}

if len(p.overrides) > 0 {
chart.OverrideValues(p.overrides)
err := chart.OverrideValues(p.overrides)
if err != nil {
return err
}
}

if repo != nil {
Expand Down Expand Up @@ -201,7 +204,7 @@ func (p *pushCmd) push() error {
return err
}

resp, err := client.UploadChartPackage(chart.GetMetadata().GetName(), chartPackagePath)
resp, err := client.UploadChartPackage(chart.Metadata.Name, chartPackagePath)
if err != nil {
return err
}
Expand All @@ -222,6 +225,7 @@ func (p *pushCmd) push() error {
}

func handleReindexResponse(resp *http.Response) error {
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
Expand All @@ -235,6 +239,7 @@ func handleReindexResponse(resp *http.Response) error {
}

func handlePushResponse(resp *http.Response) error {
defer resp.Body.Close()
if resp.StatusCode != 201 {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down Expand Up @@ -266,8 +271,12 @@ func getArtifactoryError(b []byte, code int) error {
}

func main() {
cmd := newPushCmd(os.Args[1:])
cmd, err := newPushCmd(os.Args[1:])
if err != nil {
fmt.Println(fmt.Sprintf("%v", err))
}
if err := cmd.Execute(); err != nil {
fmt.Println(fmt.Sprintf("%v", err))
os.Exit(1)
}
}
87 changes: 37 additions & 50 deletions cmd/push/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"net/http"
"net/http/httptest"
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"

"k8s.io/helm/pkg/getter"
helm_env "k8s.io/helm/pkg/helm/environment"
"k8s.io/helm/pkg/helm/helmpath"
"k8s.io/helm/pkg/repo"
"helm.sh/helm/pkg/cli"
"helm.sh/helm/pkg/getter"
"helm.sh/helm/pkg/repo"
)

var (
settings helm_env.EnvSettings
settings = &cli.EnvSettings{}
testTarballPath = "../../testdata/charts/mychart/mychart-0.1.0.tgz"
testCertPath = "../../testdata/tls/test_cert.crt"
testKeyPath = "../../testdata/tls/test_key.key"
Expand Down Expand Up @@ -50,8 +50,9 @@ func TestPushCmd(t *testing.T) {
}
defer os.RemoveAll(tmp)

home := helmpath.Home(tmp)
f := repo.NewRepoFile()
filepath := path.Join(tmp, "repositories.yaml")

f := repo.NewFile()

entry := repo.Entry{}
entry.Name = "helm-push-test"
Expand All @@ -63,88 +64,74 @@ func TestPushCmd(t *testing.T) {
}

f.Update(&entry)
os.MkdirAll(home.Repository(), 0777)
f.WriteFile(home.RepositoryFile(), 0644)
err = f.WriteFile(filepath, 0644)
assert.NoError(t, err)

os.Setenv("HELM_HOME", home.String())
os.Setenv("HELM_REPOSITORY_CONFIG", filepath)
os.Setenv("HELM_REPO_USERNAME", "myuser")
os.Setenv("HELM_REPO_PASSWORD", "mypass")

// Not enough args
args := []string{}
cmd := newPushCmd(args)
cmd, err := newPushCmd(args)
assert.NoError(t, err)
err = cmd.RunE(cmd, args)
if err == nil {
t.Error("expecting error with missing args, instead got nil")
}
assert.Error(t, err)

// Bad chart path
args = []string{"/this/this/not/a/chart", "helm-push-test"}
cmd = newPushCmd(args)
args = []string{"/this/not/a/chart", "helm-push-test"}
cmd, err = newPushCmd(args)
assert.NoError(t, err)
err = cmd.RunE(cmd, args)
if err == nil {
t.Error("expecting error with bad chart path, instead got nil")
}
assert.Error(t, err)

// Bad repo name
args = []string{testTarballPath, "wkerjbnkwejrnkj"}
cmd = newPushCmd(args)
cmd, err = newPushCmd(args)
assert.NoError(t, err)
err = cmd.RunE(cmd, args)
if err == nil {
t.Error("expecting error with bad repo name, instead got nil")
}
assert.Error(t, err)

// Happy path
args = []string{testTarballPath, "helm-push-test"}
cmd = newPushCmd(args)
cmd, err = newPushCmd(args)
assert.NoError(t, err)
err = cmd.RunE(cmd, args)
if err != nil {
t.Error("unexpecting error uploading tarball", err)
}
assert.NoError(t, err)

// Happy path by repo URL
args = []string{testTarballPath, ts.URL}
cmd = newPushCmd(args)
cmd, err = newPushCmd(args)
assert.NoError(t, err)
err = cmd.RunE(cmd, args)
if err != nil {
t.Error("unexpecting error uploading tarball", err)
}
assert.NoError(t, err)

// Trigger reindex error
postStatusCode = 403
body = "{\"errors\": [{\"message\": \"Error\", \"status\": 403}]}"
args = []string{testTarballPath, ts.URL}
cmd = newPushCmd(args)
cmd, err = newPushCmd(args)
assert.NoError(t, err)
err = cmd.RunE(cmd, args)
if err == nil {
t.Error("expecting error with 403, instead got nil")
} else {
assert.Error(t, err, "403: Error")
}
assert.Error(t, err, "403: Error")

// Trigger 409
putStatusCode = 409
body = "{\"errors\": [{\"message\": \"Error\", \"status\": 409}]}"
args = []string{testTarballPath, ts.URL}
cmd = newPushCmd(args)
cmd, err = newPushCmd(args)
assert.NoError(t, err)
err = cmd.RunE(cmd, args)
if err == nil {
t.Error("expecting error with 409, instead got nil")
} else {
assert.Error(t, err, "409: Error")
}
assert.Error(t, err, "409: Error")

// Unable to parse JSON response body
putStatusCode = 500
body = "qkewjrnvqejrnbvjern"
args = []string{testTarballPath, ts.URL}
cmd = newPushCmd(args)
cmd, err = newPushCmd(args)
assert.NoError(t, err)
err = cmd.RunE(cmd, args)
if err == nil {
t.Error("expecting error with bad response body, instead got nil")
} else {
assert.Error(t, err, "500: could not properly parse response JSON: qkewjrnvqejrnbvjern")
}
assert.Error(t, err, "500: could not properly parse response JSON: qkewjrnvqejrnbvjern")

}

Expand Down Expand Up @@ -203,7 +190,7 @@ func TestPushCmdWithTlsEnabledServer(t *testing.T) {
os.MkdirAll(home.Repository(), 0777)
f.WriteFile(home.RepositoryFile(), 0644)
os.Setenv("HELM_HOME", home.String())
os.Setenv("HELM_REPOSITORY_CONFIG", home.String())
os.Setenv("HELM_REPO_USERNAME", "myuser")
os.Setenv("HELM_REPO_PASSWORD", "mypass")
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ require (
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/protobuf v1.3.2 // indirect
github.com/jfrog/jfrog-client-go v0.5.2
github.com/pkg/errors v0.8.0
github.com/spf13/cobra v0.0.5
github.com/stretchr/testify v1.4.0
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.1.0 // indirect
gopkg.in/yaml.v2 v2.2.2
k8s.io/apimachinery v0.0.0-20190925235427-62598f38f24e // indirect
helm.sh/helm v3.0.0-beta.3+incompatible
k8s.io/api v0.0.0-20190927115716-5d581ce610b0 // indirect
k8s.io/client-go v11.0.0+incompatible // indirect
k8s.io/helm v2.14.3+incompatible
)
Loading

0 comments on commit b068a81

Please sign in to comment.