Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I delete environment variable with kustomize?

I want to remove a few environment variables in a container with kustomize? Is that possible? When I patch, it just adds as you may know.

If it's not possible, can we replace environment variable name, and secret key name/key pair all together?

  containers:
    - name: container1
      env:
      - name: NAMESPACE
        valueFrom:
          secretKeyRef:
            name: x
            key: y

Any help on this will be appreciated! Thanks!

like image 531
cosmos-1905-14 Avatar asked Jul 20 '26 06:07

cosmos-1905-14


1 Answers

If you're looking remove that NAMESPACE variable from the manifest, you can use the special $patch: delete directive to do so.

If I start with this Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
  template:
    spec:
      containers:
        - name: example
          image: docker.io/traefik/whoami:latest
          env:
            - name: ENV_VAR_1
              valueFrom:
                secretKeyRef:
                  name: someSecret
                  key: someKeyName
            - name: ENV_VAR_2
              value: example-value

If I write in my kustomization.yaml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml

patches:
  - patch: |
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: example
      spec:
        template:
          spec:
            containers:
              - name: example
                env:
                  - name: ENV_VAR_1
                    $patch: delete

Then the output of kustomize build is:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
  template:
    spec:
      containers:
      - env:
        - name: ENV_VAR_2
          value: example-value
        image: docker.io/traefik/whoami:latest
        name: example

Using a strategic merge patch like this has an advantage over a JSONPatch style patch like Nijat's answer because it doesn't depend on the order in which the environment variables are defined.

like image 148
larsks Avatar answered Jul 26 '26 05:07

larsks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!