Skip to content

Commit 7133892

Browse files
authored
fix(valkey): push ACL to all data pods in accountProvision (#2872)
1 parent e01bf8b commit 7133892

2 files changed

Lines changed: 15 additions & 27 deletions

File tree

addons/valkey/scripts/account-provision.sh

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,29 @@
22
set -e
33
# account-provision.sh — called by KubeBlocks to create/update a database account.
44
#
5-
# Learning note:
6-
# KubeBlocks injects three special variables before calling this action:
7-
# KB_ACCOUNT_NAME - logical account name (e.g., "default")
8-
# KB_ACCOUNT_PASSWORD - generated password (from passwordGenerationPolicy)
9-
# KB_ACCOUNT_STATEMENT - the command to execute, built by KubeBlocks
5+
# KubeBlocks injects:
6+
# KB_ACCOUNT_NAME - logical account name (e.g., "default")
7+
# KB_ACCOUNT_PASSWORD - generated password (from passwordGenerationPolicy)
8+
# KB_ACCOUNT_STATEMENT - the command to execute (ACL SETUSER ...)
109
#
11-
# For Redis/Valkey, KB_ACCOUNT_STATEMENT is an ACL SETUSER command:
12-
# e.g. "ACL SETUSER myuser on >password ~* +@all"
13-
#
14-
# Because Valkey's auth model is ACL-based (not SQL), the provision script
15-
# simply passes KB_ACCOUNT_STATEMENT verbatim to valkey-cli and then
16-
# calls ACL SAVE to persist the ACL file to disk.
17-
#
18-
# The `initAccount: true` flag on the systemAccount means KubeBlocks
19-
# calls this action once during cluster initialisation, using the
20-
# "default" user. Subsequent ACL changes go through OpsRequest.
10+
# With targetPodSelector: All, KubeBlocks runs this on every pod.
11+
# Each pod applies the ACL locally via ACL SETUSER + ACL SAVE.
12+
# Valkey ACL is not replicated via the native replication stream —
13+
# each node maintains its own users.acl, so every pod must be
14+
# provisioned independently.
2115

2216
port="${SERVICE_PORT:-6379}"
2317

24-
base_cmd=(valkey-cli --no-auth-warning -p "${port}")
18+
cli_cmd=(valkey-cli --no-auth-warning -h 127.0.0.1 -p "${port}")
2519
if [ -n "${VALKEY_DEFAULT_PASSWORD}" ]; then
26-
base_cmd+=(-a "${VALKEY_DEFAULT_PASSWORD}")
20+
cli_cmd+=(-a "${VALKEY_DEFAULT_PASSWORD}")
2721
fi
2822
if [ -n "${VALKEY_CLI_TLS_ARGS}" ]; then
2923
# shellcheck disable=SC2206
30-
base_cmd+=(${VALKEY_CLI_TLS_ARGS})
24+
cli_cmd+=(${VALKEY_CLI_TLS_ARGS})
3125
fi
3226

33-
# Execute the statement provided by KubeBlocks.
34-
# Pipe via stdin so that '>' in ACL password syntax (e.g. >mypassword) is not
35-
# misinterpreted as a shell output-redirect operator.
36-
# valkey-cli exits 0 even for protocol errors; capture output and check content.
37-
output=$(echo "${KB_ACCOUNT_STATEMENT}" | "${base_cmd[@]}" 2>&1) || {
27+
output=$(echo "${KB_ACCOUNT_STATEMENT}" | "${cli_cmd[@]}" 2>&1) || {
3828
echo "ERROR: account statement failed: ${output}" >&2
3929
exit 1
4030
}
@@ -43,8 +33,7 @@ if [ "${output}" != "OK" ]; then
4333
exit 1
4434
fi
4535

46-
# Persist ACL to disk so it survives pod restarts
47-
acl_save_out=$("${base_cmd[@]}" ACL SAVE 2>&1) || {
36+
acl_save_out=$("${cli_cmd[@]}" ACL SAVE 2>&1) || {
4837
echo "ERROR: ACL SAVE failed: ${acl_save_out}" >&2
4938
exit 1
5039
}

addons/valkey/templates/cmpd.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,7 @@ spec:
446446
- sh
447447
- -c
448448
- /scripts/account-provision.sh
449-
targetPodSelector: Role
450-
matchingKey: primary
449+
targetPodSelector: All
451450

452451
# switchover: when Sentinel is present, delegates to SENTINEL FAILOVER.
453452
# When absent (standalone), performs a direct REPLICAOF NO ONE.

0 commit comments

Comments
 (0)