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.
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
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
Most helpful comment
You can do this by setting up a custom transformer config in your kustomization.
kustomization.yaml
config.yaml
ns.yaml