Skip to content

Commit b37545d

Browse files
committed
Do not log an error during resource deletion
A notify script is being called by the galera library each time it transitions into another state (e.g. Connected -> Synced). The notify script uses a token generated by k8s to communicate with the API server and update the current endpoint for the galera service. When a galera resource is deleted, the token becomes invalid, so when the notify script is called, it cannot connect to the API server anymore. This is not needed as the resource is being deleted anyway, so handle that case to avoid logging unecessary errors.
1 parent a06f656 commit b37545d

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

templates/galera/bin/mysql_wsrep_notify.sh

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ CACERT=${SERVICEACCOUNT}/ca.crt
1515
RETRIES=6
1616
WAIT=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)
2124
function log() {
22-
echo "$(date +%F_%H_%M_%S) `basename $0` $*"
25+
echo "$(date +%F_%H_%M_%S) `basename $0` $*" >&2
2326
}
2427

2528
function 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
152168
function 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
180194
log "called with args: $*"
181195

182196
# Galera always calls script with --status argument

0 commit comments

Comments
 (0)