Merge "Support for dbaas(HA/SA) in deployment"
[ric-plt/ric-dep.git] / helm / dbaasha / templates / configmap.yaml
1 ################################################################################
2 #   Copyright (c) 2019 AT&T Intellectual Property.                             #
3 #   Copyright (c) 2019 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.dbaasha" . }}-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.dbaasha" . }}
27 data:
28   redis.conf: |
29     dir "/data"
30     {{- range $key, $value := .Values.dbaasha.redis.config }}
31     {{ $key }} {{ $value }}
32     {{- end }}
33
34   sentinel.conf: |
35     dir "/data"
36     {{- $root := . -}}
37     {{- range $key, $value := .Values.dbaasha.sentinel.config }}
38     sentinel {{ $key }} {{ $root.Values.dbaasha.redis.masterGroupName }} {{ $value }}
39     {{- end }}
40     protected-mode {{ index .Values.dbaasha.sentinel "protected-mode" }}
41
42   init.sh: |
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" . }}
53     set -eu
54
55     sentinel_update() {
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
61     }
62
63     redis_update() {
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
68     }
69
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
74     }
75
76     copy_config() {
77         if [ -f "$SENTINEL_CONF" ]; then
78             grep "sentinel myid" "$SENTINEL_CONF" > sentinel-id || true
79         fi
80         cp /readonly-config/redis.conf "$REDIS_CONF"
81         cp /readonly-config/sentinel.conf "$SENTINEL_CONF"
82     }
83
84     setup_defaults() {
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"
89             redis_master_update
90             sentinel_update "$ANNOUNCE_IP"
91         else
92             DEFAULT_MASTER="$(getent hosts "$SERVICE-announce-0" | awk '{ print $1 }')"
93             if [ -z "$DEFAULT_MASTER" ]; then
94                 echo "Unable to resolve host"
95                 exit 1
96             fi
97             echo "Setting default slave config.."
98             redis_update "$DEFAULT_MASTER"
99             sentinel_update "$DEFAULT_MASTER"
100         fi
101     }
102
103     find_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
108                setup_defaults
109                return 0
110            fi
111            sleep 10
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"
116            else
117               echo "Could not failover, exiting..."
118               exit 1
119            fi
120         else
121             echo "Found reachable master, updating config"
122             sentinel_update "$MASTER"
123             redis_update "$MASTER"
124         fi
125     }
126
127     mkdir -p /data/conf/
128
129     echo "Initializing config.."
130     copy_config
131
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"
135         exit 1
136     elif [ "$MASTER" ]; then
137         find_master
138     else
139         setup_defaults
140     fi
141
142     if [ "${AUTH:-}" ]; then
143         echo "Setting auth values"
144         sed -i "s/replace-default-auth/$AUTH/" "$REDIS_CONF" "$SENTINEL_CONF"
145     fi
146
147     echo "Ready..."