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!
CodePudding user response:
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.
CodePudding user response:
You can delete an environment variable from a Kubernetes resource by removing the corresponding field from the resource's YAML definition. In kustomize, you can use a patch to modify the resource definition.
Here's an example of how you could delete an environment variable in a Deployment resource:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
template:
spec:
containers:
- name: my-container
env:
- name: MY_VAR
value: "some value"
To delete the environment variable, you can create a patch in a kustomization file that removes the corresponding field:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
patches:
- target:
group: apps
version: v1
kind: Deployment
name: my-deployment
patch: |-
- op: remove
path: "/spec/template/spec/containers/0/env/0"
You can then apply the kustomization to your cluster using kubectl apply -k . This will remove the environment variable from the Deployment resource in your cluster