How to edit a kubernetes resource from a shell script

Your command is missing a backtick. But even though you put it there, it won't work. The reason is because when you do kubectl edit ..., it edits the file on vim. I am not sure sed would work on vim though. Even though if it does, the output goes to a file, so you get the Vim: Warning: Output is not to a terminal error, which I don't know how to solve.

I would recommend you to get the file and save it. Replace the desired parameters and run it again:

kubectl get deploy tiller-deploy -n kube-system -o yaml > tiller.yaml && sed -i "s/automountServiceAccountToken:.*$/automountServiceAccountToken: true/g" tiller.yaml && kubectl replace -f tiller.yaml

I tried the command above and it worked.

Note: no need to add -n kube-system as the yaml file already contains the namespace.


I just found a less convoluted way of doing this:

KUBE_EDITOR="sed -i s/SOMETHING TO CHANGE/CHANGED/g" kubectl edit resource -n your-ns

Tags:

Shell

Sed

Kubectl