diff --git a/.functions/kubernetes.sh b/.functions/kubernetes.sh index 8a19386..d7e8843 100644 --- a/.functions/kubernetes.sh +++ b/.functions/kubernetes.sh @@ -79,3 +79,51 @@ function k8s_neuvector_cve_db_version(){ function k8s_node_taints(){ kubectl get nodes -o json | jq '.items[].spec.taints' } + +function k8s_apps(){ + # parameters, + # stop = Set all deployments & statefultsets to replica=0 + # start = set all deployments & statefulsets to replica=1 + # show = show status of all deployments & statefulsets + CHOICE="$1" + if [[ ! "start stop show" =~ (" "|^)"$CHOICE"(" "|$) ]]; then + echo "no parameter given" + return 1 + fi + local USER_PROJECT_IDS=(p-c5fcj p-q8pbm) + local OK='\e[32m\u2714\e[0m' + local NOT_OK='\u274c' + printf "%-22s %s\n" "namespace" "application" + printf -- '-%.0s' {1..34} + printf "\n" + for PROJECT in "${USER_PROJECT_IDS[@]}"; do + for NS in $(kubectl get namespaces --selector=field.cattle.io/projectId="$PROJECT" --template "{{range .items}}{{.metadata.name}}{{\"\n\"}}{{end}}"); do + for DEPLOYMENT in $(kubectl --namespace "$NS" get deployments --template "{{range .items}}{{.metadata.name}}{{\"\n\"}}{{end}}"); do + if [[ "$CHOICE" == "stop" ]];then + kubectl --namespace "$NS" scale deployment "$DEPLOYMENT" --replicas=0 > /dev/null + elif [[ "$CHOICE" == "start" ]];then + kubectl --namespace "$NS" scale deployment "$DEPLOYMENT" --replicas=1 > /dev/null + fi + if [[ $(kubectl --namespace "$NS" get deployments "$DEPLOYMENT" -o jsonpath='{.spec.replicas}') == "0" ]];then + echo -n -e "$NOT_OK " + else + echo -n -e "$OK " + fi + printf "%-20s %s\n" "$NS" "$DEPLOYMENT" + done + for STATEFULSET in $(kubectl --namespace "$NS" get statefulsets --template "{{range .items}}{{.metadata.name}}{{\"\n\"}}{{end}}"); do + if [[ "$CHOICE" == "stop" ]];then + kubectl --namespace "$NS" scale statefulset "$STATEFULSET" --replicas=0 > /dev/null + elif [[ "$CHOICE" == "start" ]];then + kubectl --namespace "$NS" scale statefulset "$STATEFULSET" --replicas=1 > /dev/null + fi + if [[ $(kubectl --namespace "$NS" get statefulsets "$STATEFULSET" -o jsonpath='{.spec.replicas}') == "0" ]];then + echo -n -e "$NOT_OK " + else + echo -n -e "$OK " + fi + printf "%-20s %s\n" "$NS" "$STATEFULSET" + done + done + done +}