40

I'm looking for a way to export a yaml file from a deployed component but without the cluster specific information.

kubectl get MYOBJECT --export -o yaml > my.yaml

but since "export" is now deprecated (since 1.14 and should normally disappear in 1.18 (didn't find it in changelog), what would be an alternative ?

thanks

Bobbob601
  • 433
  • 1
  • 4
  • 6

11 Answers11

14

Using JQ does the trick.

kubectl get secret <secretname> -ojson | jq 'del(.metadata.namespace,.metadata.resourceVersion,.metadata.uid) | .metadata.creationTimestamp=null'

produces exactly the same JSON as

kubectl get secret <secretname> -ojson --export
Shane Bishop
  • 3,905
  • 4
  • 17
  • 47
adel-s
  • 137
  • 1
  • 6
  • 1
    here example with more cleanup and change name. `kubectl get secret my-source-secret -n -my-source-ns -ojson | jq 'del(.metadata.namespace,.metadata.resourceVersion,.metadata.uid,.metadata.selfLink,.metadata.managedFields,.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration") | .metadata.creationTimestamp=null | .metadata.name="my-new-secret-name"' | kubectl apply -n target-ns -f -` – Tilo Sep 16 '21 at 17:24
11

If you want to use YAML input / output, you can use yq.

This did the trick for me, add or remove filters as appropriate for you:

kubectl get secret "my_secret" -n "my_namespace" --context "my_context" -o yaml \
    | yq d - 'metadata.resourceVersion' \
    | yq d - 'metadata.uid' \
    | yq d - 'metadata.annotations' \
    | yq d - 'metadata.creationTimestamp' \
    | yq d - 'metadata.selfLink'
Erik Živković
  • 4,867
  • 2
  • 35
  • 53
  • might as well delete the entire metadata and do a write of a new value – Sa'ad Sep 30 '20 at 06:56
  • 1
    add this line : ``` | yq d - 'metadata.managedFields' ``` – Serge Hartmann Nov 10 '20 at 15:47
  • 3
    If you are like me and got yq 4 the new syntax is as follows: kubectl get secret "my_secret" --namespace="my_namespace" -o yaml \ | yq e 'del(.metadata.resourceVersion)' - \ | yq e 'del(.metadata.uid)' - \ | yq e 'del(.metadata.annotations)' - \ | yq e 'del(.metadata.creationTimestamp)' - \ | yq e 'del(.metadata.selfLink)' - \ | yq e 'del(.metadata.managedFields)' - \ – Conor Apr 23 '21 at 18:37
11

Finally an easy to use tool has been created: https://github.com/itaysk/kubectl-neat

You could easily install all it as a kubectl krew plugin:

kubectl krew install neat

Usage as well is pretty simple

kubectl get pod mypod -o yaml | kubectl neat
toschneck
  • 770
  • 7
  • 12
9

There is no consistent way to do this since there is no overall guidelines about defaulting and other live data clean up. That is why it was deprecated. You should keep your source files in git or similar.

coderanger
  • 52,400
  • 4
  • 52
  • 75
7

Currently the one option is to do -o yaml or -o json and remove the unnecessary fields

hoque
  • 5,735
  • 1
  • 19
  • 29
  • 1
    Indeed doing `kubectl get MYOBJECT -o yaml > my.yaml` instead of `kubectl get MYOBJECT --export -o yaml > my.yaml` seems to produce almost the same output except for a few additional fields which should not do any harm. – finrod Jun 22 '20 at 09:53
5

Another option is to make use of the annotation field kubectl.kubernetes.io/last-applied-configuration which holds the resource initial applied configuraiton without auto-generated fields.

Example:

kubectl get <resource kind> <resource name> -o yaml | \
yq r - 'metadata.annotations."kubectl.kubernetes.io/last-applied-configuration"'
Marcelo
  • 2,245
  • 4
  • 20
  • 24
4

For anyone using yq v4.x you can do the following to get what you need:

kubectl get <resource> -n <namespace> <resource-name> -o yaml \
  | yq eval 'del(.metadata.resourceVersion, .metadata.uid, .metadata.annotations, .metadata.creationTimestamp, .metadata.selfLink, .metadata.managedFields)' -
Grant
  • 811
  • 1
  • 8
  • 18
0

Based on the above input, I created a short at our fubectl project: https://github.com/kubermatic/fubectl/pull/58

hopefully it helps also for others:

kget-ex RESOURCE > export.yaml
toschneck
  • 770
  • 7
  • 12
0

Export is deprecated in latest version of Kube or Openshift. We can directly do it like below

 oc get virtualservices -o yaml > project.yaml
 oc get routes -o yaml > project.yaml
Nilesh Kumar
  • 109
  • 1
  • 5
0

solution with ZERO dependencies to any 3rd-party tool:

kubectl -n $ns get [resourcetype] [resourcename] |\
 sed '/^  uid: /d; /^  resourceVersion: /d; /^  creationTimestamp: /d; /^  selfLink: /d; /^status:$/Q;'

Note: Works only with individual resources

Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
-2

Use yq to fetch the "last-applied-configuration" field (json format), then convert it to yaml.

 kbprod get deploy xxx -o yaml | yq e '.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration"' | yq -p json -o yaml
acala
  • 332
  • 3
  • 11