19

I went through the documentation to edit kubernetes resource using kubectl edit command. Once I execute the command, the file in YAML-format is opened in the editor where I can change the values as per requirement and save it. I am trying to execute these steps by means of sed. How can the following steps be achieved?

  1. Execute kubectl edit for a deployment resource
  2. Set a value from true to false (using sed)
  3. Save the changes

I tried to achieve this in the following way :

$ kubectl edit deployment tiller-deploy -n kube-system | \
   sed -i "s/\(automountServiceAccountToken:.*$\)/automountServiceAccountToken: true/g"`
kvantour
  • 25,269
  • 4
  • 47
  • 72
ambikanair
  • 4,004
  • 11
  • 43
  • 83

6 Answers6

16

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.

suren
  • 7,817
  • 1
  • 30
  • 51
11

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
Nebril
  • 3,153
  • 1
  • 33
  • 50
  • 1
    I got this error: there was a problem with the editor "sed -i s/role: webserver/role: apache2/g" – Andrew May 06 '22 at 07:10
  • @Andrew While KUBE_EDITOR environment variable with sed might bring you some convenience, be aware of the separator ('/'). Either you single quote it, e.g. KUBE_EDITOR="sed -i 's/role: webserver/role: apache2/g'" kubectl ..., or choose the separator other than '/', e.g. '%'. – Ming Hsieh Jun 20 '23 at 04:13
7

I automate through piping the commands through sed command without creating a temporary file. Take the below example, where I am replacing nameserver 8.8.8.8 with 1.1.1.1

$ kubectl -n kube-system get configmap/kube-dns -o yaml | sed "s/8.8.8.8/1.1.1.1/" | kubectl replace -f -
vikas027
  • 5,282
  • 4
  • 39
  • 51
4

An easy way to do this, just use kubectl-patch instead of sed.

$ kubectl patch deployment tiller-deploy -n kube-system --patch '{"map": {"to": {"the": {"key": {"automountServiceAccountToken": "true"}}}}}'
Andrew
  • 124
  • 1
  • 12
3

Thanks, @suren for giving what I really looking for, but you don't need to save it in a file. you can directly do kubectl replace using pipe operations

kubectl get deploy test-deploy  -o yaml | sed "s/find/replace/g" | kubectl replace -f -

Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
1

I don't know kubectl but doc seems to explain that it extract data, edit from an editor than send back, not sure sed pipe work in this case

if piping wokrs Don't use -i, you don't change a file in a pipe

kubectl edit deployment tiller-deploy -n kube-system | \
 sed 's/automountServiceAccountToken:.*$/automountServiceAccountToken: true/g'

if editing a file (and using group in sed)

kubectl edit deployment tiller-deploy -n kube-system > YourCOnfigFile && \
 sed -i 's/\(automountServiceAccountToken:\).*$/\1 true/g' YourConfigFile \
 && Some kubectl to send back YourConfigFile
NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
  • `kubectl edit deployment tiller-deploy -n kube-system > YourCOnfigFile` would throw an error – suren May 08 '18 at 09:40