diff --git a/README.md b/README.md index 74e883c..f3b2197 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,9 @@ porter mixin install kubernetes --version $VERSION --url https://github.com/getp ```yaml - kubernetes: clientVersion: v1.15.5 + namespaces: + - my-namespace-1 + - my-namespace-2 ``` ### Mixin Actions Syntax @@ -52,7 +55,6 @@ install: manifests: - /cnab/app/manifests/hello wait: true - ``` #### Upgrade Action @@ -64,7 +66,6 @@ upgrade: manifests: - /cnab/app/manifests/hello wait: true - ``` #### Uninstall Action @@ -76,7 +77,6 @@ uninstall: manifests: - /cnab/app/manifests/hello wait: true - ``` #### Outputs @@ -85,9 +85,9 @@ The mixin supports extracting resource metadata from Kubernetes as outputs. ```yaml outputs: - - name: NAME - resourceType: RESOURCE_TYPE - resourceName: RESOURCE_TYPE_NAME - namespace: NAMESPACE - jsonPath: JSON_PATH_DEFINITION + - name: NAME + resourceType: RESOURCE_TYPE + resourceName: RESOURCE_TYPE_NAME + namespace: NAMESPACE + jsonPath: JSON_PATH_DEFINITION ``` diff --git a/pkg/kubernetes/build.go b/pkg/kubernetes/build.go index b777fc7..600fd00 100644 --- a/pkg/kubernetes/build.go +++ b/pkg/kubernetes/build.go @@ -3,6 +3,7 @@ package kubernetes import ( "bytes" "fmt" + "strings" "text/template" "get.porter.sh/porter/pkg/exec/builder" @@ -24,7 +25,8 @@ chmod a+x /usr/local/bin/kubectl ) type MixinConfig struct { - ClientVersion string `yaml:"clientVersion,omitempty"` + ClientVersion string `yaml:"clientVersion,omitempty"` + Namespaces []string `yaml:"namespaces,omitempty"` } // BuildInput represents stdin passed to the mixin for the build command. @@ -74,6 +76,16 @@ func (m *Mixin) Build() error { return err } + // Go through Namespaces if defined + if len(input.Config.Namespaces) > 0 { + namespacesCommand, err := getNamespacesCommand(input.Config.Namespaces) + if err != nil && m.Debug { + fmt.Fprintf(m.Err, "DEBUG: addition of namespace failed: %s\n", err.Error()) + } else { + fmt.Fprintf(m.Out, strings.Join(namespacesCommand, " ")) + } + } + return nil } @@ -91,3 +103,15 @@ func validate(clientVersion, constraint string) (bool, error) { return c.Check(v), nil } + +func getNamespacesCommand(namespaces []string) (namespaceCommand []string, err error) { + + var commandBuilder []string + // + for _, namespace := range namespaces { + commandBuilder = append(commandBuilder, "kubectl", "create", "namespace", namespace, "||", "true;") + } + commandBuilder = append([]string{"\nRUN"}, commandBuilder...) + + return commandBuilder, nil +} diff --git a/pkg/kubernetes/build_test.go b/pkg/kubernetes/build_test.go index 60c3a50..ec76a15 100644 --- a/pkg/kubernetes/build_test.go +++ b/pkg/kubernetes/build_test.go @@ -45,4 +45,21 @@ func TestMixin_Build(t *testing.T) { gotOutput := m.TestContext.GetOutput() assert.Equal(t, wantOutput, gotOutput) }) + + t.Run("build with custom Kubernetes version", func(t *testing.T) { + b, err := ioutil.ReadFile("testdata/build-input-with-namespaces.yaml") + require.NoError(t, err) + + m := NewTestMixin(t) + m.Debug = false + m.In = bytes.NewReader(b) + err = m.Build() + require.NoError(t, err) + + wantOutput := fmt.Sprintf(buildOutputTemplate, "v1.15.5") + + "\nRUN kubectl create namespace my-namespace-1 || true; kubectl create namespace my-namespace-2 || true; " + + "kubectl create namespace my-namespace-3 || true; kubectl create namespace my-namespace-4 || true;" + gotOutput := m.TestContext.GetOutput() + assert.Equal(t, wantOutput, gotOutput) + }) } diff --git a/pkg/kubernetes/testdata/build-input-with-namespaces.yaml b/pkg/kubernetes/testdata/build-input-with-namespaces.yaml new file mode 100644 index 0000000..b060643 --- /dev/null +++ b/pkg/kubernetes/testdata/build-input-with-namespaces.yaml @@ -0,0 +1,6 @@ +config: + namespaces: + - my-namespace-1 + - my-namespace-2 + - my-namespace-3 + - my-namespace-4 \ No newline at end of file