Kubernetes: export a clean yaml manifest that can be re-imported

It is easy to export the full manifest of an object from a Kubernetes cluster, but it will have extraneous accounting fields that not only make it hard to visually assess and compare to its original form, but can also cause a re-apply to fail.

Using a combination of the jq and yq utilities, we can export a clean yaml manifest using the syntax below.

kubectl get $type $name  -n $namespace -o=json | jq 'del(.metadata.resourceVersion,.metadata.uid,.metadata.selfLink,.metadata.creationTimestamp,.metadata.annotations,.metadata.generation,.metadata.ownerReferences,.status)' | yq eval . --prettyPrint

On Ubuntu, these utilities can be installed using:

sudo apt install jq -y
sudo snap install yq

The latest ‘yq‘ for linux can also be installed manually:

latest_yq_linux=$(curl -sL https://api.github.com/repos/mikefarah/yq/releases/latest | jq -r ".assets[].browser_download_url" | grep linux_amd64.tar.gz)
wget $latest_yq_linux
tar xvfz yq_linux_amd64.tar.gz
sudo cp yq_linux_amd64 /usr/local/bin/yq
sudo chown root:root /usr/local/bin/yq

REFERENCES

github yq project

NOTES

Pull key containing period using yq [1]

yq '.data."tls.crt"' tls.yaml