Kubelet healthcheck reimagined.
[ta/caas-kubernetes.git] / ansible / roles / kubelet / templates / kubelet_healthcheck.sh
1 #!/bin/bash
2 # Copyright 2019 Nokia
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 error=0
17
18 while true
19 do
20   set +e
21   result="$(curl 127.0.0.1:{{ kubelet_healthcheck_port }}/healthz)"
22   set -e
23   if [ "$result" == "ok" ]
24   then
25     echo "Healtcheck success."
26     error=0
27     set +e
28     uncordonresult="$(/usr/bin/kubectl uncordon {{ ansible_host }} 2>&1)"
29     set -e
30     echo "$uncordonresult"
31   else
32     echo "Healtcheck failed."
33     error=$(($error+1))
34   fi
35   if [ "$error" -ge "5" ]
36   then
37     activeState="$(systemctl show -p ActiveState --value kubelet)"
38     if [[ "$activeState" == "deactivating" ]] || [[ "$activeState" == "activating" ]]
39     then
40       echo "Kubelet is possibly restarting."
41       error=0
42     else
43       echo "Error with kubelet (Healtcheck failed 5 times) restarting it."
44       systemctl restart kubelet.service
45     fi
46   fi
47   sleep 1
48 done