The ‘export’ flag in kubectl was deprecated in 1.18, but you still need a way to dump the yaml of Kubernetes objects, often for the purpose of reapplying changes.
The problem with doing a simple ‘get’ using a yaml output like below is that additional internal accounting attributes like ‘selfLink’, ‘creationTimestamp’, ‘uid’, will be present in the capture which makes it non-portable and breaks reapply.
# captures all elements and attributes, even internal accounting kubectl get pod <podname> -n <namespace> -o=yaml > my-object.yaml
There are utilities such as kubectl-neat that can be used, but you can also use the jq utility to remove these elements. Below is the jq command to delete the elements, then yq to convert back to yaml.
kubectl get pod <podname> -n <namespace> -o=json | jq 'del(.metadata.resourceVersion,.metadata.uid,.metadata.selfLink,.metadata.creationTimestamp,.metadata.annotations,.metadata.generation,.metadata.ownerReferences)' | yq eval - -P > my-object.cleaned.yaml
This would make it easy to reapply the yaml, or apply in a different cluster.
REFERENCES
stackoverflow, stripping yaml elements with jq
https://github.com/kubernetes/kubernetes/issues/50454
reddit.com, kubectl export deprecated in 1.18
NOTES
older yq versions do not have eval or ‘P’ flag for pretty print of yaml
# newer versions from mikefarah yq eval - -P # older versions yq read -