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