From 539cfab8cf5fa9be28db9ee270537b2995b0593f Mon Sep 17 00:00:00 2001 From: Ivan Pedrazas Date: Wed, 11 Jan 2017 20:10:57 +0000 Subject: [PATCH] --tiller-namespace para added --- README.md | 15 ++++++++++++++- main.go | 6 ++++++ plugin.go | 19 +++++++++++++++---- plugin_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d7c4e2d..ad98379 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,20 @@ To test the plugin, you can run `minikube` and just run the docker image as foll quay.io/ipedrazas/drone-helm -This plugin expects [Tiller](https://github.com/kubernetes/helm/blob/master/docs/architecture.md) to be already installed in the cluster +This plugin installs [Tiller](https://github.com/kubernetes/helm/blob/master/docs/architecture.md) in the cluster, if you want to specify the namespace where `tiller` ins installed, use the `tiller_ns` attribute. +The following example will install `tiller` in the `operations` namespace: + + pipeline_production: + helm_deploy: + image: quay.io/ipedrazas/drone-helm + skip_tls_verify: true + chart: ./charts/my-chart + release: ${DRONE_BRANCH} + values: image.tag=${DRONE_BRANCH}-${DRONE_COMMIT_SHA:0:7} + prefix: PROD + tiller_ns: operations + when: + branch: [master] Happy Helming! diff --git a/main.go b/main.go index 53dff9f..1e512f8 100644 --- a/main.go +++ b/main.go @@ -63,6 +63,11 @@ func main() { Usage: "Prefix for all the secrets", EnvVar: "PLUGIN_PREFIX,PREFIX", }, + cli.StringFlag{ + Name: "tiller-ns", + Usage: "Namespace to install Tiller", + EnvVar: "PLUGIN_TILLER_NS,TILLER_NS", + }, } if err := app.Run(os.Args); err != nil { logrus.Fatal(err) @@ -87,6 +92,7 @@ func run(c *cli.Context) error { DryRun: c.Bool("dry-run"), Secrets: c.StringSlice("secrets"), Prefix: c.String("prefix"), + TillerNs: c.String("tiller_ns"), }, } resolveSecrets(&plugin) diff --git a/plugin.go b/plugin.go index 305f6c2..d120d22 100644 --- a/plugin.go +++ b/plugin.go @@ -30,6 +30,7 @@ type ( DryRun bool `json:"dry_run"` Secrets []string `json:"secrets"` Prefix string `json:"prefix"` + TillerNs string `json:"tiller_ns"` } // Plugin default Plugin struct { @@ -83,6 +84,17 @@ func setHelmCommand(p *Plugin) { } +func doHelmInit(p *Plugin) []string { + init := make([]string, 1) + init[0] = "init" + if p.Config.TillerNs != "" { + init = append(init, "--tiller-namespace") + init = append(init, p.Config.TillerNs) + } + return init + +} + // Exec default method func (p *Plugin) Exec() error { resolveSecrets(p) @@ -98,16 +110,15 @@ func (p *Plugin) Exec() error { p.debug() } - init := make([]string, 1) - init[0] = "init" + init := doHelmInit(p) err := runCommand(init) if err != nil { return fmt.Errorf("Error running helm comand: " + strings.Join(init[:], " ")) } - setHelmCommand(p) + if p.Config.Debug { - log.Println("helm comand: " + strings.Join(p.Config.HelmCommand[:], " ")) + log.Println("helm command: " + strings.Join(p.Config.HelmCommand[:], " ")) } err = runCommand(p.Config.HelmCommand) if err != nil { diff --git a/plugin_test.go b/plugin_test.go index 9b5b082..dc03ea1 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -143,3 +143,27 @@ func TestSetHelmHelp(t *testing.T) { t.Error("Helm help is not displayed") } } + +func TestDetHelmInit(t *testing.T) { + plugin := &Plugin{ + Config: Config{ + HelmCommand: nil, + Namespace: "default", + SkipTLSVerify: true, + Debug: true, + DryRun: true, + Chart: "./chart/test", + Release: "test-release", + Prefix: "MY", + Values: "image.tag=$TAG,api=${API_SERVER},nameOverride=my-over-app,second.tag=${TAG}", + TillerNs: "system-test", + }, + } + init := doHelmInit(plugin) + result := strings.Join(init, " ") + expected := "init --tiller-namespace " + plugin.Config.TillerNs + + if expected != result { + t.Error("Tiller not installed in proper namespace") + } +}