Fix the bug of failure to write due to not sufficient good replicas in SA DBAAS
[ric-plt/ric-dep.git] / helm / dbaas / templates / configmap.yaml
1 ################################################################################
2 #   Copyright (c) 2020 AT&T Intellectual Property.                             #
3 #   Copyright (c) 2020 Nokia.                                                  #
4 #                                                                              #
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                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
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 ################################################################################
17
18 apiVersion: v1
19 kind: ConfigMap
20 metadata:
21   name: {{ template "common.configmapname.dbaas" . }}-config
22   labels:
23     heritage: {{ .Release.Service }}
24     release: {{ .Release.Name }}
25     chart: {{ .Chart.Name }}-{{ .Chart.Version }}
26     app: {{ include "common.namespace.platform" . }}-{{ include "common.name.dbaas" . }}
27 data:
28   redis.conf: |
29     dir "/data"
30 {{- if $.Values.dbaas.enableHighAvailability }}
31     {{- range $key, $value := .Values.dbaas.redis.ha_config }}
32     {{ $key }} {{ $value }}
33     {{- end }}
34 {{- else }}
35     {{- range $key, $value := .Values.dbaas.redis.sa_config }}
36     {{ $key }} {{ $value }}
37     {{- end }}
38 {{- end }}
39
40 {{- if $.Values.dbaas.enableHighAvailability }}
41   sentinel.conf: |
42     dir "/data"
43     {{- $root := . -}}
44     {{- range $key, $value := .Values.dbaas.sentinel.config }}
45     sentinel {{ $key }} {{ $root.Values.dbaas.redis.masterGroupName }} {{ $value }}
46     {{- end }}
47     protected-mode {{ index .Values.dbaas.sentinel "protected-mode" }}
48
49   init.sh: |
50     HOSTNAME="$(hostname)"
51     INDEX="${HOSTNAME##*-}"
52     MASTER="$(redis-cli -h {{ template "common.servicename.dbaas.tcp" . }}.{{ include "common.namespace.platform" . }} -p {{ include "common.serviceport.dbaas.sentinel" .  }} sentinel get-master-addr-by-name {{ .Values.dbaas.redis.masterGroupName }} | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')"
53     MASTER_GROUP="{{ .Values.dbaas.redis.masterGroupName }}"
54     QUORUM="{{ .Values.dbaas.sentinel.quorum }}"
55     REDIS_CONF=/data/conf/redis.conf
56     REDIS_PORT={{ include "common.serviceport.dbaas.redis" . }}
57     SENTINEL_CONF=/data/conf/sentinel.conf
58     SENTINEL_PORT={{ include "common.serviceport.dbaas.sentinel" . }}
59     SERVICE={{ include "common.servicename.dbaas.tcp" . }}
60     set -eu
61
62     sentinel_update() {
63         echo "Updating sentinel config"
64         sed -i "1s/^/$(cat sentinel-id)\\n/" "$SENTINEL_CONF"
65         sed -i "2s/^/sentinel monitor $MASTER_GROUP $1 $REDIS_PORT $QUORUM \\n/" "$SENTINEL_CONF"
66         echo "sentinel announce-ip $ANNOUNCE_IP" >> $SENTINEL_CONF
67         echo "sentinel announce-port $SENTINEL_PORT" >> $SENTINEL_CONF
68     }
69
70     redis_update() {
71         echo "Updating redis config"
72         echo "slaveof $1 $REDIS_PORT" >> "$REDIS_CONF"
73         echo "slave-announce-ip $ANNOUNCE_IP" >> $REDIS_CONF
74         echo "slave-announce-port $REDIS_PORT" >> $REDIS_CONF
75     }
76
77     redis_master_update() {
78         echo "Updating redis default master config"
79         echo "slave-announce-ip $ANNOUNCE_IP" >> $REDIS_CONF
80         echo "slave-announce-port $REDIS_PORT" >> $REDIS_CONF
81     }
82
83     copy_config() {
84         if [ -f "$SENTINEL_CONF" ]; then
85             grep "sentinel myid" "$SENTINEL_CONF" > sentinel-id || true
86         fi
87         cp /readonly-config/redis.conf "$REDIS_CONF"
88         cp /readonly-config/sentinel.conf "$SENTINEL_CONF"
89     }
90
91     setup_defaults() {
92         echo "Setting up defaults"
93         if [ "$INDEX" = "0" ]; then
94             echo "Setting this pod as the default master"
95             sed -i "s/^.*slaveof.*//" "$REDIS_CONF"
96             redis_master_update
97             sentinel_update "$ANNOUNCE_IP"
98         else
99             DEFAULT_MASTER="$(getent hosts "{{ include "common.statefulsetname.dbaas" . }}-server-0.$SERVICE" | awk '{ print $1 }')"
100             if [ -z "$DEFAULT_MASTER" ]; then
101                 echo "Unable to resolve host"
102                 exit 1
103             fi
104             echo "Setting default slave config.."
105             redis_update "$DEFAULT_MASTER"
106             sentinel_update "$DEFAULT_MASTER"
107         fi
108     }
109
110     find_master() {
111         echo "Attempting to find master"
112         if [ "$(redis-cli -h "$MASTER"{{ if .Values.auth }} -a "$AUTH"{{ end }} ping)" != "PONG" ]; then
113            echo "Can't ping master, attempting to force failover"
114            if redis-cli -h "$SERVICE" -p "$SENTINEL_PORT" sentinel failover "$MASTER_GROUP" | grep -q 'NOGOODSLAVE' ; then
115                setup_defaults
116                return 0
117            fi
118            sleep 10
119            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}')"
120            if [ "$MASTER" ]; then
121                sentinel_update "$MASTER"
122                redis_update "$MASTER"
123            else
124               echo "Could not failover, exiting..."
125               exit 1
126            fi
127         else
128             echo "Found reachable master, updating config"
129             sentinel_update "$MASTER"
130             redis_update "$MASTER"
131         fi
132     }
133
134     mkdir -p /data/conf/
135
136     echo "Initializing config.."
137     copy_config
138
139     #    ANNOUNCE_IP=$(getent hosts "{{ include "common.statefulsetname.dbaas" . }}-server-$INDEX.$SERVICE" | awk '{ print $1 }')
140     if [ -z "$ANNOUNCE_IP" ]; then
141         "Could not resolve the announce ip for this pod"
142         exit 1
143     elif [ "$MASTER" ]; then
144         find_master
145     else
146         setup_defaults
147     fi
148
149     if [ "${AUTH:-}" ]; then
150         echo "Setting auth values"
151         sed -i "s/replace-default-auth/$AUTH/" "$REDIS_CONF" "$SENTINEL_CONF"
152     fi
153
154     echo "Ready..."
155
156 {{- end }}