0
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review! Why is this question tagged with python? There does not seem to be a single line of Python code in the whole post, but only bash? \$\endgroup\$ – AlexV Sep 11 at 18:34
  • \$\begingroup\$ Oh I thought with Python Tag I can get Python code for similar func. \$\endgroup\$ – AhmFM Sep 11 at 21:05
  • 2
    \$\begingroup\$ That's not the purpose of this site! Code Review is a platform where you can get feedback on the code you have written, but not a code writing service. \$\endgroup\$ – AlexV Sep 11 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.