Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
fix race condition bug with kubectl returning nodes (#1889)
Browse files Browse the repository at this point in the history
  • Loading branch information
trstringer authored and jackfrancis committed Dec 6, 2017
1 parent 7f3e25c commit ed6a196
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion extensions/prometheus-grafana-k8s/v1/prometheus-grafana-k8s.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,35 @@ then
exit 1
fi

master_nodes() {
kubectl get no -L kubernetes.io/role -l kubernetes.io/role=master --no-headers -o jsonpath="{.items[*].metadata.name}" | tr " " "\n" | sort | head -n 1
}

wait_for_master_nodes() {
ATTEMPTS=90
SLEEP_TIME=10

ITERATION=0
while [[ $ITERATION -lt $ATTEMPTS ]]; do
echo $(date) " - Is kubectl returning master nodes? (attempt $(( $ITERATION + 1 )) of $ATTEMPTS)"

FIRST_K8S_MASTER=$(master_nodes)

if [[ -n $FIRST_K8S_MASTER ]]; then
echo $(date) " - kubectl is returning master nodes"
return
fi

ITERATION=$(( $ITERATION + 1 ))
sleep $SLEEP_TIME
done

echo $(date) " - kubectl failed to return master nodes in the alotted time"
return 1
}

should_this_node_run_extension() {
FIRST_K8S_MASTER=$(kubectl get no -L kubernetes.io/role -l kubernetes.io/role=master --no-headers -o jsonpath="{.items[*].metadata.name}" | tr " " "\n" | sort | head -n 1)
FIRST_K8S_MASTER=$(master_nodes)
if [[ $FIRST_K8S_MASTER = $(hostname) ]]; then
echo $(date) " - Local node $(hostname) is found to be the first master node $FIRST_K8S_MASTER"
return
Expand Down Expand Up @@ -180,6 +207,11 @@ ensure_k8s_namespace_exists() {
# should run the extension is to alphabetically determine
# if this local machine is the first in the list of master nodes
# if it is, then run the extension. if not, exit
wait_for_master_nodes
if [[ $? -ne 0 ]]; then
echo $(date) " - Error while waiting for kubectl to output master nodes. Exiting"
exit 1
fi
should_this_node_run_extension
if [[ $? -ne 0 ]]; then
echo $(date) " - Not the first master node, no longer continuing extension. Exiting"
Expand Down

0 comments on commit ed6a196

Please sign in to comment.