DBaaS standalone and HA version update
[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     {{- range $key, $value := .Values.dbaas.redis.config }}
31     {{ $key }} {{ $value }}
32     {{- end }}
33
34 {{- if $.Values.dbaas.enableHighAvailability }}
35   sentinel.conf: |
36     dir "/data"
37     {{- $root := . -}}
38     {{- range $key, $value := .Values.dbaas.sentinel.config }}
39     sentinel {{ $key }} {{ $root.Values.dbaas.redis.masterGroupName }} {{ $value }}
40     {{- end }}
41     protected-mode {{ index .Values.dbaas.sentinel "protected-mode" }}
42
43   init.sh: |
44     HOSTNAME="$(hostname)"
45     INDEX="${HOSTNAME##*-}"
46     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}')"
47     MASTER_GROUP="{{ .Values.dbaas.redis.masterGroupName }}"
48     QUORUM="{{ .Values.dbaas.sentinel.quorum }}"
49     REDIS_CONF=/data/conf/redis.conf
50     REDIS_PORT={{ include "common.serviceport.dbaas.redis" . }}
51     SENTINEL_CONF=/data/conf/sentinel.conf
52     SENTINEL_PORT={{ include "common.serviceport.dbaas.sentinel" . }}
53     SERVICE={{ include "common.servicename.dbaas.tcp" . }}
54     set -eu
55
56     sentinel_update() {
57         echo "Updating sentinel config"
58         sed -i "1s/^/$(cat sentinel-id)\\n/" "$SENTINEL_CONF"
59         sed -i "2s/^/sentinel monitor $MASTER_GROUP $1 $REDIS_PORT $QUORUM \\n/" "$SENTINEL_CONF"
60         echo "sentinel announce-ip $ANNOUNCE_IP" >> $SENTINEL_CONF
61         echo "sentinel announce-port $SENTINEL_PORT" >> $SENTINEL_CONF
62     }
63
64     redis_update() {
65         echo "Updating redis config"
66         echo "slaveof $1 $REDIS_PORT" >> "$REDIS_CONF"
67         echo "slave-announce-ip $ANNOUNCE_IP" >> $REDIS_CONF
68         echo "slave-announce-port $REDIS_PORT" >> $REDIS_CONF
69     }
70
71     redis_master_update() {
72         echo "Updating redis default master config"
73         echo "slave-announce-ip $ANNOUNCE_IP" >> $REDIS_CONF
74         echo "slave-announce-port $REDIS_PORT" >> $REDIS_CONF
75     }
76
77     copy_config() {
78         if [ -f "$SENTINEL_CONF" ]; then
79             grep "sentinel myid" "$SENTINEL_CONF" > sentinel-id || true
80         fi
81         cp /readonly-config/redis.conf "$REDIS_CONF"
82         cp /readonly-config/sentinel.conf "$SENTINEL_CONF"
83     }
84
85     setup_defaults() {
86         echo "Setting up defaults"
87         if [ "$INDEX" = "0" ]; then
88             echo "Setting this pod as the default master"
89             sed -i "s/^.*slaveof.*//" "$REDIS_CONF"
90             redis_master_update
91             sentinel_update "$ANNOUNCE_IP"
92         else
93             DEFAULT_MASTER="$(getent hosts "{{ include "common.statefulsetname.dbaas" . }}-server-0.$SERVICE" | awk '{ print $1 }')"
94             if [ -z "$DEFAULT_MASTER" ]; then
95                 echo "Unable to resolve host"
96                 exit 1
97             fi
98             echo "Setting default slave config.."
99             redis_update "$DEFAULT_MASTER"
100             sentinel_update "$DEFAULT_MASTER"
101         fi
102     }
103
104     find_master() {
105         echo "Attempting to find master"
106         if [ "$(redis-cli -h "$MASTER"{{ if .Values.auth }} -a "$AUTH"{{ end }} ping)" != "PONG" ]; then
107            echo "Can't ping master, attempting to force failover"
108            if redis-cli -h "$SERVICE" -p "$SENTINEL_PORT" sentinel failover "$MASTER_GROUP" | grep -q 'NOGOODSLAVE' ; then
109                setup_defaults
110                return 0
111            fi
112            sleep 10
113            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}')"
114            if [ "$MASTER" ]; then
115                sentinel_update "$MASTER"
116                redis_update "$MASTER"
117            else
118               echo "Could not failover, exiting..."
119               exit 1
120            fi
121         else
122             echo "Found reachable master, updating config"
123             sentinel_update "$MASTER"
124             redis_update "$MASTER"
125         fi
126     }
127
128     mkdir -p /data/conf/
129
130     echo "Initializing config.."
131     copy_config
132
133     #    ANNOUNCE_IP=$(getent hosts "{{ include "common.statefulsetname.dbaas" . }}-server-$INDEX.$SERVICE" | awk '{ print $1 }')
134     if [ -z "$ANNOUNCE_IP" ]; then
135         "Could not resolve the announce ip for this pod"
136         exit 1
137     elif [ "$MASTER" ]; then
138         find_master
139     else
140         setup_defaults
141     fi
142
143     if [ "${AUTH:-}" ]; then
144         echo "Setting auth values"
145         sed -i "s/replace-default-auth/$AUTH/" "$REDIS_CONF" "$SENTINEL_CONF"
146     fi
147
148     echo "Ready..."
149
150 {{- end }}