Kustomize: Allow Var for Dynamic name in Namespace

Created on 22 Aug 2019  路  3Comments  路  Source: kubernetes-sigs/kustomize

I'm using kustomize edit set namespace "$CUSTOMER_ID" to have all of my kubernetes resources to be put into the same namespace. Works great.

But I'm struggling with actually creating that namespace with-in kustomize. I read there were some attempts at this by dynamic creation of namespaces.

500

what I'm thinking is would it be possible to allowvarreference.go to

- path: metadata.name
  kind: Namespace

Then I would have to additionally create a config map...

kustomize edit add configmap source-vars --from-literal CUSTOMER_ID="$CUSTOMER_ID"

and in my kustomization.yaml

vars:
  - name: CUSTOMER_ID
    objref:
      kind: ConfigMap
      name: source-vars
      apiVersion: v1
    fieldref:
      fieldpath: data.CUSTOMER_ID

Most helpful comment

You can do this by setting up a custom transformer config in your kustomization.

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ns.yaml
vars:
- fieldref:
    fieldPath: data.CUSTOMER_ID
  name: CUSTOMER_ID
  objref:
    apiVersion: v1
    kind: ConfigMap
    name: source-vars
configurations:
- config.yaml

config.yaml

varReference:
- path: metadata/name
  kind: Namespace

ns.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: $(CUSTOMER_ID)
$ CUSTOMER_ID=cust1234
$ kustomize edit add configmap source-vars --from-literal CUSTOMER_ID="$CUSTOMER_ID"
$ kustomize edit set namespace "$CUSTOMER_ID"
$ kustomize build .
apiVersion: v1
kind: Namespace
metadata:
  name: cust1234
---
apiVersion: v1
data:
  CUSTOMER_ID: "cust1234"
kind: ConfigMap
metadata:
  name: source-vars-k59btk8td4
  namespace: cust1234

All 3 comments

You can do this by setting up a custom transformer config in your kustomization.

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ns.yaml
vars:
- fieldref:
    fieldPath: data.CUSTOMER_ID
  name: CUSTOMER_ID
  objref:
    apiVersion: v1
    kind: ConfigMap
    name: source-vars
configurations:
- config.yaml

config.yaml

varReference:
- path: metadata/name
  kind: Namespace

ns.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: $(CUSTOMER_ID)
$ CUSTOMER_ID=cust1234
$ kustomize edit add configmap source-vars --from-literal CUSTOMER_ID="$CUSTOMER_ID"
$ kustomize edit set namespace "$CUSTOMER_ID"
$ kustomize build .
apiVersion: v1
kind: Namespace
metadata:
  name: cust1234
---
apiVersion: v1
data:
  CUSTOMER_ID: "cust1234"
kind: ConfigMap
metadata:
  name: source-vars-k59btk8td4
  namespace: cust1234

Thank you for the fantastic example.

Is there a way to use namespace in Kustomization directly?

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

namespace: xxx
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mgoodness picture mgoodness  路  4Comments

bcbrockway picture bcbrockway  路  5Comments

davidsbond picture davidsbond  路  3Comments

Liujingfang1 picture Liujingfang1  路  6Comments

nabadger picture nabadger  路  4Comments