Delete Pod without restarting other resources/object

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> 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
</code>
<code> 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 </code>
      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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>~ $ kubectl get pods
NAME READY STATUS RESTARTS AGE
logging-event-7a8gl478e4-7pms9 1/1 Running 0 22m
my-workspace-92757a9w8p-ghedk 1/1 Running 0 25m
my-web-page-347817pt37-7wu1a 1/1 Running 0 21m
hello-world-8i64337229-4yd37 1/1 Running 0 21m
http-client-test 1/1 Running 0 21m
mysql8demo-7425681afg-krdlh 1/1 Running 0 22m
postgres16demo-4p25jr3g9k-2piex 1/1 Running 0 21m
mytestcurl 1/1 Running 0 21m
mytestdemo-9449873a4c-at5kg 1/1 Running 0 22m
</code>
<code>~ $ kubectl get pods NAME READY STATUS RESTARTS AGE logging-event-7a8gl478e4-7pms9 1/1 Running 0 22m my-workspace-92757a9w8p-ghedk 1/1 Running 0 25m my-web-page-347817pt37-7wu1a 1/1 Running 0 21m hello-world-8i64337229-4yd37 1/1 Running 0 21m http-client-test 1/1 Running 0 21m mysql8demo-7425681afg-krdlh 1/1 Running 0 22m postgres16demo-4p25jr3g9k-2piex 1/1 Running 0 21m mytestcurl 1/1 Running 0 21m mytestdemo-9449873a4c-at5kg 1/1 Running 0 22m </code>
~ $ kubectl get pods
NAME                                        READY   STATUS    RESTARTS      AGE
logging-event-7a8gl478e4-7pms9              1/1     Running   0             22m
my-workspace-92757a9w8p-ghedk               1/1     Running   0             25m
my-web-page-347817pt37-7wu1a                1/1     Running   0             21m
hello-world-8i64337229-4yd37                1/1     Running   0             21m
http-client-test                            1/1     Running   0             21m
mysql8demo-7425681afg-krdlh                 1/1     Running   0             22m
postgres16demo-4p25jr3g9k-2piex             1/1     Running   0             21m
mytestcurl                                  1/1     Running   0             21m
mytestdemo-9449873a4c-at5kg                 1/1     Running   0             22m
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>~ $ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
logging-event 1/1 1 1 22m
my-workspace 1/1 1 1 25m
my-web-page 1/1 1 1 21m
hello-world 1/1 1 1 21m
mysql8demo 1/1 1 1 22m
postgres16demo 1/1 1 1 21m
mytestdemo 1/1 1 1 22m
</code>
<code>~ $ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE logging-event 1/1 1 1 22m my-workspace 1/1 1 1 25m my-web-page 1/1 1 1 21m hello-world 1/1 1 1 21m mysql8demo 1/1 1 1 22m postgres16demo 1/1 1 1 21m mytestdemo 1/1 1 1 22m </code>
~ $ kubectl get deployment
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
logging-event              1/1     1            1           22m
my-workspace               1/1     1            1           25m
my-web-page                1/1     1            1           21m
hello-world                1/1     1            1           21m
mysql8demo                 1/1     1            1           22m
postgres16demo             1/1     1            1           21m
mytestdemo                 1/1     1            1           22m
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>~ $ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
logging-event ClusterIP ########## <none> 8080/TCP 22m
my-web-page ClusterIP ########## <none> 80/TCP 21m
hello-world ClusterIP ########## <none> 80/TCP 21m
kubernetes ClusterIP ########## <none> 443/TCP 31m
mysql8demo ClusterIP ########## <none> 1433/TCP 22m
postgres16demo ClusterIP ########## <none> 5432/TCP 21m
mytestdemo ClusterIP ########## <none> 8080/TCP 22m
</code>
<code>~ $ kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE logging-event ClusterIP ########## <none> 8080/TCP 22m my-web-page ClusterIP ########## <none> 80/TCP 21m hello-world ClusterIP ########## <none> 80/TCP 21m kubernetes ClusterIP ########## <none> 443/TCP 31m mysql8demo ClusterIP ########## <none> 1433/TCP 22m postgres16demo ClusterIP ########## <none> 5432/TCP 21m mytestdemo ClusterIP ########## <none> 8080/TCP 22m </code>
~ $ kubectl get service
NAME                       TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
logging-event              ClusterIP   ##########     <none>        8080/TCP   22m
my-web-page                ClusterIP   ##########     <none>        80/TCP     21m
hello-world                ClusterIP   ##########     <none>        80/TCP     21m
kubernetes                 ClusterIP   ##########     <none>        443/TCP    31m
mysql8demo                 ClusterIP   ##########     <none>        1433/TCP   22m
postgres16demo             ClusterIP   ##########     <none>        5432/TCP   21m
mytestdemo                 ClusterIP   ##########     <none>        8080/TCP   22m
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>~ $ kubectl get configmap
NAME DATA AGE
logging-event 1 22m
my-workspace-scripts 1 26m
hello-world-html 1 21m
kube-root-ca.crt 1 31m
mysql8demo-hook 1 22m
mysql8demo-part-66772g4g2ba1fdd37d7273f2gs64b4a8-aa-cm 1 22m
postgres16demo-hook 1 22m
postgres16demo-part-9r54446g81dbr16836g4214g85189139-aa-cm 1 22m
mytestdemo-mappings 3 22m
</code>
<code>~ $ kubectl get configmap NAME DATA AGE logging-event 1 22m my-workspace-scripts 1 26m hello-world-html 1 21m kube-root-ca.crt 1 31m mysql8demo-hook 1 22m mysql8demo-part-66772g4g2ba1fdd37d7273f2gs64b4a8-aa-cm 1 22m postgres16demo-hook 1 22m postgres16demo-part-9r54446g81dbr16836g4214g85189139-aa-cm 1 22m mytestdemo-mappings 3 22m </code>
~ $ kubectl get configmap
NAME                                                         DATA   AGE
logging-event                                                1      22m
my-workspace-scripts                                         1      26m
hello-world-html                                             1      21m
kube-root-ca.crt                                             1      31m
mysql8demo-hook                                              1      22m
mysql8demo-part-66772g4g2ba1fdd37d7273f2gs64b4a8-aa-cm       1      22m
postgres16demo-hook                                          1      22m
postgres16demo-part-9r54446g81dbr16836g4214g85189139-aa-cm   1      22m
mytestdemo-mappings                                          3      22m

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật