Skip to content

Instantly share code, notes, and snippets.

@tcarreira
Created August 25, 2022 20:26
Show Gist options
  • Save tcarreira/81c73b2f38a7ac27dab330c71b91ba3c to your computer and use it in GitHub Desktop.
Save tcarreira/81c73b2f38a7ac27dab330c71b91ba3c to your computer and use it in GitHub Desktop.
kubectl get filter by annotation
# including this inside .bashrc and use the kfilter command on your shell
function escape_things() {
echo $1 | sed -E "s@([\.\/])@\\\\\1@g"
}
function kfilter() {
# kfilter -n <namespace> <annotation> <value> # search on a single namespace
# kfilter <annotation> <value> # all namespaces (takes longer)
if [ "_$1" = "_-n" ] ; then
namespace=("-n" "$2")
shift ; shift
else
namespace=("-A")
fi
ktype="$1" ; shift
annotation_name="$(escape_things "$1")"
annotation_value="$2"
kubectl get "${ktype}" "${namespace[@]}" \
"-o=jsonpath='{.items[?(@.metadata.annotations.${annotation_name}==\"${annotation_value}\")]}'"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment