Skip to content

Commit b4d18d3

Browse files
committed
Implement preStop lifecycle handler
1 parent ffe20a7 commit b4d18d3

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
set -e
4+
5+
VALKEY_ROLE="$(valkey-cli -c info replication 2>/dev/null | awk '/^role:/ { print $1 }' | tr -d '\r')"
6+
7+
if [ "${VALKEY_ROLE}" != "role:master" ]; then
8+
echo "ROLE IS NOT MASTER"
9+
exit 0
10+
fi
11+
12+
CONNTECTED_SLAVES="$(valkey-cli -c info replication | awk -F: '/^connected_slaves:/ { print $2 }')"
13+
14+
if [ "$CONNTECTED_SLAVES" -le "0" ]; then
15+
echo "Connected slaves is 0, therefore nothing to do"
16+
exit 0
17+
fi
18+
19+
SLAVE0="$(valkey-cli -c info replication | awk -F: '/^slave0:/ { print $2 }')"
20+
SLAVE_IP="$(echo "${SLAVE0}" | tr , '\n' | awk -F= '/^ip/ { print $2 }')"
21+
SLAVE_PORT="$(echo "${SLAVE0}" | tr , '\n' | awk -F= '/^port/ { print $2 }')"
22+
SLAVE_STATUS="$(echo "${SLAVE0}" | tr , '\n' | awk -F= '/^state/ { print $2 }')"
23+
if [ "${SLAVE_STATUS}" != "online" ]; then
24+
echo "Slave status is $SLAVE_STATUS, therefore nothing to do"
25+
exit 0
26+
fi
27+
28+
echo "Pause writes"
29+
valkey-cli -c CLIENT PAUSE "5000" WRITE
30+
31+
valkey-cli -h "${SLAVE_IP}" -p "${SLAVE_PORT}" -c cluster failover
32+
sleep 3s

internal/controller/valkey_controller.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,11 @@ func (r *ValkeyReconciler) upsertConfigMap(ctx context.Context, valkey *hyperv1.
390390
logger.Error(err, "failed to read ping_liveness_local.sh")
391391
return err
392392
}
393+
preStop, err := scripts.ReadFile("scripts/pre_stop.sh")
394+
if err != nil {
395+
logger.Error(err, "failed to read pre_stop.sh")
396+
return err
397+
}
393398
cm := &corev1.ConfigMap{
394399
ObjectMeta: metav1.ObjectMeta{
395400
Name: valkey.Name,
@@ -400,6 +405,7 @@ func (r *ValkeyReconciler) upsertConfigMap(ctx context.Context, valkey *hyperv1.
400405
"valkey.conf": conf.String(),
401406
"ping_readiness_local.sh": string(pingReadinessLocal),
402407
"ping_liveness_local.sh": string(pingLivenessLocal),
408+
"pre_stop.sh": string(preStop),
403409
},
404410
}
405411
if valkey.Spec.TLS {
@@ -2342,6 +2348,17 @@ func (r *ValkeyReconciler) upsertStatefulSet(ctx context.Context, valkey *hyperv
23422348
},
23432349
},
23442350
},
2351+
Lifecycle: &corev1.Lifecycle{
2352+
PreStop: &corev1.LifecycleHandler{
2353+
Exec: &corev1.ExecAction{
2354+
Command: []string{
2355+
"sh",
2356+
"-c",
2357+
"/scripts/pre_stop.sh",
2358+
},
2359+
},
2360+
},
2361+
},
23452362
Resources: getResourceRequirements(valkey),
23462363
VolumeMounts: []corev1.VolumeMount{
23472364
{

0 commit comments

Comments
 (0)