kubectl patch: Is it possible to add multiple values to an array within a sinlge patch execution kubectl patch: Is it possible to add multiple values to an array within a sinlge patch execution kubernetes kubernetes

kubectl patch: Is it possible to add multiple values to an array within a sinlge patch execution


Below uses a single patch but it's not very DRY:

kubectl patch deployment <deployment-name> -n <namespace> --type "json" -p '[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-1"},{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-2"},{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"arg-3"}]'

I've been doing something similar for cert-manager to allow fully automated TLS:

kubectl patch deployment cert-manager -n cert-manager --type "json" -p '[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--default-issuer-name=letsencrypt-prod"},{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--default-issuer-kind=ClusterIssuer"},{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--default-issuer-group=cert-manager.io"}]'


You can use kubectl edit command to edit the resource.
Example usage:
kubectl edit deploy <deployment_name>

For more info, refer: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#edit.

Edit: There is a nasty way of doing it programmatically. You can pipe the yaml to python, alter the values you want to change and apply the new yaml. In your case, it would be something like,

kubectl get deploy <deploy_name> -o yaml | python -c 'import sys,yaml; yml = yaml.safe_load(sys.stdin); yml["spec"]["template"]["spec"]["containers"][0]["args"].extend(["newValue1", "newValue2"]); print(yaml.dump(yml));' | kubectl apply -f -

Obviously you want to do this only if there isn't any easier way to do it.