25

I run

kubectl edit deployment

to change the version of one of my pods (this commands opens a temp file in my text editor and then I usually edit and close this temp file) and even before I close this temp file in my text editor I can see the following note in my bash.

Edit cancelled, no changes made.

It was OK before I installed fish and I tried to switch to bash but it doesn't help either.

How can I fix it?

A. Karnaval
  • 727
  • 2
  • 8
  • 12

4 Answers4

22

Things like this are most likely caused by it opening an editor that forks off instead of staying.

That means you'll want to set $EDITOR to an editor that does wait. E.g. nano, vim or emacs should work, and e.g. if you use sublime text you'll have to use subl -w to explicitly tell it to wait.

It's not quite clear which shell you're running at the moment. If it's bash, run export EDITOR="subl -w", in fish run set -gx EDITOR subl -w (or "subl -w" if you use fish < 3.0).

faho
  • 14,470
  • 2
  • 37
  • 47
  • 3
    This works for VSCode - `KUBE_EDITOR="code -w" kubectl edit ...` – billett Mar 03 '20 at 16:51
  • @billett I still get the same error, even after restarting powershell on win. Do you know what might be wrong? – pablete Sep 24 '20 at 13:23
  • after executing the command, what operation needs to be performed once we edit the file and saved – Spartan May 13 '22 at 07:20
  • @Spartan after that you need to close the editor, because that is what the command is waiting for. – faho May 13 '22 at 19:09
  • Thanks for the quick reply but unfortunately the edit operation dint get executed after closing the file :( – Spartan May 15 '22 at 04:31
7

A refinement to the ample answer provided by @faho.

An approach with the $EDITOR variable achieves the goal but changes the default command-line editor. This might affect other programs dependent on this setting (e.g. crontab, edquota).

It'd be better to rely on the $KUBE_EDITOR variable. For example for the one-time use you might try:

KUBE_EDITOR="nano" kubectl edit deploy/hello-world

(Please see Editing Resources)

mebius99
  • 2,495
  • 1
  • 5
  • 9
  • In this case, $EDITOR is faulty for *most things*. A $EDITOR that does not stop will also confuse e.g. git, so you shouldn't use one. – faho Jul 01 '19 at 18:38
  • If you really cannot use editor, you can also just try patching deployment. See https://stackoverflow.com/questions/36920171/how-can-i-edit-a-deployment-without-modify-the-file-manually – cwhsu Aug 11 '19 at 02:44
  • This works fine. Tested on xubuntu VM via mobaxterm – sam Dec 12 '21 at 18:36
4

With vim, when you try to save it saves an edited copy which is specified in /tmp/ path, along with the error message, when you quit the editor.

This is equivalent to using get the resource, edit it in text editor, and then apply the resource with the updated version:

kubectl get deployment my-nginx -o yaml > /tmp/nginx.yaml
vim /tmp/nginx.yaml


kubectl apply -f /tmp/nginx.yaml
deployment.apps/my-nginx configured

then remove the file

rm /tmp/nginx.yaml

So basically use apply on the file saved in the /tmp/<file.yaml>

Petronella
  • 2,327
  • 1
  • 15
  • 24
1

This issue may also happen when changes made by you are not picked by an kubectl eg. because of incorrect YAML.

  • Please make other change that you are sure of.
  • After saving check if still getting the same problem

Example issue replication:

  • the spec.selector.app: xxx" is invalid as it is duplicated;
  • the last one will remain; the first will be ignored.
  • so if you just added the first one - it no changes will be made.
spec:
  clusterIP: 10.152.183.151
  clusterIPs:
  - 10.152.183.151
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: xxx
    app: rng
Witold Kaczurba
  • 9,845
  • 3
  • 58
  • 67