Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Predefined Dynamic namespaces #23

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,7 +55,6 @@ install:
manifests:
- /cnab/app/manifests/hello
wait: true

```

#### Upgrade Action
Expand All @@ -64,7 +66,6 @@ upgrade:
manifests:
- /cnab/app/manifests/hello
wait: true

```

#### Uninstall Action
Expand All @@ -76,7 +77,6 @@ uninstall:
manifests:
- /cnab/app/manifests/hello
wait: true

```

#### Outputs
Expand All @@ -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
```
26 changes: 25 additions & 1 deletion pkg/kubernetes/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kubernetes
import (
"bytes"
"fmt"
"strings"
"text/template"

"get.porter.sh/porter/pkg/exec/builder"
Expand All @@ -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.
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
17 changes: 17 additions & 0 deletions pkg/kubernetes/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
6 changes: 6 additions & 0 deletions pkg/kubernetes/testdata/build-input-with-namespaces.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
config:
namespaces:
- my-namespace-1
- my-namespace-2
- my-namespace-3
- my-namespace-4