Skip to content

Instantly share code, notes, and snippets.

@hoegaarden
Last active November 13, 2020 08:44
Show Gist options
  • Save hoegaarden/bf0548fd0efda248c31268955a830b65 to your computer and use it in GitHub Desktop.
Save hoegaarden/bf0548fd0efda248c31268955a830b65 to your computer and use it in GitHub Desktop.
k-filter -- for when kubectl talks too much
#!/usr/bin/env bash
set -e
set -u
set -o pipefail
declare -rA FILTERS=(
# Remove some mostly unintersting fields
['klean']='
del( .. | .metadata?
| .managedFields? , .uid? , .selfLink? , .resourceVersion? , .generation?
)
'
# Make secrets readable by base64 decoding them
['sekret']='
walk(
if (type=="object" and .kind=="Secret" and (.data | type=="object"))
then
.data |= map_values(@base64d)
else
.
end
)
'
)
nope() {
printf 'Cannot be called as %q because no such filter exists\n' "$1"
echo
echo 'Available filters:'
printf ' - %q\n' "${!FILTERS[@]}"
echo
echo 'Example call:'
printf ' kubectl get secrets -o yaml | %q\n' "$1"
}
main() {
local mode
mode="$( basename "$0" )"
[ "${FILTERS[$mode]+something}" ] || {
nope "$mode" >&2
return 1
}
yq -y "${FILTERS[$mode]}"
}
main "$@"
k-filter -- some filters for kubectl when it talks too much or grabage
0. Needs jq & yq on the system
jq: https://stedolan.github.io/jq/ (apt install jq)
yq: https://pypi.org/project/yq/ (pip3 install yq)
1. create links
ln -s k-filter klean
ln -s k-filter sekret
2. run it in a pipeline
kubectl get secrets -o yaml | klean | sekret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment