Use variable in a patchesJson6902 of a kustomization.yaml file

3/30/2021

I would like to set the name field in a Namespace resource and also replace the namespace field in a Deployment resource with the same value, for example my-namespace.

Here is kustomization.json:

namespace: <NAMESPACE>
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
patchesJson6902:
- patch: |-
    - op: replace
      path: /metadata/name
      value: <NAMESPACE>
  target:
    kind: Namespace
    name: system
    version: v1
resources:
- manager.yaml

and manager.yaml:

apiVersion: v1
kind: Namespace
metadata:
  labels:
    control-plane: controller-manager
  name: system
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: controller-manager
  namespace: system
spec:
  selector:
    matchLabels:
      control-plane: controller-manager
  replicas: 1
  template:
    metadata:
      labels:
        control-plane: controller-manager
    spec:
      containers:
      - command:
        - /manager
        args:
        - --enable-leader-election
        image: controller:latest
        name: manager

I tried using kustomize edit set namespace my-namespace && kustomize build, but it only changes the namespace field in the Deployment object.

Is there a way to change both field without using sed, in 'pure' kustomize and without having to change manually <NAMESPACE> value in kustomization.json?

-- Fabrice Jammes
kubernetes
kustomize

1 Answer

3/30/2021

Is there a way to change both field without using sed, in 'pure' kustomize and without having to change manually value in kustomization.json?

I managed to achieve somewhat similar with the following configuration:

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: my-namespace
resources:
- deployment.yaml
depyloment.yaml
---
apiVersion: v1
kind: Namespace
metadata:
  name: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.14.2
        name: nginx
        ports:
        - containerPort: 80

And here is the output of the command that you used:

➜  kustomize kustomize edit set namespace my-namespace7 && kustomize build .  
apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace7
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: my-namespace7
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.14.2
        name: nginx
        ports:
        - containerPort: 80

What is happening here is that once you set the namespace globally in kustomization.yaml it will apply it to your targets which looks to me that looks an easier way to achieve what you want.

I cannot test your config without manager_patch.yaml content. If you wish to go with your way further you will have update the question with the file content.

-- acid_fuji
Source: StackOverflow