Skip to content

Commit 0179df2

Browse files
Merge pull request #177 from haisamido/master
a change to ./containers/kubernetes/kubernetes.sh by Rob J.
2 parents d2ac35b + 24ec1c6 commit 0179df2

File tree

1 file changed

+49
-2
lines changed

1 file changed

+49
-2
lines changed

containers/kubernetes/kubernetes.sh

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ REALM=${OU}-${ENVIRO}-${CONTEXT}
55

66
MISSION=m01
77
SPACECRAFT=sc01
8+
# Timeouts for starting the cluster
9+
TIMEOUT_SECONDS=300 # 5 minutes
10+
SLEEP_INTERVAL=15 # Wait 15 seconds between checks
811

912
# resource names, to be used with context, cluster, namespace, pods, and services
1013
RESOURCE_NAME=${REALM}-${MISSION}
@@ -47,8 +50,50 @@ kubectl create deployment ${K8S_DEPLOYMENT} --context ${K8S_CONTEXT} --namespace
4750
--image=${IMAGE_URI} \
4851
--replicas=1
4952

50-
echo "Sleeping for 120 seconds to ensure that pod(s) are up and running"
51-
sleep 120
53+
echo "Waiting for pod in namespace ${K8S_NAMESPACE} to be in a 'Running' state..."
54+
55+
start_time=$(date +%s)
56+
while true; do
57+
# Use kubectl with jq to find the pod and get its status and name
58+
# We select the pod whose name starts with our prefix
59+
pod_info=$(kubectl get pods -n "$K8S_NAMESPACE" -o json 2>/dev/null | \
60+
jq -r '.items[] | select(.metadata.name | startswith("'$K8S_NAMESPACE'")) | {name: .metadata.name, status: .status.phase}')
61+
62+
# Check if a pod was found and get its name and status
63+
if [ -n "$pod_info" ]; then
64+
POD_NAME=$(echo "$pod_info" | jq -r '.name')
65+
pod_status=$(echo "$pod_info" | jq -r '.status')
66+
else
67+
POD_NAME=""
68+
pod_status=""
69+
fi
70+
71+
# Check if the pod is in the "Running" status
72+
if [ "$pod_status" == "Running" ]; then
73+
echo "Success: Pod '$POD_NAME' is now running."
74+
break # Exit the loop
75+
fi
76+
77+
# Check for timeout
78+
current_time=$(date +%s)
79+
elapsed_time=$((current_time - start_time))
80+
if [ "$elapsed_time" -ge "$TIMEOUT_SECONDS" ]; then
81+
if [ -z "$POD_NAME" ]; then
82+
echo "Error: Timeout reached ($TIMEOUT_SECONDS seconds). No pod with prefix '$POD_NAME_PREFIX' was found."
83+
else
84+
echo "Error: Timeout reached ($TIMEOUT_SECONDS seconds). Pod '$POD_NAME' status is '$pod_status'."
85+
fi
86+
exit 1
87+
fi
88+
89+
# Report current status and wait before the next check
90+
if [ -n "$POD_NAME" ]; then
91+
echo "Current status for '$POD_NAME': '$pod_status'. Waiting..."
92+
else
93+
echo "No pod with prefix '$POD_NAME_PREFIX' found yet. Waiting..."
94+
fi
95+
sleep "$SLEEP_INTERVAL"
96+
done
5297

5398
kubectl scale deployment ${K8S_DEPLOYMENT} --context ${K8S_CONTEXT} --namespace ${K8S_NAMESPACE} --replicas=1
5499

@@ -65,3 +110,5 @@ kubectl expose deployment ${K8S_DEPLOYMENT} --context ${K8S_CONTEXT} --namespace
65110
# port-forwarding
66111
kubectl port-forward service/${K8S_SERVICE} --context ${K8S_CONTEXT} --namespace ${K8S_NAMESPACE} \
67112
${APP_EXTERNAL_PORT}:${APP_INTERNAL_PORT} &
113+
114+
echo "open browser to http://localhost:${APP_EXTERNAL_PORT}/vnc.html and click on 'Connect'"

0 commit comments

Comments
 (0)