I have my below prototyped script and it runs fine but I need to scale it out at large. So, wanted to get feedback if my script can be improved in terms of performance and if there are any non standard stuff am doing in it.
What is does: Script is to delete all the pods under defined namespaces and then also from kube-system namespace only certain pods. It also gives user 10sec to chose to cancel. Operation.
I am going to merge this to production but I want to assure am atleast in good shape to do coz there will be 1000s of pods in production. Am new to coding, if bash is best choice, please suggest how to proceed with it.
#!/bin/bash
TT_NAMESPACES=(ambassador
argo-workflow-controller
webhook
)
#Pulling teamstest pods under teamstest service name namespace
printf "Deleting pods for teamstest Namespaces: %s\n" "${TT_NAMESPACES[@]}"
for nmspc in ${TT_NAMESPACES[@]}
do
printf "Terminate Jenkins Job in: %d seconds to cancel this operation\n" "10"
sleep 10
printf "Deleting PODS for Namespace: %s\n" "$nmspc"
kubectl -n $nmspc delete --all pods --force --grace-period 0
printf '\n'
done
#Pulling teamstest pods under Kube-system namespace
TT_SERVICES_KS_NMSPC=(local-volume-provisioner
registry-creds
tiller
)
printf "Deleting pods for teamstest services in kube-system Namespace: %s\n" "${TT_SERVICES_KS_NMSPC[@]}"
for ml_service_pod in ${TT_SERVICES_KS_NMSPC[@]}
do
printf "Deleting teamstest PODS for %s in Kube-System Namespace\n" "$ml_service_pod"
printf "Terminate Jenkins Job in: %d seconds to cancel this operation\n" "10"
sleep 10
kubectl -n kube-system get pods | grep $ml_service_pod | xargs -l1 -- sh -c 'kubectl -n kube-system delete pod $1 --force --grace-period 0' --
printf '\n'
done