Skip to content

Instantly share code, notes, and snippets.

@yoonian
Last active August 26, 2020 06:05
Show Gist options
  • Select an option

  • Save yoonian/9ad415a8f0470516d47b19492fc63041 to your computer and use it in GitHub Desktop.

Select an option

Save yoonian/9ad415a8f0470516d47b19492fc63041 to your computer and use it in GitHub Desktop.
chaostest restart any pod at interval
#!/bin/bash
#
# chaos kill any pod at interval
# by yoon
#
usage() {
echo "Usage: $0 [-n namespaces] [-a annotation] [-l labels] [-r number of pods] [-i interval] [-f|-d]"
echo " -n namespaces: comma seperated namespace. you can use ! as a not operator. default) !kube-system,!production"
echo " -a annotation: annotation to matched. default) chaos.alpha.kubernetes.io/enabled=true"
echo " -l labels: labels to match pod."
echo " -r number of pods to restart. default) 1"
echo " -i interval: interval to restart. you can use s,m postfix like sleep. default) 10m"
echo " -f : force to restart. default) no"
echo " -d : dry-run. default) no"
exit 1;
}
get_gotemplate() {
local _BEGIN="{{range .items}}{{with .metadata}}"
local _END="{{end}}{{end}}"
local NAMESPACE_EXPS=$1
local ANN=$2
IFS=',' read -ra EXPS <<< "$NAMESPACE_EXPS"
for exp in "${EXPS[@]}"; do
OPERATOR=eq
[[ ${exp} == \!* ]] && OPERATOR=ne
NS=${exp#*\!}
_BEGIN="${_BEGIN}{{if ${OPERATOR} .namespace \"${NS}\"}}"
_END="{{end}}${_END}"
done
if [ ${#ANN} -gt 0 ];
then
_BEGIN="${_BEGIN}{{if .annotations}}"
_BEGIN="${_BEGIN}{{if index .annotations \"${ANN%=*}\"}}"
_BEGIN="${_BEGIN}{{if eq (index .annotations \"${ANN%=*}\") \"${ANN#*=}\"}}"
_END="{{end}}{{end}}{{end}}${_END}"
fi
echo -n ${_BEGIN}
echo -n "{{println .name}}"
echo ${_END}
}
# begin
INTERVAL=10m
NUM=1
LABELS=
FORCE=
DRYRUN=0
ANNOTATION='chaos.alpha.kubernetes.io/enabled=true'
NAMESPACES='!kube-system,!production'
while getopts :n:a:r:i:hfdl: o; do
case "${o}" in
n)
NAMESPACES=${OPTARG}
;;
i)
INTERVAL=${OPTARG}
;;
a)
ANNOTATION=${OPTARG}
;;
l)
LABELS="-l${OPTARG}"
;;
r)
NUM=${OPTARG}
;;
d)
DRYRUN=1
;;
f)
FORCE="--force --grace-period=0"
;;
h|*)
usage
;;
esac
done
shift $((OPTIND-1))
TEMPLATE=$(get_gotemplate ${NAMESPACES} ${ANNOTATION})
if [[ $DRYRUN -gt 0 ]];
then
echo "NOTE: dry-run"
fi
echo "${NUM} pod(s) with annotation(${ANNOTATION}) in namespace matching condition(${NAMESPACES}) will be restarted at ${WAIT} interval."
while true; do
sleep ${INTERVAL}
PODS=$(kubectl get po --no-headers --all-namespaces ${LABELS} -o go-template --template="${TEMPLATE}")
if [[ ${#PODS} -gt 0 ]];
then
PODS=$(echo $PODS | xargs shuf -n${NUM} -e)
echo "$(date) $PODS will be restarted."
if [[ $DRYRUN -eq 0 ]];
then
kubectl delete po ${FORCE} ${PODS} 2> /dev/null
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment