Skip to content

Instantly share code, notes, and snippets.

@suhas316380
Last active December 11, 2021 00:44
Show Gist options
  • Select an option

  • Save suhas316380/2182be680ba0bc27ac0af351e7d42c34 to your computer and use it in GitHub Desktop.

Select an option

Save suhas316380/2182be680ba0bc27ac0af351e7d42c34 to your computer and use it in GitHub Desktop.
Remove unwanted info from Kubernetes manifests to make them more readable

This can be achieved using 2 ways:

  1. Using kubectl-neat: kubectl-neat
  2. using jq/yq

Approach 1: kubectl-neat

  1. 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
)
  1. Set the following in ~/.bashrc
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
source <(kubectl completion bash) # Optional
  1. Install plugins command syntax: kubectl krew install PLUGIN_NAME
  2. Install kubectl-neat: kubectl krew install neat
  3. Test: kubectl get deploy -n <your_namespace> <your_deployment> -o yaml | kubectl neat

Approach 2: Using jq/yq

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment