22set -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
2216port=" ${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} " )
2519if [ -n " ${VALKEY_DEFAULT_PASSWORD} " ]; then
26- base_cmd +=(-a " ${VALKEY_DEFAULT_PASSWORD} " )
20+ cli_cmd +=(-a " ${VALKEY_DEFAULT_PASSWORD} " )
2721fi
2822if [ -n " ${VALKEY_CLI_TLS_ARGS} " ]; then
2923 # shellcheck disable=SC2206
30- base_cmd +=(${VALKEY_CLI_TLS_ARGS} )
24+ cli_cmd +=(${VALKEY_CLI_TLS_ARGS} )
3125fi
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
4434fi
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}
0 commit comments