Skip to content

Commit baf4df6

Browse files
author
Chris Montoro
authored
DV-12285: add create-namespace parameter (#155)
1 parent 2909135 commit baf4df6

File tree

5 files changed

+78
-2
lines changed

5 files changed

+78
-2
lines changed

.drone.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ slack_config: &slack_config
2222
image: plugins/slack
2323
pull: if-not-exists
2424
settings:
25-
channel: dv-notifications
25+
channel: dv-cicd
2626
environment:
2727
SLACK_WEBHOOK:
2828
from_secret: slack_webhook

DOCS.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,30 @@ steps:
154154
# ...
155155
```
156156

157+
### `namespace`
158+
159+
_**type**_ `string`
160+
161+
_**default**_ `''`
162+
163+
_**description**_ name of Kubernetes Namespace where manifests will be applied
164+
165+
_**notes**_ if not specified, resources will be applied to `default` Namespace; if specified and [`create_namespace`](#create_namespace) is set to `false`, the Namespace resource must already exist within the cluster
166+
167+
_**example**_
168+
169+
```yaml
170+
# .drone.yml
171+
---
172+
pipeline:
173+
# ...
174+
deploy:
175+
image: nytimes/drone-gke
176+
cluster: prod
177+
namespace: petstore
178+
# ...
179+
```
180+
157181
### `template`
158182

159183
_**type**_ `string`
@@ -511,6 +535,30 @@ steps:
511535
# ...
512536
```
513537

538+
### `create_namespace`
539+
540+
_**type**_ `bool`
541+
542+
_**default**_ `true`
543+
544+
_**description**_ automatically create a Namespace resource when a [`namespace`](#namespace) value is specified
545+
546+
_**notes**_ depends on non-empty `namespace` value; the resource will _always_ be applied to the cluster _prior to_ any resources included in [`template`](#template) / [`secret_template`](#secret_template); may modify any existing Namespace resource configuration; the automatically created Namespace resource is not configurable (see [source](https://github.com/nytimes/drone-gke/blob/2909135b2dce136aa5095d609d91b0963fbb4697/main.go#L51-L54) for more details);
547+
548+
_**example**_
549+
550+
```yaml
551+
# .drone.yml
552+
---
553+
pipeline:
554+
# ...
555+
deploy:
556+
image: nytimes/drone-gke
557+
namespace: petstore
558+
create_namespace: false
559+
# ...
560+
```
561+
514562
## Service Account Credentials
515563

516564
`drone-gke` requires a Google service account and uses its [JSON credential file][service-account] to authenticate.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ $(coverage_name) : export GO111MODULE ?= on
143143
$(coverage_name) : export GOPROXY ?= https://proxy.golang.org
144144

145145
# test binary
146-
$(coverage_name) : $(binary_name)
146+
$(coverage_name) : $(binary_name) dump_test.go exec_test.go main_test.go
147147
@$(go) test -cover -vet all -coverprofile=$@
148148

149149
.PHONY : test-coverage

main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ func getAppFlags() []cli.Flag {
106106
Usage: "Kubernetes namespace to operate in",
107107
EnvVars: []string{"PLUGIN_NAMESPACE"},
108108
},
109+
&cli.BoolFlag{
110+
Name: "create-namespace",
111+
Usage: "automatically create a Namespace resource when a 'namespace' value is specified",
112+
EnvVars: []string{"PLUGIN_CREATE_NAMESPACE"},
113+
Value: true,
114+
},
109115
&cli.StringFlag{
110116
Name: "kube-template",
111117
Usage: "template for Kubernetes resources, e.g. Deployments",
@@ -710,6 +716,10 @@ func setNamespace(c *cli.Context, project string, runner Runner) error {
710716
return fmt.Errorf("Error: %s\n", err)
711717
}
712718

719+
if !c.Bool("create-namespace") {
720+
return nil
721+
}
722+
713723
// Write the namespace manifest to a tmp file for application.
714724
resource := fmt.Sprintf(nsTemplate, namespace)
715725

main_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ func TestSetNamespace(t *testing.T) {
447447
set.String("cluster", "cluster-0", "")
448448
set.String("namespace", "test-ns", "")
449449
set.Bool("dry-run", false, "")
450+
set.Bool("create-namespace", true, "")
450451
c := cli.NewContext(nil, set, nil)
451452

452453
testRunner := new(MockedRunner)
@@ -462,6 +463,7 @@ func TestSetNamespace(t *testing.T) {
462463
set.String("cluster", "regional-cluster", "")
463464
set.String("namespace", "test-ns", "")
464465
set.Bool("dry-run", false, "")
466+
set.Bool("create-namespace", true, "")
465467
c = cli.NewContext(nil, set, nil)
466468

467469
testRunner = new(MockedRunner)
@@ -481,6 +483,7 @@ func TestSetNamespace(t *testing.T) {
481483
set.String("cluster", "cluster-0", "")
482484
set.String("namespace", "Feature/1892-TEST-NS", "")
483485
set.Bool("dry-run", true, "")
486+
set.Bool("create-namespace", true, "")
484487
c = cli.NewContext(nil, set, nil)
485488

486489
testRunner = new(MockedRunner)
@@ -489,6 +492,21 @@ func TestSetNamespace(t *testing.T) {
489492
err = setNamespace(c, "test-project", testRunner)
490493
testRunner.AssertExpectations(t)
491494
assert.NoError(t, err)
495+
496+
// Opt-out of auto namespace creation
497+
set = flag.NewFlagSet("no-create-namespace-set", 0)
498+
set.String("zone", "us-east1-b", "")
499+
set.String("cluster", "cluster-0", "")
500+
set.String("namespace", "Feature/1892-TEST-NS", "")
501+
set.Bool("dry-run", false, "")
502+
set.Bool("create-namespace", false, "")
503+
c = cli.NewContext(nil, set, nil)
504+
505+
testRunner = new(MockedRunner)
506+
testRunner.On("Run", []string{"kubectl", "config", "set-context", "gke_test-project_us-east1-b_cluster-0", "--namespace", "feature-1892-test-ns"}).Return(nil)
507+
err = setNamespace(c, "test-project", testRunner)
508+
testRunner.AssertExpectations(t)
509+
assert.NoError(t, err)
492510
}
493511

494512
func TestApplyManifests(t *testing.T) {

0 commit comments

Comments
 (0)