Skip to content

Commit

Permalink
Add wait_kubeapi function
Browse files Browse the repository at this point in the history
  • Loading branch information
macropin committed Oct 5, 2023
1 parent f72b1bc commit 39edc46
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions functions/wait_kubeapi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# wait_kubeapi [RETRIES]
# wait_kubeapi 30
wait_kubeapi() {
# Wait for Kubernetes API to be available
command -v kubectl >/dev/null 2>&1 || { error "This function requires kubectl to be installed."; return 1; }
local retries="${1:-30}"
echo -n "Connecting to Kubernetes API"
for (( i=0;; i++ )); do
if [[ ${i} -eq "${retries}" ]]; then
echo " timeout!"
return 99
fi
sleep 1
(kubectl version) &>/dev/null && break
echo -n "."
done
echo " connected."
exec 3>&-
exec 3<&-
}
24 changes: 24 additions & 0 deletions tests/wait_kubeapi.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bats

source ../functions/10-common.sh
source ../functions/wait_kubeapi.sh

@test "wait_kubeapi: connecting to mock kubeapi" {
kubectl() {
echo "Mocked kubectl command"
sleep 1
}
run wait_kubeapi 5
[ "$status" -eq 0 ]
[[ "${lines[0]}" == "Connecting to Kubernetes API"* ]]
}

@test "wait_kubeapi: timeout connecting mock kubeapi" {
kubectl() {
echo "Mocked kubectl command"
exit 1
}
run wait_kubeapi 1
[ "$status" -eq 99 ]
[[ "${lines[0]}" == *". timeout!" ]]
}

0 comments on commit 39edc46

Please sign in to comment.