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