2014-07-29 04:42:53 +00:00
#!/bin/bash
2015-05-01 16:19:44 +00:00
# Copyright 2014 The Kubernetes Authors All rights reserved.
2014-07-29 04:42:53 +00:00
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2015-04-21 20:30:16 +00:00
# Validates that the cluster is healthy.
2014-07-29 04:42:53 +00:00
2014-10-06 20:25:27 +00:00
set -o errexit
set -o nounset
set -o pipefail
2014-07-29 04:42:53 +00:00
2014-10-03 21:58:49 +00:00
KUBE_ROOT = $( dirname " ${ BASH_SOURCE } " ) /..
source " ${ KUBE_ROOT } /cluster/kube-env.sh "
source " ${ KUBE_ROOT } /cluster/ ${ KUBERNETES_PROVIDER } /util.sh "
2014-07-29 04:42:53 +00:00
2015-02-04 23:56:52 +00:00
MINIONS_FILE = /tmp/minions-$$
trap 'rm -rf "${MINIONS_FILE}"' EXIT
2015-04-21 20:30:16 +00:00
2015-01-14 00:03:30 +00:00
# Make several attempts to deal with slow cluster birth.
2014-12-09 23:37:06 +00:00
attempt = 0
while true; do
2015-05-07 22:50:11 +00:00
# The "kubectl get nodes" output is three columns like this:
#
# NAME LABELS STATUS
# kubernetes-minion-03nb <none> Ready
#
# Echo the output, strip the first line, then gather 2 counts:
# - Total number of nodes.
# - Number of "ready" nodes.
" ${ KUBE_ROOT } /cluster/kubectl.sh " get nodes > " ${ MINIONS_FILE } " || true
found = $( cat " ${ MINIONS_FILE } " | sed '1d' | grep -c .) || true
ready = $( cat " ${ MINIONS_FILE } " | sed '1d' | awk '{print $NF}' | grep -c '^Ready' ) || true
if ( ( ${ found } = = " ${ NUM_MINIONS } " ) ) && ( ( ${ ready } = = " ${ NUM_MINIONS } " ) ) ; then
2014-12-09 23:37:06 +00:00
break
else
if ( ( attempt > 5 ) ) ; then
2015-05-07 22:50:11 +00:00
echo -e " ${ color_red } Detected ${ ready } ready nodes, found ${ found } nodes out of expected ${ NUM_MINIONS } . Your cluster may not be working. ${ color_norm } "
2015-02-04 23:56:52 +00:00
cat -n " ${ MINIONS_FILE } "
2014-12-09 23:37:06 +00:00
exit 2
fi
attempt = $(( attempt+1))
sleep 30
fi
done
echo " Found ${ found } nodes. "
2015-02-04 23:56:52 +00:00
cat -n " ${ MINIONS_FILE } "
2014-07-29 04:42:53 +00:00
2015-04-21 20:30:16 +00:00
attempt = 0
while true; do
2015-04-28 21:57:14 +00:00
kubectl_output = $( " ${ KUBE_ROOT } /cluster/kubectl.sh " get cs) || true
2015-03-25 03:49:55 +00:00
2015-04-21 20:30:16 +00:00
# The "kubectl componentstatuses" output is four columns like this:
#
# COMPONENT HEALTH MSG ERR
# controller-manager Healthy ok nil
#
# Parse the output to capture the value of the second column("HEALTH"), then use grep to
2015-05-07 22:50:11 +00:00
# count the number of times it doesn't match "Healthy".
2015-04-21 20:30:16 +00:00
non_success_count = $( echo " ${ kubectl_output } " | \
2015-05-07 22:50:11 +00:00
sed '1d' |
2015-04-21 22:57:05 +00:00
sed -n 's/^[[:alnum:][:punct:]]/&/p' | \
2015-04-28 21:57:14 +00:00
grep --invert-match -c '^[[:alnum:][:punct:]]\{1,\}[[:space:]]\{1,\}Healthy' ) || true
2014-10-17 21:48:11 +00:00
2015-05-07 22:50:11 +00:00
if ( ( non_success_count > 0) ) ; then
2015-04-21 20:30:16 +00:00
if ( ( attempt < 5) ) ; then
echo -e " ${ color_yellow } Cluster not working yet. ${ color_norm } "
2014-12-09 23:37:06 +00:00
attempt = $(( attempt+1))
sleep 30
2015-04-21 20:30:16 +00:00
else
echo -e " ${ color_yellow } Validate output: ${ color_norm } "
echo " ${ kubectl_output } "
echo -e " ${ color_red } Validation returned one or more failed components. Cluster is probably broken. ${ color_norm } "
exit 1
fi
else
break
fi
2014-07-29 04:42:53 +00:00
done
2015-04-21 20:30:16 +00:00
echo "Validate output:"
echo " ${ kubectl_output } "
2014-12-09 23:37:06 +00:00
echo -e " ${ color_green } Cluster validation succeeded ${ color_norm } "