Skip to content

Instantly share code, notes, and snippets.

@mjf
Last active May 12, 2023 10:04
Show Gist options
  • Save mjf/cae41ad80fbd612069a644482f1b466f to your computer and use it in GitHub Desktop.
Save mjf/cae41ad80fbd612069a644482f1b466f to your computer and use it in GitHub Desktop.
kubectl-scaleall - Scale most deployments and stateful sets up or down
#! /bin/sh
# kubectl-scaleall - Scale most deployments and stateful sets up or down
# Copyright (C) 2023 Matous Jan Fialka, <https://mjf.cz/>
# Released under the terms of the "MIT" license
PROGRAM="${0##*/}"
VERSION='0.1.4'
LICENSE='MIT'
AUTHORS='2022 Matous Jan Fialka, <https://mjf.cz/>'
# HELPERS
proc() {
local n=$1
local a=''
shift 1
for a in $@; do
if [ $no_color_out ]; then
printf -- 'PROC[%05d] %s: prosessing '\''%s'\''\n' $n "$PROGRAM" "$a"
else
printf -- '\x1b[32mPROC\x1b[0m[%05d] \x1b[35m%s\x1b[0m: prosessing '\''\x1b[34;47;7m%s\x1b[0m'\''\n' $n "$PROGRAM" "$a"
fi
done
}
info() {
local n=$1
shift 1
if [ $no_color_out ]; then
printf -- 'INFO[%05d] %s: %s\n' $n "$PROGRAM" "$1"
else
printf -- '\x1b[36mINFO\x1b[0m[%05d] \x1b[35m%s\x1b[0m: %s\n' $n "$PROGRAM" "$1"
fi
}
warn() {
local n=$1
shift 1
if [ $no_color_err ]; then
printf -- 'WARN[%05d] %s: %s\n' $n "$PROGRAM" "$1" >&2
else
printf -- '\x1b[33mWARN\x1b[0m[%05d] \x1b[35m%s\x1b[0m: \x1b[33m%s\x1b[0m\n' $n "$PROGRAM" "$1" >&2
fi
}
fail() {
local n=$1
shift 1
if [ $no_color_err ]; then
printf -- 'FAIL[%05d] %s: %s\n' $n "$PROGRAM" "$2" >&2
else
printf -- '\x1b[31;47;7mFAIL\x1b[0m[%05d] \x1b[35m%s\x1b[0m: \x1b[31;47;7m%s\x1b[0m\n' $n "$PROGRAM" "$2" >&2
fi
exit ${1:-1}
}
# MAIN
no_color_out=$NO_COLOR
if ! [ -t 1 ]; then
no_color_out='yes'
fi
no_color_err=$NO_COLOR
if ! [ -t 2 ]; then
no_color_err='yes'
fi
replicas=${1:-1}
info 0 "rescaling all deployments and stateful sets in all namespaces to $replicas replicas"
info 10000 "getting list of namespaces"
namespaces=$(
kubectl get namespaces 2>/dev/null |
sed '1d' |
awk '{print$1}'
)
error=$?
if [ $error -ne 0 ]; then
fail 10001 $error "could not get list of namespaces"
fi
i=19999
for namespace in $namespaces; do
let i++
info $i "getting list of deployments in namespace '$namespace'"
if [ "$namespace" = 'kube-system' -a $replicas -eq 0 ]; then
warn $i "skipped system namespace 'kube-system' (unsafe to rescale)"
continue
fi
deployments=$(
kubectl --namespace "$namespace" get deployments 2>/dev/null |
sed '1d' |
awk '{print$1}'
)
error=$?
if [ $error -ne 0 -o ${#deployments} -eq 0 ]; then
warn $i "cannot get list of deployments in namespace '$namespace' (skipped)"
continue
fi
info $i "rescaling deployments in namespace '$namespace'"
proc $i "$deployments"
kubectl scale deployment \
--namespace $namespace \
--replicas $replicas \
$deployments &>/dev/null
error=$?
if [ $error -ne 0 ]; then
warn $i "could not rescale deployments in namespace '$namespace' (review suggested)"
else
info $i "deployments in namespace '$namespace' rescaled to $replicas replicas"
fi
done
i=29999
for namespace in $namespaces; do
let i++
info $i "getting list of stateful sets in namespace '$namespace'"
if [ "$namespace" = 'kube-system' -a $replicas -eq 0 ]; then
warn $i "skipped system namespace 'kube-system' (unsafe to rescale)"
continue
fi
statefulsets=$(
kubectl --namespace "$namespace" get statefulsets 2>/dev/null |
sed '1d' |
awk '{print$1}'
)
error=$?
if [ $error -ne 0 -o ${#statefulsets} -eq 0 ]; then
warn $i "cannot get list of stateful sets in namespace '$namespace' (skipped)"
continue
fi
info $i "rescaling stateful sets in namespace '$namespace'"
proc $i "$statefulsets"
kubectl scale statefulset \
--namespace $namespace \
--replicas $replicas \
$statefulsets &>/dev/null
error=$?
if [ $error -ne 0 ]; then
warn $i "could not rescale stateful sets in namespace '$namespace' (review suggested)"
else
info $i "stateful sets in namespace '$namespace' rescaled to $replicas replicas"
fi
done
info 0 'rescaling done (review of overall cluster state suggested)'
# vi:ft=sh:nowrap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment