Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/preview/workflow/lib/k8s-util.sh
2500 views
1
#!/usr/bin/env bash
2
3
# this script is meant to be sourced
4
5
function waitUntilAllPodsAreReady {
6
local namespace
7
local exitCode
8
local kube_path
9
local kube_context
10
11
kube_path="$1"
12
kube_context="$2"
13
namespace="$3"
14
15
echo "Waiting until all pods in namespace ${namespace} are Running/Succeeded/Completed."
16
local attempts=0
17
local successful=false
18
while [ ${attempts} -lt 200 ]
19
do
20
attempts=$((attempts+1))
21
set +e
22
pods=$(
23
kubectl \
24
--kubeconfig "${kube_path}" \
25
--context "${kube_context}" \
26
get pods -n "${namespace}" \
27
-l 'component!=workspace' \
28
-o=jsonpath='{range .items[*]}{@.metadata.name}:{@.metadata.ownerReferences[0].kind}:{@.status.phase} {end}'
29
)
30
exitCode=$?
31
set -e
32
if [[ $exitCode -gt 0 ]]; then
33
echo "Failed to get pods in namespace. Exit code $exitCode"
34
echo "Sleeping 3 seconds"
35
sleep 3
36
continue
37
fi
38
39
if [[ -z "${pods}" ]]; then
40
echo "The namespace is empty or does not exist."
41
echo "Sleeping 3 seconds"
42
sleep 3
43
continue
44
fi
45
46
unreadyPods=""
47
for pod in $pods; do
48
owner=$(echo "$pod" | cut -d ":" -f 2)
49
phase=$(echo "$pod" | cut -d ":" -f 3)
50
if [[ $owner == "Job" && $phase != "Succeeded" ]]; then
51
unreadyPods="$pod $unreadyPods"
52
fi
53
if [[ $owner != "Job" && $phase != "Running" ]]; then
54
unreadyPods="$pod $unreadyPods"
55
fi
56
done
57
58
if [[ -z "${unreadyPods}" ]]; then
59
echo "All pods are Running/Succeeded/Completed!"
60
successful="true"
61
break
62
fi
63
64
echo "Unready pods: $unreadyPods"
65
echo "Sleeping 10 seconds before checking again"
66
sleep 10
67
done
68
69
if [[ "${successful}" == "true" ]]; then
70
return 0
71
else
72
echo "Not all pods in namespace ${namespace} transitioned to 'Running' or 'Succeeded/Completed' during the expected time."
73
return 1
74
fi
75
}
76
77
function diff-apply {
78
local context=$1
79
shift
80
local yaml=$1
81
yaml=$(realpath "${yaml}")
82
83
if kubectl --context "${context}" diff -f "${yaml}" > /dev/null; then
84
echo "Skipping ${yaml}, as it produced no diff"
85
else
86
kubectl --context "${context}" apply --server-side --force-conflicts -f "${yaml}"
87
fi
88
}
89
90