-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.sh
More file actions
143 lines (121 loc) · 4.6 KB
/
common.sh
File metadata and controls
143 lines (121 loc) · 4.6 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/bash
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print section headers
print_header() {
echo -e "\n${BLUE}=== $1 ===${NC}\n"
}
# Function to print success messages
print_success() {
echo -e "${GREEN}✓ $1${NC}"
}
# Function to print error messages
print_error() {
echo -e "${RED}✗ $1${NC}"
}
# Function to print info messages
print_info() {
echo -e "${YELLOW}ℹ $1${NC}"
}
# Function to wait with progress indicator
wait_with_progress() {
local resource=$1
local condition=$2
local timeout=$3
local start_time=$(date +%s)
local dots=""
local last_status=""
while true; do
# Special handling for Pending state
if [ "${condition}" = "Pending" ]; then
local current_state=$(kubectl get ${resource} -o jsonpath='{.status.phase}' 2>/dev/null)
if [ "${current_state}" = "Pending" ]; then
echo -e "\n"
return 0
fi
else
# For pods, get detailed status
if [[ "${resource}" == pod/* ]]; then
local pod_status=$(kubectl get ${resource} -o jsonpath='{.status.phase}' 2>/dev/null)
local pod_conditions=$(kubectl get ${resource} -o jsonpath='{.status.conditions[*].type}' 2>/dev/null)
local pod_events=$(kubectl get events --field-selector involvedObject.name=${resource#pod/} --sort-by='.lastTimestamp' -o jsonpath='{.items[-1].reason}:{.items[-1].message}' 2>/dev/null)
# Only print if status has changed
if [ "${pod_status}" != "${last_status}" ]; then
echo -e "\n${YELLOW}Pod Status: ${pod_status}${NC}"
if [ ! -z "${pod_conditions}" ]; then
echo -e "${YELLOW}Conditions: ${pod_conditions}${NC}"
fi
if [ ! -z "${pod_events}" ]; then
echo -e "${YELLOW}Latest Event: ${pod_events}${NC}"
fi
last_status="${pod_status}"
fi
fi
if kubectl wait --for=condition=${condition} ${resource} --timeout=5s 2>/dev/null; then
echo -e "\n"
return 0
fi
fi
# Update progress indicator
dots="${dots}."
if [ ${#dots} -gt 3 ]; then
dots=""
fi
echo -ne "\r${YELLOW}Waiting for ${resource} to be ${condition}${dots}${NC}"
# Check if timeout exceeded
local current_time=$(date +%s)
if [ $((current_time - start_time)) -ge $timeout ]; then
echo -e "\n"
print_error "Timeout waiting for ${resource} to be ${condition}"
# Check for errors
local error_msg=$(kubectl get ${resource} -o jsonpath='{.status.conditions[?(@.type=="Failed")].message}')
if [ ! -z "$error_msg" ]; then
print_error "Error details: ${error_msg}"
fi
# For pods, show events on timeout
if [[ "${resource}" == pod/* ]]; then
print_error "Pod events:"
kubectl get events --field-selector involvedObject.name=${resource#pod/} --sort-by='.lastTimestamp' | tail -n 5
fi
return 1
fi
sleep 2
done
}
# Function to clean up resources with progress indicator
cleanup_resource() {
local resource=$1
local timeout=${2:-30}
if kubectl get ${resource} &>/dev/null; then
print_info "Found existing ${resource}, removing..."
kubectl delete ${resource} --ignore-not-found
local start_time=$(date +%s)
local dots=""
while true; do
if ! kubectl get ${resource} &>/dev/null; then
echo -e "\n"
print_success "${resource} deleted successfully"
return 0
fi
# Update progress indicator
dots="${dots}."
if [ ${#dots} -gt 3 ]; then
dots=""
fi
echo -ne "\r${YELLOW}Waiting for ${resource} to be deleted${dots}${NC}"
# Check if timeout exceeded
local current_time=$(date +%s)
if [ $((current_time - start_time)) -ge $timeout ]; then
echo -e "\n"
print_error "Timeout waiting for ${resource} to be deleted"
return 1
fi
sleep 2
done
fi
return 0
}