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

fix race condition bug with kubectl returning nodes #1889

Merged
merged 1 commit into from
Dec 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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