1 ################################################################################
2 # Copyright (c) 2019 AT&T Intellectual Property. #
3 # Copyright (c) 2019 Nokia. #
5 # Licensed under the Apache License, Version 2.0 (the "License"); #
6 # you may not use this file except in compliance with the License. #
7 # You may obtain a copy of the License at #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
11 # Unless required by applicable law or agreed to in writing, software #
12 # distributed under the License is distributed on an "AS IS" BASIS, #
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
14 # See the License for the specific language governing permissions and #
15 # limitations under the License. #
16 ################################################################################
21 name: {{ template "common.configmapname.dbaasha" . }}-config
23 heritage: {{ .Release.Service }}
24 release: {{ .Release.Name }}
25 chart: {{ .Chart.Name }}-{{ .Chart.Version }}
26 app: {{ include "common.namespace.platform" . }}-{{ include "common.name.dbaasha" . }}
30 {{- range $key, $value := .Values.dbaasha.redis.config }}
31 {{ $key }} {{ $value }}
37 {{- range $key, $value := .Values.dbaasha.sentinel.config }}
38 sentinel {{ $key }} {{ $root.Values.dbaasha.redis.masterGroupName }} {{ $value }}
40 protected-mode {{ index .Values.dbaasha.sentinel "protected-mode" }}
43 HOSTNAME="$(hostname)"
44 INDEX="${HOSTNAME##*-}"
45 MASTER="$(redis-cli -h {{ template "common.servicename.dbaasha.tcp" . }}.{{ .Release.Namespace }}.svc.cluster.local -p {{ include "common.serviceport.dbaasha.sentinel" . }} sentinel get-master-addr-by-name {{ .Values.dbaasha.redis.masterGroupName }} | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')"
46 MASTER_GROUP="{{ .Values.dbaasha.redis.masterGroupName }}"
47 QUORUM="{{ .Values.dbaasha.sentinel.quorum }}"
48 REDIS_CONF=/data/conf/redis.conf
49 REDIS_PORT={{ include "common.serviceport.dbaasha.redis" . }}
50 SENTINEL_CONF=/data/conf/sentinel.conf
51 SENTINEL_PORT={{ include "common.serviceport.dbaasha.sentinel" . }}
52 SERVICE={{ template "common.fullname.dbaasha" . }}
56 echo "Updating sentinel config"
57 sed -i "1s/^/$(cat sentinel-id)\\n/" "$SENTINEL_CONF"
58 sed -i "2s/^/sentinel monitor $MASTER_GROUP $1 $REDIS_PORT $QUORUM \\n/" "$SENTINEL_CONF"
59 echo "sentinel announce-ip $ANNOUNCE_IP" >> $SENTINEL_CONF
60 echo "sentinel announce-port $SENTINEL_PORT" >> $SENTINEL_CONF
64 echo "Updating redis config"
65 echo "slaveof $1 $REDIS_PORT" >> "$REDIS_CONF"
66 echo "slave-announce-ip $ANNOUNCE_IP" >> $REDIS_CONF
67 echo "slave-announce-port $REDIS_PORT" >> $REDIS_CONF
70 redis_master_update() {
71 echo "Updating redis default master config"
72 echo "slave-announce-ip $ANNOUNCE_IP" >> $REDIS_CONF
73 echo "slave-announce-port $REDIS_PORT" >> $REDIS_CONF
77 if [ -f "$SENTINEL_CONF" ]; then
78 grep "sentinel myid" "$SENTINEL_CONF" > sentinel-id || true
80 cp /readonly-config/redis.conf "$REDIS_CONF"
81 cp /readonly-config/sentinel.conf "$SENTINEL_CONF"
85 echo "Setting up defaults"
86 if [ "$INDEX" = "0" ]; then
87 echo "Setting this pod as the default master"
88 sed -i "s/^.*slaveof.*//" "$REDIS_CONF"
90 sentinel_update "$ANNOUNCE_IP"
92 DEFAULT_MASTER="$(getent hosts "$SERVICE-announce-0" | awk '{ print $1 }')"
93 if [ -z "$DEFAULT_MASTER" ]; then
94 echo "Unable to resolve host"
97 echo "Setting default slave config.."
98 redis_update "$DEFAULT_MASTER"
99 sentinel_update "$DEFAULT_MASTER"
104 echo "Attempting to find master"
105 if [ "$(redis-cli -h "$MASTER"{{ if .Values.auth }} -a "$AUTH"{{ end }} ping)" != "PONG" ]; then
106 echo "Can't ping master, attempting to force failover"
107 if redis-cli -h "$SERVICE" -p "$SENTINEL_PORT" sentinel failover "$MASTER_GROUP" | grep -q 'NOGOODSLAVE' ; then
112 MASTER="$(redis-cli -h $SERVICE -p $SENTINEL_PORT sentinel get-master-addr-by-name $MASTER_GROUP | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')"
113 if [ "$MASTER" ]; then
114 sentinel_update "$MASTER"
115 redis_update "$MASTER"
117 echo "Could not failover, exiting..."
121 echo "Found reachable master, updating config"
122 sentinel_update "$MASTER"
123 redis_update "$MASTER"
129 echo "Initializing config.."
132 ANNOUNCE_IP=$(getent hosts "$SERVICE-announce-$INDEX" | awk '{ print $1 }')
133 if [ -z "$ANNOUNCE_IP" ]; then
134 "Could not resolve the announce ip for this pod"
136 elif [ "$MASTER" ]; then
142 if [ "${AUTH:-}" ]; then
143 echo "Setting auth values"
144 sed -i "s/replace-default-auth/$AUTH/" "$REDIS_CONF" "$SENTINEL_CONF"