From 6ad5d9f55b30b6e138279ee113bfdaa230a2c50d Mon Sep 17 00:00:00 2001 From: luktom Date: Wed, 5 Dec 2018 11:18:57 +0100 Subject: [PATCH] Add namespace for config map and secret generators --- docs/kustomization.yaml | 7 ++++ .../configmapandsecret/configmapfactory.go | 1 + k8sdeps/configmapandsecret/secretfactory.go | 1 + .../expected.yaml | 33 +++++++++++++++++++ .../in/kustomization.yaml | 19 +++++++++++ .../in/password.txt | 1 + .../testcase-generators-namespace/test.yaml | 4 +++ pkg/types/kustomization.go | 6 ++++ 8 files changed, 72 insertions(+) create mode 100644 pkg/commands/build/testdata/testcase-generators-namespace/expected.yaml create mode 100644 pkg/commands/build/testdata/testcase-generators-namespace/in/kustomization.yaml create mode 100644 pkg/commands/build/testdata/testcase-generators-namespace/in/password.txt create mode 100644 pkg/commands/build/testdata/testcase-generators-namespace/test.yaml diff --git a/docs/kustomization.yaml b/docs/kustomization.yaml index acf26e2e6..9ada50dc2 100644 --- a/docs/kustomization.yaml +++ b/docs/kustomization.yaml @@ -94,6 +94,13 @@ secretGenerator: tls.crt: "cat secret/tls.cert" tls.key: "cat secret/tls.key" type: "kubernetes.io/tls" +- name: app-tls-namespaced + # you can define a namespace to generate secret in, defaults to: "default" + namspace: apps + commands: + tls.crt: "cat secret/tls.cert" + tls.key: "cat secret/tls.key" + type: "kubernetes.io/tls" - name: downloaded_secret # timeoutSeconds specifies the number of seconds to # wait for the commands below. It defaults to 5 seconds. diff --git a/k8sdeps/configmapandsecret/configmapfactory.go b/k8sdeps/configmapandsecret/configmapfactory.go index ff77e6fa4..71e932f8e 100644 --- a/k8sdeps/configmapandsecret/configmapfactory.go +++ b/k8sdeps/configmapandsecret/configmapfactory.go @@ -49,6 +49,7 @@ func (f *ConfigMapFactory) makeFreshConfigMap( cm.APIVersion = "v1" cm.Kind = "ConfigMap" cm.Name = args.Name + cm.Namespace = args.Namespace cm.Data = map[string]string{} return cm } diff --git a/k8sdeps/configmapandsecret/secretfactory.go b/k8sdeps/configmapandsecret/secretfactory.go index 2d8db576b..5ba3bba0c 100644 --- a/k8sdeps/configmapandsecret/secretfactory.go +++ b/k8sdeps/configmapandsecret/secretfactory.go @@ -52,6 +52,7 @@ func (f *SecretFactory) makeFreshSecret(args *types.SecretArgs) *corev1.Secret { s.APIVersion = "v1" s.Kind = "Secret" s.Name = args.Name + s.Namespace = args.Namespace s.Type = corev1.SecretType(args.Type) if s.Type == "" { s.Type = corev1.SecretTypeOpaque diff --git a/pkg/commands/build/testdata/testcase-generators-namespace/expected.yaml b/pkg/commands/build/testdata/testcase-generators-namespace/expected.yaml new file mode 100644 index 000000000..803b2a325 --- /dev/null +++ b/pkg/commands/build/testdata/testcase-generators-namespace/expected.yaml @@ -0,0 +1,33 @@ +apiVersion: v1 +data: + altGreeting: Good Morning from default namespace! + enableRisky: "false" +kind: ConfigMap +metadata: + name: the-map-4959m5tm6c +--- +apiVersion: v1 +data: + altGreeting: Good Morning from non-default namespace! + enableRisky: "false" +kind: ConfigMap +metadata: + name: the-non-default-namespace-map-b6h49k7mt8 + namespace: non-default +--- +apiVersion: v1 +data: + password.txt: dmVyeSRlY3JldA== +kind: Secret +metadata: + name: the-secret-cfbmct72tb +type: Opaque +--- +apiVersion: v1 +data: + password.txt: dmVyeSRlY3JldA== +kind: Secret +metadata: + name: the-non-default-namespace-secret-255294gd9d + namespace: non-default +type: Opaque diff --git a/pkg/commands/build/testdata/testcase-generators-namespace/in/kustomization.yaml b/pkg/commands/build/testdata/testcase-generators-namespace/in/kustomization.yaml new file mode 100644 index 000000000..b0a1f76df --- /dev/null +++ b/pkg/commands/build/testdata/testcase-generators-namespace/in/kustomization.yaml @@ -0,0 +1,19 @@ +configMapGenerator: +- name: the-non-default-namespace-map + namespace: non-default + literals: + - altGreeting=Good Morning from non-default namespace! + - enableRisky="false" +- name: the-map + literals: + - altGreeting=Good Morning from default namespace! + - enableRisky="false" + +secretGenerator: +- name: the-non-default-namespace-secret + namespace: non-default + commands: + password.txt: "cat password.txt" +- name: the-secret + commands: + password.txt: "cat password.txt" \ No newline at end of file diff --git a/pkg/commands/build/testdata/testcase-generators-namespace/in/password.txt b/pkg/commands/build/testdata/testcase-generators-namespace/in/password.txt new file mode 100644 index 000000000..0ceb1fc32 --- /dev/null +++ b/pkg/commands/build/testdata/testcase-generators-namespace/in/password.txt @@ -0,0 +1 @@ +very$ecret \ No newline at end of file diff --git a/pkg/commands/build/testdata/testcase-generators-namespace/test.yaml b/pkg/commands/build/testdata/testcase-generators-namespace/test.yaml new file mode 100644 index 000000000..bf13f64b6 --- /dev/null +++ b/pkg/commands/build/testdata/testcase-generators-namespace/test.yaml @@ -0,0 +1,4 @@ +description: generators-namespace +args: [] +filename: testdata/testcase-generators-namespace/in +expectedStdout: testdata/testcase-generators-namespace/expected.yaml diff --git a/pkg/types/kustomization.go b/pkg/types/kustomization.go index 945e2d5bd..8591ced4f 100644 --- a/pkg/types/kustomization.go +++ b/pkg/types/kustomization.go @@ -153,6 +153,9 @@ type ConfigMapArgs struct { // hash(content of configmap). Name string `json:"name,omitempty" yaml:"name,omitempty"` + // Namespace for the configmap, optional + Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` + // behavior of configmap, must be one of create, merge and replace // 'create': create a new one; // 'replace': replace the existing one; @@ -170,6 +173,9 @@ type SecretArgs struct { // hash(content of secret). Name string `json:"name,omitempty" yaml:"name,omitempty"` + // Namespace for the secret, optional + Namespace string `json:"namespace,omitempty" yaml:"namespace,omitempty"` + // behavior of secretGenerator, must be one of create, merge and replace // 'create': create a new one; // 'replace': replace the existing one;