Hello, probably not a Kubernetes question. But it is related: Why is it we can . . .

Timothy Ng:
Hello, probably not a Kubernetes question. But it is related:

Why is it we can output yaml code into another yaml file via “kubectl run testing --image=nginx --dry-run=client -o yaml > testing.yaml”.

But if we do this (Picture), there’s a Vim error?

Also, is there a way to copy the code directly into another yaml file then? Without copying via Vi/Vim? Thank you!
image.png

greg:
The kubectl edit command opens the resource in a file editor (whatever is defined in the environment variables KUBE_EDITOR, or EDITOR, with fall
back to ‘vi’ for Linux or ‘notepad’ for Windows). The > notation is used to redirect a command’s standard output. So kubectl edit pod testing -o yaml > testing.yaml opens the resource testing in Vim, then the standard output for Vim gets redirected to a file. The warning is because normally standard output for Vim is to a terminal, and it really does not make sense to redirect the standard output of an interactive editor like Vim to a file.

If you type :q you can see that your standard input is directed at Vim, and it will exit. You should also see that the file was created with what you would normally see in your terminal if you had not redirected the output to a file (with added control characters).

> kubectl run test --image=nginx
pod/test created

> kubectl edit pod test > test.yml
Vim: Warning: Output is not to a terminal
:qEdit cancelled, no changes made.

> cat -A test.yml
^[[?1049h^[[22;0;0t^[[>4;2m^[[?1h^[=^[[?2004h^[[1;56r^[[?12h^[[?12l^[[22;2t^[[22;1t^[[27m^[[23m^[[29m^[[m^[[H^[[2J^[[?25l^[[56;1H"/tmp/kubectl-edit-2077267105.yaml" 102L, 2774C^[[1;1H^[[34m# Please edit the object below. Lines beginning with a '#' will be ignored,^M$
# and an empty file will abort the edit. If an error occurs while saving this file will be^M$
# reopened with the relevant failures.^M$
#^[[m^M$
^[[36mapiVersion^[[m^[[35m:^[[m v1^M$
^[[36mkind^[[m^[[35m:^[[m Pod^M$
^[[36mmetadata^[[m^[[35m:^[[m^M$
  ^[[36mcreationTimestamp^[[m^[[35m:^[[m ^[[31m"2022-12-14T03:21:24Z"^[[m^M$
  ^[[36mlabels^[[m^[[35m:^[[m^M$
    ^[[36mrun^[[m^[[35m:^[[m test^M$
<<omitted>>

If you want to redirect a resource to a file, use the kubectl get command:

> kubectl get pod test -o yaml > test.yml

> cat test.yml   
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2022-12-14T03:21:24Z"
  labels:
    run: test
<<omitted>>

unnivkn:
Hi @Timothy Ng fyr:


greg:
I think you’ve misread or misunderstood that option. The --save-config option saves the modified config in the object’s annotation.

From the documentation:
--save-config[=false]: If true, the configuration of current object will be saved in its annotation. This is useful when you want to perform kubectl apply on this object in the future.

The creation of the temp file is because you tried to modify an attribute that cannot be modified.

Again from the docs:
> In the event an error occurs while updating, a temporary file will be created on disk that contains your unapplied changes.

unnivkn:
That’s correct @greg I intentionally created new pod with temp file :slightly_smiling_face:

greg:
I know, but you seem to be highlighting the --save-config option when it has no bearing on what you did.

unnivkn:
okie… I just want to indicate k edit is available for pods and it can be implemented one or the other way, may be direct or indirect way. In fact I haven’t used it for pods. So just tried that option in the help command :blush:

greg:
Ok, gotcha

Timothy Ng:
Thanks for the input Greg! Makes sense. Thank you too Unnivkn for the additional input. Just played around with the commands and everything checks out!