Created
August 25, 2022 20:26
-
-
Save tcarreira/81c73b2f38a7ac27dab330c71b91ba3c to your computer and use it in GitHub Desktop.
kubectl get filter by annotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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