[COMMON][ETCD] Skip startup self-discovery for etcd nodes
[oom.git] / kubernetes / common / etcd / templates / statefulset.yaml
1 # Copyright © 2019 Intel Corporation Inc
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 apiVersion: apps/v1beta1
15 kind: StatefulSet
16 metadata:
17   name: {{ include "common.fullname" .  }}
18   labels:
19     heritage: "{{ .Release.Service }}"
20     release: "{{ include "common.release" . }}"
21     chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
22     app: {{ include "common.name" . }}
23 spec:
24   serviceName: {{ include "common.servicename" .}}
25   replicas: {{ .Values.replicaCount }}
26   template:
27     metadata:
28       labels:
29         heritage: "{{ .Release.Service }}"
30         release: "{{ include "common.release" . }}"
31         chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
32         app: {{ include "common.name" . }}
33     spec:
34 {{- if .Values.affinity }}
35       affinity:
36 {{ toYaml .Values.affinity | indent 8 }}
37 {{- end }}
38 {{- if .Values.nodeSelector }}
39       nodeSelector:
40 {{ toYaml .Values.nodeSelector | indent 8 }}
41 {{- end }}
42 {{- if .Values.tolerations }}
43       tolerations:
44 {{ toYaml .Values.tolerations | indent 8 }}
45 {{- end }}
46       containers:
47       - name: {{ include "common.fullname" .  }}
48         image: "{{ .Values.repository }}/{{ .Values.image }}"
49         imagePullPolicy: "{{ .Values.pullPolicy }}"
50         ports:
51         - containerPort: {{ .Values.service.peerInternalPort }}
52           name: {{ .Values.service.peerPortName }}
53         - containerPort: {{ .Values.service.clientInternalPort }}
54           name: {{ .Values.service.clientPortName }}
55         {{- if eq .Values.liveness.enabled true }}
56         livenessProbe:
57           tcpSocket:
58             port: {{ .Values.service.clientInternalPort }}
59           initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
60           periodSeconds: {{ .Values.liveness.periodSeconds }}
61           timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
62         {{ end -}}
63         resources:
64 {{ include "common.resources" . | indent 10 }}
65         env:
66         - name: INITIAL_CLUSTER_SIZE
67           value: {{ .Values.replicaCount | quote }}
68         - name: SET_NAME
69           value: {{ include "common.fullname" . }}
70         - name: SERVICE_NAME
71           value: {{ include "common.servicename" . }}
72 {{- if .Values.extraEnv }}
73 {{ toYaml .Values.extraEnv | indent 8 }}
74 {{- end }}
75         lifecycle:
76           preStop:
77             exec:
78               command:
79                 - "/bin/sh"
80                 - "-ec"
81                 - |
82                   EPS=""
83                   for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
84                       EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
85                   done
86
87                   HOSTNAME=$(hostname)
88
89                   member_hash() {
90                       etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
91                   }
92
93                   SET_ID=${HOSTNAME##*[^0-9]}
94
95                   if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then
96                       echo "Removing ${HOSTNAME} from etcd cluster"
97                       ETCDCTL_ENDPOINT=${EPS} etcdctl member remove $(member_hash)
98                       if [ $? -eq 0 ]; then
99                           # Remove everything otherwise the cluster will no longer scale-up
100                           rm -rf /var/run/etcd/*
101                       fi
102                   fi
103         command:
104           - "/bin/sh"
105           - "-ec"
106           - |
107             HOSTNAME=$(hostname)
108
109             # store member id into PVC for later member replacement
110             collect_member() {
111                 while ! etcdctl member list &>/dev/null; do sleep 1; done
112                 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1 > /var/run/etcd/member_id
113                 exit 0
114             }
115
116             eps() {
117                 EPS=""
118                 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
119                     EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
120                 done
121                 echo ${EPS}
122             }
123
124             member_hash() {
125                 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
126             }
127
128             # we should wait for other pods to be up before trying to join
129             # otherwise we got "no such host" errors when trying to resolve other members
130             for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
131                 if [ "${SET_NAME}-${i}" == "${HOSTNAME}" ]; then
132                     echo "Skipping self-checking"
133                     continue
134                 fi
135                 while true; do
136                     echo "Waiting for ${SET_NAME}-${i}.${SERVICE_NAME} to come up"
137                     ping -W 1 -c 1 ${SET_NAME}-${i}.${SERVICE_NAME} > /dev/null && break
138                     sleep 1s
139                 done
140             done
141
142             # re-joining after failure?
143             if [[ -e /var/run/etcd/default.etcd && -f /var/run/etcd/member_id ]]; then
144                 echo "Re-joining etcd member"
145                 member_id=$(cat /var/run/etcd/member_id)
146
147                 # re-join member
148                 ETCDCTL_ENDPOINT=$(eps) etcdctl member update ${member_id} http://${HOSTNAME}.${SERVICE_NAME}:2380 | true
149                 exec etcd --name ${HOSTNAME} \
150                     --listen-peer-urls http://0.0.0.0:2380 \
151                     --listen-client-urls http://0.0.0.0:2379\
152                     --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
153                     --data-dir /var/run/etcd/default.etcd
154             fi
155
156             # etcd-SET_ID
157             SET_ID=${HOSTNAME##*[^0-9]}
158
159             # adding a new member to existing cluster (assuming all initial pods are available)
160             if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then
161                 export ETCDCTL_ENDPOINT=$(eps)
162
163                 # member already added?
164                 MEMBER_HASH=$(member_hash)
165                 if [ -n "${MEMBER_HASH}" ]; then
166                     # the member hash exists but for some reason etcd failed
167                     # as the datadir has not be created, we can remove the member
168                     # and retrieve new hash
169                     etcdctl member remove ${MEMBER_HASH}
170                 fi
171
172                 echo "Adding new member"
173                 etcdctl member add ${HOSTNAME} http://${HOSTNAME}.${SERVICE_NAME}:2380 | grep "^ETCD_" > /var/run/etcd/new_member_envs
174
175                 if [ $? -ne 0 ]; then
176                     echo "Exiting"
177                     rm -f /var/run/etcd/new_member_envs
178                     exit 1
179                 fi
180
181                 cat /var/run/etcd/new_member_envs
182                 source /var/run/etcd/new_member_envs
183
184                 collect_member &
185
186                 exec etcd --name ${HOSTNAME} \
187                     --listen-peer-urls http://0.0.0.0:2380 \
188                     --listen-client-urls http://0.0.0.0:2379 \
189                     --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
190                     --data-dir /var/run/etcd/default.etcd \
191                     --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
192                     --initial-cluster ${ETCD_INITIAL_CLUSTER} \
193                     --initial-cluster-state ${ETCD_INITIAL_CLUSTER_STATE}
194             fi
195
196             PEERS=""
197             for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
198                 PEERS="${PEERS}${PEERS:+,}${SET_NAME}-${i}=http://${SET_NAME}-${i}.${SERVICE_NAME}:2380"
199             done
200
201             collect_member &
202
203             # join member
204             exec etcd --name ${HOSTNAME} \
205                 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
206                 --listen-peer-urls http://0.0.0.0:2380 \
207                 --listen-client-urls http://0.0.0.0:2379 \
208                 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
209                 --initial-cluster-token etcd-cluster-1 \
210                 --initial-cluster ${PEERS} \
211                 --initial-cluster-state new \
212                 --data-dir /var/run/etcd/default.etcd
213         volumeMounts:
214         - name: {{ include "common.fullname" . }}-data
215           mountPath: /var/run/etcd
216   {{- if .Values.persistence.enabled }}
217   volumeClaimTemplates:
218   - metadata:
219       name: {{ include "common.fullname" . }}-data
220       labels:
221         name: {{ include "common.fullname" . }}
222         chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
223         release: "{{ include "common.release" . }}"
224         heritage: "{{ .Release.Service }}"
225     spec:
226       accessModes:
227       - "{{ .Values.persistence.accessMode }}"
228       storageClassName: {{ include "common.storageClass" . }}
229       resources:
230         requests:
231           # upstream recommended max is 700M
232           storage: "{{ .Values.persistence.storage }}"
233   {{- else }}
234       volumes:
235       - name: {{ include "common.fullname" . }}-data
236       {{- if .Values.memoryMode }}
237         emptyDir:
238           medium: Memory
239       {{- else }}
240         emptyDir: {}
241       {{- end }}
242   {{- end }}