Skip to content

Commit 0050770

Browse files
authored
add script (#294)
2 parents 1462fda + 8652631 commit 0050770

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/bin/bash
2+
3+
NAMESPACE=$1
4+
RELEASE_NAME=$2
5+
TIMEOUT=300 # Timeout in seconds
6+
INTERVAL=5 # Polling interval in seconds
7+
8+
# Store original replica counts
9+
#declare -A original_replicas
10+
11+
12+
# Function to save original replica counts
13+
save_original_replicas() {
14+
local resource_type=$1
15+
echo "Saving original replica counts for $resource_type..."
16+
for resource in $(kubectl get $resource_type -n $NAMESPACE -l app.kubernetes.io/instance=$RELEASE_NAME -o name); do
17+
replicas=$(kubectl get $resource -n $NAMESPACE -o jsonpath='{.spec.replicas}')
18+
resource_name=$(echo "$resource" | awk -F'/' '{print $2}')
19+
original_replicas["$resource_name"]=$replicas
20+
echo "$resource_name: $replicas replicas"
21+
done
22+
}
23+
24+
# Function to scale down resources of a specific type
25+
scale_down() {
26+
local resource_type=$1
27+
echo "Scaling down $resource_type..."
28+
kubectl get $resource_type -n $NAMESPACE -l app.kubernetes.io/instance=$RELEASE_NAME -o name | xargs -n 1 kubectl scale --replicas=0 -n $NAMESPACE
29+
}
30+
31+
# Function to wait for all resources of a specific type to scale down
32+
wait_for_scaling() {
33+
local resource_type=$1
34+
local start_time=$(date +%s)
35+
36+
echo "Waiting for $resource_type to scale down..."
37+
while true; do
38+
local remaining=$(kubectl get $resource_type -n $NAMESPACE -l app.kubernetes.io/instance=$RELEASE_NAME -o jsonpath='{.items[*].status.replicas}' | tr ' ' '\n' | awk '{s+=$1} END {print s+0}')
39+
if [ "$remaining" -eq 0 ]; then
40+
echo "All $resource_type scaled down."
41+
break
42+
fi
43+
44+
# Check timeout
45+
local current_time=$(date +%s)
46+
if [ $((current_time - start_time)) -ge $TIMEOUT ]; then
47+
echo "Timeout while waiting for $resource_type to scale down."
48+
exit 1
49+
fi
50+
51+
sleep $INTERVAL
52+
done
53+
}
54+
55+
# Function to scale resources back to their original replica counts
56+
scale_up() {
57+
local resource_type=$1
58+
echo "Scaling $resource_type back to original replica counts..."
59+
for resource in $(kubectl get $resource_type -n $NAMESPACE -l app.kubernetes.io/instance=$RELEASE_NAME -o name); do
60+
replicas=${original_replicas[$resource]:-0}
61+
echo " Scaling $resource to $replicas replicas..."
62+
kubectl scale --replicas=$replicas -n $NAMESPACE $resource
63+
done
64+
}
65+
66+
# Main workflow
67+
echo "Scaling down all resources for release $RELEASE_NAME in namespace $NAMESPACE..."
68+
69+
# Save and scale down Deployments
70+
#save_original_replicas "deployment"
71+
scale_down "deployment"
72+
wait_for_scaling "deployment"
73+
74+
# Save and scale down StatefulSets
75+
#save_original_replicas "statefulset"
76+
scale_down "statefulset"
77+
wait_for_scaling "statefulset"
78+
79+
# Save and scale down ReplicaSets
80+
#save_original_replicas "replicaset"
81+
scale_down "replicaset"
82+
wait_for_scaling "replicaset"
83+
84+
echo "All resources for release $RELEASE_NAME have been scaled down."
85+
86+
echo "Deleting PVCs..."
87+
88+
kubectl get pvc -n $NAMESPACE -l app.kubernetes.io/instance=$RELEASE_NAME
89+
kubectl delete pvc -n $NAMESPACE -l app.kubernetes.io/instance=$RELEASE_NAME
90+
91+
echo "Waiting for PVCs to be fully deleted..."
92+
while kubectl get pvc -n $NAMESPACE -l app.kubernetes.io/instance=$RELEASE_NAME 2>/dev/null | grep -q pvc; do
93+
echo "PVCs still terminating..."
94+
sleep 5
95+
done
96+
97+
echo "All PVCs for release $RELEASE_NAME have been deleted."
98+
99+
# Scale resources back up
100+
#echo "Scaling resources back to their original replica counts..."
101+
#scale_up "deployment"
102+
#scale_up "statefulset"
103+
#scale_up "replicaset"

0 commit comments

Comments
 (0)