3

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!

cosmos-1905-14
  • 783
  • 2
  • 12
  • 23

1 Answers1

6

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.

larsks
  • 277,717
  • 41
  • 399
  • 399