This can be achieved using 2 ways:
- Using kubectl-neat: kubectl-neat
- using jq/yq
- Run the following from your terminal to Install krew:
(
set -x; cd "$(mktemp -d)" &&
OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
KREW="krew-${OS}_${ARCH}" &&
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
tar zxvf "${KREW}.tar.gz" &&
./"${KREW}" install krew
)
- Set the following in ~/.bashrc
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
source <(kubectl completion bash) # Optional
- Install plugins command syntax:
kubectl krew install PLUGIN_NAME - Install kubectl-neat:
kubectl krew install neat - Test:
kubectl get deploy -n <your_namespace> <your_deployment> -o yaml | kubectl neat
kubectl_yaml() {
kubectl -o yaml "$@" \
| yq d - 'items[*].metadata.managedFields' \
| yq d - '.metadata.managedFields' \
| yq d - 'items[*].metadata.ownerReferences' \
| yq d - 'metadata.ownerReferences' \
| yq d - 'items[*].status' \
| yq d - 'status'
}
kubectl_json() {
kubectl -o json "$@" \
| jq 'del(.items[]?.metadata.managedFields)' \
| jq 'del(.metadata.managedFields)' \
| jq 'del(.items[]?.metadata.ownerReferences)' \
| jq 'del(.metadata.ownerReferences)' \
| jq 'del(.items[]?.status)' \
| jq 'del(.status)'
}
kubectl_yaml get pods
kubectl_json get service my-service