feat(node): support for name replacement
Some checks failed
Build and Push Image / Build and push image (push) Failing after 13s
Some checks failed
Build and Push Image / Build and push image (push) Failing after 13s
now when you include this project you can change then service and statefulset name from the valkey-env configMap
This commit is contained in:
parent
36e6cd84f9
commit
3c550b3015
@ -2,6 +2,7 @@
|
||||
{{- $service := env "VALKEY_SERVICE" }}
|
||||
{{- $namespace := env "NAMESPACE" }}
|
||||
{{- $port := env "VALKEY_PORT" }}
|
||||
{{- $name := env "VALKEY_NAME" }}
|
||||
{{- $sentinel_port := env "VALKEY_SENTINEL_PORT" }}
|
||||
{{- $replicas := env "VALKEY_REPLICAS" }}
|
||||
{{- $domain := printf "%s.%s.svc.cluster.local" $service $namespace }}
|
||||
@ -36,7 +37,7 @@ sentinel leader-epoch mymaster 0
|
||||
sentinel current-epoch 0
|
||||
|
||||
{{- range $i, $e := until ( int $replicas ) }}
|
||||
{{- $ndeHostname := printf "valkey-node-%d" $i }}
|
||||
{{- $ndeHostname := printf "%s-%d" $name $i }}
|
||||
{{- $ndeFQDN := printf "%s.%s" $ndeHostname $domain }}
|
||||
{{- $nodeID := sha1sum $ndeHostname }}
|
||||
{{- if (ne $fqdn $ndeFQDN) }}
|
||||
|
@ -1,151 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
. /opt/bitnami/scripts/libos.sh
|
||||
. /opt/bitnami/scripts/liblog.sh
|
||||
. /opt/bitnami/scripts/libvalidations.sh
|
||||
|
||||
get_port() {
|
||||
hostname="$1"
|
||||
type="$2"
|
||||
|
||||
port_var=$(echo "${hostname^^}_SERVICE_PORT_$type" | sed "s/-/_/g")
|
||||
port=${!port_var}
|
||||
|
||||
if [ -z "$port" ]; then
|
||||
case $type in
|
||||
"SENTINEL")
|
||||
echo 26379
|
||||
;;
|
||||
"VALKEY")
|
||||
echo 6379
|
||||
;;
|
||||
esac
|
||||
else
|
||||
echo $port
|
||||
fi
|
||||
}
|
||||
|
||||
get_full_hostname() {
|
||||
hostname="$1"
|
||||
full_hostname="${hostname}.${HEADLESS_SERVICE}"
|
||||
echo "${full_hostname}"
|
||||
}
|
||||
|
||||
VALKEYPORT=$(get_port "$HOSTNAME" "VALKEY")
|
||||
|
||||
HEADLESS_SERVICE="valkey-headless.mse-dev.svc.cluster.local"
|
||||
|
||||
if [ -n "$VALKEY_EXTERNAL_PRIMARY_HOST" ]; then
|
||||
VALKEY_SERVICE="$VALKEY_EXTERNAL_PRIMARY_HOST"
|
||||
else
|
||||
VALKEY_SERVICE="valkey.mse-dev.svc.cluster.local"
|
||||
fi
|
||||
|
||||
SENTINEL_SERVICE_PORT=$(get_port "valkey" "SENTINEL")
|
||||
validate_quorum() {
|
||||
if is_boolean_yes "$VALKEY_TLS_ENABLED"; then
|
||||
quorum_info_command="valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT --tls --cert ${VALKEY_TLS_CERT_FILE} --key ${VALKEY
|
||||
_TLS_KEY_FILE} --cacert ${VALKEY_TLS_CA_FILE} sentinel primary mymaster"
|
||||
else
|
||||
quorum_info_command="valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT sentinel primary mymaster"
|
||||
fi
|
||||
info "about to run the command: $quorum_info_command"
|
||||
eval $quorum_info_command | grep -Fq "s_down"
|
||||
}
|
||||
|
||||
trigger_manual_failover() {
|
||||
if is_boolean_yes "$VALKEY_TLS_ENABLED"; then
|
||||
failover_command="valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT --tls --cert ${VALKEY_TLS_CERT_FILE} --key ${VALKEY_TL
|
||||
S_KEY_FILE} --cacert ${VALKEY_TLS_CA_FILE} sentinel failover mymaster"
|
||||
else
|
||||
failover_command="valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT sentinel failover mymaster"
|
||||
fi
|
||||
|
||||
info "about to run the command: $failover_command"
|
||||
eval $failover_command
|
||||
}
|
||||
|
||||
get_sentinel_primary_info() {
|
||||
if is_boolean_yes "$VALKEY_TLS_ENABLED"; then
|
||||
sentinel_info_command="timeout 90 valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT --tls --cert ${VALKEY_TLS_CERT_FILE} -
|
||||
-key ${VALKEY_TLS_KEY_FILE} --cacert ${VALKEY_TLS_CA_FILE} sentinel get-primary-addr-by-name mymaster"
|
||||
else
|
||||
sentinel_info_command="timeout 90 valkey-cli -h $VALKEY_SERVICE -p $SENTINEL_SERVICE_PORT sentinel get-primary-addr-by-name myma
|
||||
ster"
|
||||
fi
|
||||
info "about to run the command: $sentinel_info_command"
|
||||
retry_while "eval $sentinel_info_command" 2 5
|
||||
}
|
||||
|
||||
[[ -f $VALKEY_PASSWORD_FILE ]] && export VALKEY_PASSWORD="$(< "${VALKEY_PASSWORD_FILE}")"
|
||||
[[ -f $VALKEY_PRIMARY_PASSWORD_FILE ]] && export VALKEY_PRIMARY_PASSWORD="$(< "${VALKEY_PRIMARY_PASSWORD_FILE}")"
|
||||
|
||||
# check if there is a primary
|
||||
primary_in_persisted_conf="$(get_full_hostname "$HOSTNAME")"
|
||||
primary_port_in_persisted_conf="$VALKEY_PRIMARY_PORT_NUMBER"
|
||||
primary_in_sentinel="$(get_sentinel_primary_info)"
|
||||
valkeyRetVal=$?
|
||||
|
||||
if [[ -f /opt/bitnami/valkey-sentinel/etc/sentinel.conf ]]; then
|
||||
primary_in_persisted_conf="$(awk '/monitor/ {print $4}' /opt/bitnami/valkey-sentinel/etc/sentinel.conf)"
|
||||
primary_port_in_persisted_conf="$(awk '/monitor/ {print $5}' /opt/bitnami/valkey-sentinel/etc/sentinel.conf)"
|
||||
info "Found previous primary ${primary_in_persisted_conf}:${primary_port_in_persisted_conf} in /opt/bitnami/valkey-sentinel/etc/sent
|
||||
inel.conf"
|
||||
debug "$(cat /opt/bitnami/valkey-sentinel/etc/sentinel.conf | grep monitor)"
|
||||
fi
|
||||
|
||||
if [[ $valkeyRetVal -ne 0 ]]; then
|
||||
if [[ "$primary_in_persisted_conf" == "$(get_full_hostname "$HOSTNAME")" ]]; then
|
||||
# Case 1: No active sentinel and in previous sentinel.conf we were the primary --> PRIMARY
|
||||
info "Configuring the node as primary"
|
||||
export VALKEY_REPLICATION_MODE="primary"
|
||||
else
|
||||
# Case 2: No active sentinel and in previous sentinel.conf we were not primary --> REPLICA
|
||||
info "Configuring the node as replica"
|
||||
export VALKEY_REPLICATION_MODE="replica"
|
||||
VALKEY_PRIMARY_HOST=${primary_in_persisted_conf}
|
||||
VALKEY_PRIMARY_PORT_NUMBER=${primary_port_in_persisted_conf}
|
||||
fi
|
||||
else
|
||||
# Fetches current primary's host and port
|
||||
VALKEY_SENTINEL_INFO=($(get_sentinel_primary_info))
|
||||
info "Current primary: VALKEY_SENTINEL_INFO=(${VALKEY_SENTINEL_INFO[0]},${VALKEY_SENTINEL_INFO[1]})"
|
||||
VALKEY_PRIMARY_HOST=${VALKEY_SENTINEL_INFO[0]}
|
||||
VALKEY_PRIMARY_PORT_NUMBER=${VALKEY_SENTINEL_INFO[1]}
|
||||
|
||||
if [[ "$VALKEY_PRIMARY_HOST" == "$(get_full_hostname "$HOSTNAME")" ]]; then
|
||||
# Case 3: Active sentinel and primary it is this node --> PRIMARY
|
||||
info "Configuring the node as primary"
|
||||
export VALKEY_REPLICATION_MODE="primary"
|
||||
else
|
||||
# Case 4: Active sentinel and primary is not this node --> REPLICA
|
||||
info "Configuring the node as replica"
|
||||
export VALKEY_REPLICATION_MODE="replica"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ -n "$VALKEY_EXTERNAL_PRIMARY_HOST" ]]; then
|
||||
VALKEY_PRIMARY_HOST="$VALKEY_EXTERNAL_PRIMARY_HOST"
|
||||
VALKEY_PRIMARY_PORT_NUMBER="${VALKEY_EXTERNAL_PRIMARY_PORT}"
|
||||
fi
|
||||
|
||||
if [[ -f /opt/bitnami/valkey/mounted-etc/replica.conf ]];then
|
||||
cp /opt/bitnami/valkey/mounted-etc/replica.conf /opt/bitnami/valkey/etc/replica.conf
|
||||
fi
|
||||
|
||||
if [[ -f /opt/bitnami/valkey/mounted-etc/valkey.conf ]];then
|
||||
cp /opt/bitnami/valkey/mounted-etc/valkey.conf /opt/bitnami/valkey/etc/valkey.conf
|
||||
fi
|
||||
|
||||
echo "" >> /opt/bitnami/valkey/etc/replica.conf
|
||||
echo "replica-announce-port $VALKEYPORT" >> /opt/bitnami/valkey/etc/replica.conf
|
||||
echo "replica-announce-ip $(get_full_hostname "$HOSTNAME")" >> /opt/bitnami/valkey/etc/replica.conf
|
||||
ARGS=("--port" "${VALKEY_PORT}")
|
||||
|
||||
if [[ "$VALKEY_REPLICATION_MODE" = "replica" ]]; then
|
||||
ARGS+=("--replicaof" "${VALKEY_PRIMARY_HOST}" "${VALKEY_PRIMARY_PORT_NUMBER}")
|
||||
fi
|
||||
ARGS+=("--protected-mode" "no")
|
||||
ARGS+=("--include" "/opt/bitnami/valkey/etc/replica.conf")
|
||||
ARGS+=("--include" "/opt/bitnami/valkey/etc/valkey.conf")
|
||||
exec valkey-server "${ARGS[@]}"
|
@ -6,15 +6,14 @@ resources:
|
||||
- resources/statefulset.yaml
|
||||
- resources/svc.yaml
|
||||
|
||||
replacements:
|
||||
- path: ./replacements/replicas.yaml
|
||||
|
||||
configMapGenerator:
|
||||
- name: valkey-env
|
||||
literals:
|
||||
- NAMESPACE="default"
|
||||
- VALKEY_ENV="base"
|
||||
- VALKEY_SERVICE="valkey-headless"
|
||||
- VALKEY_NAME="valkey-node"
|
||||
- VALKEY_SERVICE="valkey"
|
||||
- VALKEY_HEADLESS_SERVICE="valkey-headless"
|
||||
- VALKEY_REPLICAS="4"
|
||||
- VALKEY_PORT="6379"
|
||||
- VALKEY_SENTINEL_PORT="26379"
|
||||
@ -38,3 +37,7 @@ configMapGenerator:
|
||||
- files/scripts/ping-sentinel.sh
|
||||
- files/scripts/liveness-local.sh
|
||||
- files/scripts/readiness-local.sh
|
||||
|
||||
replacements:
|
||||
- path: ./replacements/services.yaml
|
||||
- path: ./replacements/statefulset.yaml
|
||||
|
@ -1,10 +0,0 @@
|
||||
- source:
|
||||
kind: ConfigMap
|
||||
name: valkey-env
|
||||
fieldPath: data.VALKEY_REPLICAS
|
||||
targets:
|
||||
- select:
|
||||
kind: StatefulSet
|
||||
name: valkey-node
|
||||
fieldPaths:
|
||||
- spec.replicas
|
20
resources/node/replacements/services.yaml
Normal file
20
resources/node/replacements/services.yaml
Normal file
@ -0,0 +1,20 @@
|
||||
- source:
|
||||
kind: ConfigMap
|
||||
name: valkey-env
|
||||
fieldPath: data.VALKEY_HEADLESS_SERVICE
|
||||
targets:
|
||||
- select:
|
||||
kind: Service
|
||||
name: valkey-headless
|
||||
fieldPaths:
|
||||
- metadata.name
|
||||
- source:
|
||||
kind: ConfigMap
|
||||
name: valkey-env
|
||||
fieldPath: data.VALKEY_SERVICE
|
||||
targets:
|
||||
- select:
|
||||
kind: Service
|
||||
name: valkey
|
||||
fieldPaths:
|
||||
- metadata.name
|
30
resources/node/replacements/statefulset.yaml
Normal file
30
resources/node/replacements/statefulset.yaml
Normal file
@ -0,0 +1,30 @@
|
||||
- source:
|
||||
kind: ConfigMap
|
||||
name: valkey-env
|
||||
fieldPath: data.VALKEY_REPLICAS
|
||||
targets:
|
||||
- select:
|
||||
kind: StatefulSet
|
||||
name: valkey-node
|
||||
fieldPaths:
|
||||
- spec.replicas
|
||||
- source:
|
||||
kind: ConfigMap
|
||||
name: valkey-env
|
||||
fieldPath: data.VALKEY_HEADLESS_SERVICE
|
||||
targets:
|
||||
- select:
|
||||
kind: StatefulSet
|
||||
name: valkey-node
|
||||
fieldPaths:
|
||||
- spec.serviceName
|
||||
- source:
|
||||
kind: ConfigMap
|
||||
name: valkey-env
|
||||
fieldPath: data.VALKEY_NAME
|
||||
targets:
|
||||
- select:
|
||||
kind: StatefulSet
|
||||
name: valkey-node
|
||||
fieldPaths:
|
||||
- metadata.name
|
Loading…
x
Reference in New Issue
Block a user