truestuff/get-ix-pods

92 lines
3.0 KiB
Bash
Executable File

#! /usr/bin/env bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
. ${SHRC_D:-$SCRIPT_DIR}/01_util.functions
set -e
get-apps() {
k3s kubectl get --all-namespaces deployments.apps --sort-by='.metadata.namespace' "${@}"
}
FILTER1=('')
FILTER2=()
OUTPUT=()
while [[ -n "$1" ]]; do
ARG="${1}"
shift
if [[ "${#OUTPUT[@]}" -eq 0 && "$ARG" =~ ^-(N|name|names-only)$ ]]; then
OUTPUT=(awk "'{print \$1;}'" \| sort --unique)
elif [[ "${#OUTPUT[@]}" -eq 0 && "$ARG" =~ ^-Nx$ ]]; then
OUTPUT=(awk "'{print \$1;}'" \| sed -e "'s|^ix-||'" \| sort --unique)
elif [[ "${#OUTPUT[@]}" -eq 0 && "$ARG" =~ ^-(H|(no-?)(head(e(r(s)?)?)?))$ ]]; then
OUTPUT=('grep' '.')
elif [[ -z "${FILTER1[*]}" && "$ARG" =~ ^- ]]; then
if [[ "$ARG" =~ ^(-A|--all)$ ]]; then
FILTER1=('.')
elif [[ "$ARG" =~ ^-(a|-active)$ ]]; then
FILTER1=(-v '\b0/0\b')
elif [[ "$ARG" =~ ^-(r|-ready)$ ]]; then
FILTER1=('\b([1-9]\d*)/\1\b')
elif [[ "$ARG" =~ ^-(l|-limbo)$ ]]; then
FILTER1=('\b([0-9]+)/(?!\1)([1-9][0-9]*)\b')
elif [[ "$ARG" =~ ^-(s|-stop|-stopped)$ ]]; then
FILTER1=('\b0/0\b')
elif [[ "$ARG" =~ ^(-h|-?|/h|/?|--help)$ ]]; then
printf '%s\n' \
'Usage:' \
" $(basename "$0") <args> [grep -P filter]" \
'' \
' Default behavior will list all apps' \
'' \
'Arguments:' \
' -h | --help Display this help/usage message' \
'' \
'Filters:' \
' -A | --all (default) List all apps' \
' -a | --active List all active (non-stopped) apps' \
' -r | --ready List all active apps in ready state' \
' -s | --stopped List all apps in stopped state' \
' -l | --limbo List all active apps not in ready state' \
'' \
'Output:' \
' -N | --names-only App names only' \
' -Nx App names only with "ix-" prefix removed' \
' -H | --no-headers No headers'
exit 0
else
printf '%s\n' \
"Invalid argument: $ARG" \
>&2
exit 1
fi
else
FILTER2=("${FILTER2[@]}" "$ARG")
fi
done
APPS="$(get-apps)"
HEADER="$(head -n1 <<<"$APPS")"
APPS="$(tail +1 <<<"$APPS")"
APPS="$(
grep -E --color=no "$(
get-apps --output=json | jq -r '
[.items[]
| select( .metadata.labels."app.kubernetes.io/managed-by" == "Helm" )
| .metadata.namespace]
| join( "|" )
'
)" <<<"$APPS" \
| grep -P "${FILTER1[@]}" \
| grep -P "${FILTER2[@]:-.}"
)"
if [[ "${#OUTPUT[@]}" -le 1 ]]; then
echo "$HEADER"
fi
echo "$APPS" \
| eval "${OUTPUT[*]:-grep .}"