#! /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' "${@}" } FILTER=('') ARG="${1:---all}" if [[ -n "$ARG" ]]; then if [[ "$ARG" =~ ^-(a|-active)$ ]]; then FILTER=(-v '\b0/0\b') elif [[ "$ARG" =~ ^-(r|-ready)$ ]]; then FILTER=('\b([1-9]\d*)/\1\b') elif [[ "$ARG" =~ ^-(l|-limbo)$ ]]; then FILTER=('\b([0-9]+)/(?!\1)([1-9][0-9]*)\b') elif [[ "$ARG" =~ ^-(s|-stop|-stopped)$ ]]; then FILTER=('\b0/0\b') elif [[ "$ARG" =~ ^(-h|-?|/h|/?|--help)$ ]]; then printf '%s\n' \ 'Usage:' \ " $(basename "$0") " \ '' \ ' Default behavior will list all apps' \ '' \ 'Arguments:' \ ' -h | --help Display this help/usage message' \ ' -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' exit 0 elif [[ ! "$ARG" =~ ^(-A|--all)$ ]]; then printf '%s\n' \ "Invalid argument: $ARG" \ >&2 exit 1 fi fi 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 "${FILTER[@]}" )" echo "$HEADER" echo "$APPS"