I currently have this bash script that do cleanup obsolete resources for deployment, services, and configmaps. when I try to add the kubectl delete pod
on the script, then it will delete all the remaining pods, but the problem is for a certain scenario wherein I will cleanup only the pods that do not have or without the object like the deployment, services, and configmaps it will also restart the pods due to the kubectl delete pod
and I do not want the other pods or resources also to restart but only delete the specific pod that have only an object pod, then for the other pod that have the object of deployment, services, and configmaps should not be restarted.
Here's the script:
active_pods=$(kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}') active_deployments=$(kubectl get deployments -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}') active_services=$(kubectl get services -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}') active_configmaps=$(kubectl get configmap -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}') readarray -t active_pods_array <<<"$active_pods" readarray -t active_deployments_array <<<"$active_deployments" readarray -t active_services_array <<<"$active_services" readarray -t active_configmaps_array <<<"$active_configmaps" echo "active pods: ${active_pods_array[@]}" for pod in "${active_pods_array[@]}"; do if ! grep -q "pod/${pod} " nonprod.txt && [[ -n $pod ]] ; then kubectl delete pod "$pod" fi done echo "active deployments: ${active_deployments_array[@]}" for deployment in "${active_deployments_array[@]}"; do if ! grep -q "deployment.apps/${deployment} " nonprod.txt && [[ -n $deployment ]] ; then kubectl delete deployment "$deployment" fi done echo "active services: ${active_services_array[@]}" for service in "${active_services_array[@]}"; do if ! grep -q "service/${service} " nonprod.txt && [[ -n $service ]] && [[ $service != 'kubernetes' ]] ; then kubectl delete service "$service" fi done echo "active configmap: ${active_configmaps_array[@]}" for configmap in "${active_configmaps_array[@]}"; do if ! grep -q "configmap/${configmap} " nonprod.txt && [[ -n $configmap ]] && [[ ${configmap} != "kube-root-ca.crt" ]] ; then kubectl delete configmap "${configmap}" fi done
~ $ kubectl get podsNAME READY STATUS RESTARTS AGElogging-event-7a8gl478e4-7pms9 1/1 Running 0 22mmy-workspace-92757a9w8p-ghedk 1/1 Running 0 25mmy-web-page-347817pt37-7wu1a 1/1 Running 0 21mhello-world-8i64337229-4yd37 1/1 Running 0 21mhttp-client-test 1/1 Running 0 21mmysql8demo-7425681afg-krdlh 1/1 Running 0 22mpostgres16demo-4p25jr3g9k-2piex 1/1 Running 0 21mmytestcurl 1/1 Running 0 21mmytestdemo-9449873a4c-at5kg 1/1 Running 0 22m
~ $ kubectl get deploymentNAME READY UP-TO-DATE AVAILABLE AGElogging-event 1/1 1 1 22mmy-workspace 1/1 1 1 25mmy-web-page 1/1 1 1 21mhello-world 1/1 1 1 21mmysql8demo 1/1 1 1 22mpostgres16demo 1/1 1 1 21mmytestdemo 1/1 1 1 22m
~ $ kubectl get serviceNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGElogging-event ClusterIP ########## <none> 8080/TCP 22mmy-web-page ClusterIP ########## <none> 80/TCP 21mhello-world ClusterIP ########## <none> 80/TCP 21mkubernetes ClusterIP ########## <none> 443/TCP 31mmysql8demo ClusterIP ########## <none> 1433/TCP 22mpostgres16demo ClusterIP ########## <none> 5432/TCP 21mmytestdemo ClusterIP ########## <none> 8080/TCP 22m
~ $ kubectl get configmapNAME DATA AGElogging-event 1 22mmy-workspace-scripts 1 26mhello-world-html 1 21mkube-root-ca.crt 1 31mmysql8demo-hook 1 22mmysql8demo-part-66772g4g2ba1fdd37d7273f2gs64b4a8-aa-cm 1 22mpostgres16demo-hook 1 22mpostgres16demo-part-9r54446g81dbr16836g4214g85189139-aa-cm 1 22mmytestdemo-mappings 3 22m