@@ -15,11 +15,14 @@ CACERT=${SERVICEACCOUNT}/ca.crt
1515RETRIES=6
1616WAIT=1
1717
18+
1819# #
1920# # Utilities functions
2021# #
22+ # # NOTE: mysql diverts this script's stdout, but stderr is logged to the
23+ # # configured log-error file (e.g. /var/log/mariadb/mariadb.log)
2124function log() {
22- echo " $( date +%F_%H_%M_%S) ` basename $0 ` $* "
25+ echo " $( date +%F_%H_%M_%S) ` basename $0 ` $* " >&2
2326}
2427
2528function log_error() {
@@ -65,6 +68,11 @@ function api_server {
6568 if echo " ${output} " | grep -q ' "status": "Failure"' ; then
6669 message=$( echo " ${output} " | parse_output ' ["message"]' )
6770 code=$( echo " ${output} " | parse_output ' ["code"]' )
71+ if [ " ${code} " = 401 ]; then
72+ # Unauthorized means the token is no longer valid as the galera
73+ # resource is in the process of being deleted.
74+ return 2
75+ fi
6876 log_error " API server returned an error for service ${SERVICE} : ${message} (code=${code} )"
6977 return 1
7078 fi
@@ -98,9 +106,16 @@ function retry {
98106 local retries=$RETRIES
99107 local wait=$WAIT
100108 local rc=1
109+
101110 $action
102111 rc=$?
103112 while [ $rc -ne 0 -a $retries -gt 0 ]; do
113+ # if API call are unauthorized, the resource is being deleted
114+ # exit now as there is nothing more to do
115+ if [ $rc -eq 2 ]; then
116+ log " galera resource is being deleted, exit now."
117+ return 0
118+ fi
104119 log_error " previous action failed, retrying."
105120 sleep $wait
106121 $action
@@ -129,7 +144,8 @@ function reconfigure_service_endpoint {
129144 fi
130145
131146 CURRENT_SVC=$( api_server GET " $SERVICE " )
132- [ $? == 0 ] || return 1
147+ local rc=$?
148+ [ $rc == 0 ] || return $rc
133149
134150 CURRENT_ENDPOINT=$( echo " $CURRENT_SVC " | parse_output ' ["spec"]["selector"].get("statefulset.kubernetes.io/pod-name","")' )
135151 [ $? == 0 ] || return 1
@@ -151,7 +167,8 @@ function reconfigure_service_endpoint {
151167# # Change the Active endpoint from the service
152168function remove_service_endpoint {
153169 CURRENT_SVC=$( api_server GET " $SERVICE " )
154- [ $? == 0 ] || return 1
170+ local rc=$?
171+ [ $rc == 0 ] || return $rc
155172
156173 CURRENT_ENDPOINT=$( echo " $CURRENT_SVC " | parse_output ' ["spec"]["selector"].get("statefulset.kubernetes.io/pod-name","")' )
157174 [ $? == 0 ] || return 1
@@ -174,9 +191,6 @@ function remove_service_endpoint {
174191
175192# # Main
176193
177- # mysql diverts this script's stdout/stderr, so in order for its output
178- # to be logged properly, reuse dumb-init's stdout
179- exec & > >( tee -a /proc/1/fd/1) 2>&1
180194log " called with args: $* "
181195
182196# Galera always calls script with --status argument
0 commit comments