Skip to content

Commit 6b2ddb5

Browse files
committed
wip: predefine namespaces
1 parent d4b4ef2 commit 6b2ddb5

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ porter mixin install kubernetes --version $VERSION --url https://github.com/getp
3939
```yaml
4040
- kubernetes:
4141
clientVersion: v1.15.5
42+
namespaces:
43+
- my-namespace-1
44+
- my-namespace-2
4245
```
4346
4447
### Mixin Actions Syntax
@@ -52,7 +55,6 @@ install:
5255
manifests:
5356
- /cnab/app/manifests/hello
5457
wait: true
55-
5658
```
5759
5860
#### Upgrade Action
@@ -64,7 +66,6 @@ upgrade:
6466
manifests:
6567
- /cnab/app/manifests/hello
6668
wait: true
67-
6869
```
6970
7071
#### Uninstall Action
@@ -76,7 +77,6 @@ uninstall:
7677
manifests:
7778
- /cnab/app/manifests/hello
7879
wait: true
79-
8080
```
8181
8282
#### Outputs
@@ -85,9 +85,9 @@ The mixin supports extracting resource metadata from Kubernetes as outputs.
8585
8686
```yaml
8787
outputs:
88-
- name: NAME
89-
resourceType: RESOURCE_TYPE
90-
resourceName: RESOURCE_TYPE_NAME
91-
namespace: NAMESPACE
92-
jsonPath: JSON_PATH_DEFINITION
88+
- name: NAME
89+
resourceType: RESOURCE_TYPE
90+
resourceName: RESOURCE_TYPE_NAME
91+
namespace: NAMESPACE
92+
jsonPath: JSON_PATH_DEFINITION
9393
```

pkg/kubernetes/build.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package kubernetes
33
import (
44
"bytes"
55
"fmt"
6+
"strings"
67
"text/template"
78

89
"get.porter.sh/porter/pkg/exec/builder"
@@ -24,7 +25,8 @@ chmod a+x /usr/local/bin/kubectl
2425
)
2526

2627
type MixinConfig struct {
27-
ClientVersion string `yaml:"clientVersion,omitempty"`
28+
ClientVersion string `yaml:"clientVersion,omitempty"`
29+
Namespaces []string `yaml:"namespaces,omitempty"`
2830
}
2931

3032
// BuildInput represents stdin passed to the mixin for the build command.
@@ -74,6 +76,16 @@ func (m *Mixin) Build() error {
7476
return err
7577
}
7678

79+
// Go through Namespaces if defined
80+
if len(input.Config.Namespaces) > 0 {
81+
namespacesCommand, err := getNamespacesCommand(input.Config.Namespaces)
82+
if err != nil && m.Debug {
83+
fmt.Fprintf(m.Err, "DEBUG: addition of namespace failed: %s\n", err.Error())
84+
} else {
85+
fmt.Fprintf(m.Out, strings.Join(namespacesCommand, " "))
86+
}
87+
}
88+
7789
return nil
7890
}
7991

pkg/kubernetes/build_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,21 @@ func TestMixin_Build(t *testing.T) {
4545
gotOutput := m.TestContext.GetOutput()
4646
assert.Equal(t, wantOutput, gotOutput)
4747
})
48+
49+
t.Run("build with custom Kubernetes version", func(t *testing.T) {
50+
b, err := ioutil.ReadFile("testdata/build-input-with-namespaces.yaml")
51+
require.NoError(t, err)
52+
53+
m := NewTestMixin(t)
54+
m.Debug = false
55+
m.In = bytes.NewReader(b)
56+
err = m.Build()
57+
require.NoError(t, err)
58+
59+
wantOutput := fmt.Sprintf(buildOutputTemplate, "v1.15.5") +
60+
"\nRUN kubectl create namespace my-namespace-1 || true; kubectl create namespace my-namespace-2 || true; " +
61+
"kubectl create namespace my-namespace-3 || true; kubectl create namespace my-namespace-4 || true;"
62+
gotOutput := m.TestContext.GetOutput()
63+
assert.Equal(t, wantOutput, gotOutput)
64+
})
4865
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
config:
2+
namespaces:
3+
- my-namespace-1
4+
- my-namespace-2
5+
- my-namespace-3
6+
- my-namespace-4

0 commit comments

Comments
 (0)