-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkill-services.sh
executable file
·114 lines (100 loc) · 4.03 KB
/
kill-services.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash
# Function to delete resources that do not have the critical=true label
delete_non_critical_resources() {
RESOURCES=( "cronjobs" "jobs" "statefulsets" "deployments" "replicasets" "daemonsets" "pods" "services" "configmaps" "secrets")
for RESOURCE in "${RESOURCES[@]}"; do
echo "Deleting $RESOURCE that are not labeled critical=true..."
if ! sudo kubectl delete "$RESOURCE" --selector 'critical!=true' --all-namespaces; then
echo "Failed to delete $RESOURCE"
fi
done
}
# Function to dissociate PVs from PVCs and delete PVs
clean_pvs() {
echo "Cleaning up orphaned PVs..."
# Handle Released PVs
for pv in $(sudo kubectl get pv --no-headers | awk '{if ($5 == "Released") print $1}'); do
echo "Removing claimRef for PV: $pv"
if ! sudo kubectl patch pv "$pv" --type=json -p='[{"op": "remove", "path": "/spec/claimRef"}]'; then
echo "Failed to remove claimRef for $pv"
fi
# Skip deleting critical PVs
if ! sudo kubectl get pv "$pv" -o=jsonpath='{.metadata.labels.critical}' | grep -q 'true'; then
echo "Deleting PV: $pv"
if ! sudo kubectl delete pv "$pv"; then
echo "Failed to delete PV: $pv"
fi
else
echo "Skipping critical PV: $pv"
fi
done
# Force delete Terminating PVs, skip critical ones
for pv in $(sudo kubectl get pv --no-headers | awk '{if ($5 == "Terminating") print $1}'); do
echo "Force-deleting Terminating PV: $pv"
if ! sudo kubectl patch pv "$pv" --type=json -p='[{"op": "remove", "path": "/metadata/finalizers"}]'; then
echo "Failed to remove finalizers for $pv"
fi
# Skip force-deleting critical PVs
if ! sudo kubectl get pv "$pv" -o=jsonpath='{.metadata.labels.critical}' | grep -q 'true'; then
if ! sudo kubectl delete pv "$pv" --force --grace-period=0; then
echo "Failed to force-delete PV: $pv"
fi
else
echo "Skipping force-delete for critical PV: $pv"
fi
done
}
# Function to delete PVCs that are not labeled critical=true
clean_pvcs() {
echo "Cleaning up PVCs..."
for pvc in $(sudo kubectl get pvc --no-headers | awk '{print $1}'); do
# Skip deleting critical PVCs
if ! sudo kubectl get pvc "$pvc" -o=jsonpath='{.metadata.labels.critical}' | grep -q 'true'; then
echo "Deleting PVC: $pvc"
if ! sudo kubectl delete pvc "$pvc"; then
echo "Failed to delete PVC: $pvc"
fi
else
echo "Skipping critical PVC: $pvc"
fi
done
}
# Function to kill kubectl port-forward processes for specific services with retry
kill_port_forward_processes() {
echo "Killing any running kubectl port-forward processes for specific services..."
# Define the specific services and ports to target
services=("django-service:8000" "prometheus-k8s:9090" "grafana:3000" "alertmanager-main:9093")
# Loop through the services and ports
for service in "${services[@]}"; do
# Extract service name and port
service_name=$(echo $service | cut -d: -f1)
port=$(echo $service | cut -d: -f2)
echo "Checking for kubectl port-forward process for $service_name on port $port..."
# Loop for multiple attempts if new processes spawn
for attempt in {1..5}; do
pids=$(sudo pgrep -f "kubectl port-forward .*${service_name}.*${port}")
if [ -n "$pids" ]; then
echo "Found running kubectl port-forward processes for $service_name:$port with PIDs: $pids"
for pid in $pids; do
if sudo kill "$pid"; then
echo "Successfully killed process $pid for $service_name:$port"
else
echo "Failed to kill process $pid for $service_name:$port. Trying SIGKILL..."
sudo kill -9 "$pid" && echo "Successfully killed process $pid for $service_name:$port with SIGKILL"
fi
done
else
echo "No kubectl port-forward process found for $service_name:$port."
break
fi
sleep 1 # Wait before retrying
done
done
}
# Execute the cleanup functions
helm uninstall wordpress
delete_non_critical_resources
clean_pvcs
clean_pvs
kill_port_forward_processes
echo "Cleanup completed."