Merge "[AAI] Add model-loader tracing config"
[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       {{- include "common.imagePullSecrets" . | nindent 6 }}
39       containers:
40       - name: {{ include "common.name" .  }}
41         image: {{ include "repositoryGenerator.googleK8sRepository" . }}/{{ .Values.image }}
42         imagePullPolicy: "{{ .Values.pullPolicy }}"
43         ports:
44         - containerPort: {{ .Values.service.peerInternalPort }}
45           name: {{ .Values.service.peerPortName }}
46         - containerPort: {{ .Values.service.clientInternalPort }}
47           name: {{ .Values.service.clientPortName }}
48         {{- if eq .Values.liveness.enabled true }}
49         livenessProbe:
50           tcpSocket:
51             port: {{ .Values.service.clientInternalPort }}
52           initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
53           periodSeconds: {{ .Values.liveness.periodSeconds }}
54           timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
55         {{ end -}}
56         resources: {{ include "common.resources" . | nindent 10 }}
57         env:
58         - name: INITIAL_CLUSTER_SIZE
59           value: {{ .Values.replicaCount | quote }}
60         - name: SET_NAME
61           value: {{ include "common.fullname" . }}
62         - name: SERVICE_NAME
63           value: {{ include "common.servicename" . }}.{{ include "common.namespace" . }}.svc.{{ .Values.global.clusterName }}
64 {{- if .Values.extraEnv }}
65 {{ toYaml .Values.extraEnv | indent 8 }}
66 {{- end }}
67         lifecycle:
68           preStop:
69             exec:
70               command:
71                 - "/bin/sh"
72                 - "-ec"
73                 - |
74                   EPS=""
75                   for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
76                       EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
77                   done
78
79                   HOSTNAME=$(hostname)
80
81                   member_hash() {
82                       etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
83                   }
84
85                   SET_ID=${HOSTNAME##*[^0-9]}
86
87                   if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then
88                       echo "Removing ${HOSTNAME} from etcd cluster"
89                       ETCDCTL_ENDPOINT=${EPS} etcdctl member remove $(member_hash)
90                       if [ $? -eq 0 ]; then
91                           # Remove everything otherwise the cluster will no longer scale-up
92                           rm -rf /var/run/etcd/*
93                       fi
94                   fi
95         command:
96           - "/bin/sh"
97           - "-ec"
98           - |
99             HOSTNAME=$(hostname)
100
101             # store member id into PVC for later member replacement
102             collect_member() {
103                 while ! etcdctl member list &>/dev/null; do sleep 1; done
104                 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1 > /var/run/etcd/member_id
105                 exit 0
106             }
107
108             eps() {
109                 EPS=""
110                 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
111                     EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
112                 done
113                 echo ${EPS}
114             }
115
116             member_hash() {
117                 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
118             }
119
120             # we should wait for other pods to be up before trying to join
121             # otherwise we got "no such host" errors when trying to resolve other members
122             for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
123                 if [ "${SET_NAME}-${i}" == "${HOSTNAME}" ]; then
124                     echo "Skipping self-checking"
125                     continue
126                 fi
127                 while true; do
128                     echo "Waiting for ${SET_NAME}-${i}.${SERVICE_NAME} to come up"
129                     ping -W 1 -c 1 ${SET_NAME}-${i}.${SERVICE_NAME} > /dev/null && break
130                     sleep 1s
131                 done
132             done
133
134             # re-joining after failure?
135             if [[ -e /var/run/etcd/default.etcd && -f /var/run/etcd/member_id ]]; then
136                 echo "Re-joining etcd member"
137                 member_id=$(cat /var/run/etcd/member_id)
138
139                 # re-join member
140                 ETCDCTL_ENDPOINT=$(eps) etcdctl member update ${member_id} http://${HOSTNAME}.${SERVICE_NAME}:2380 | true
141                 exec etcd --name ${HOSTNAME} \
142                     --listen-peer-urls http://0.0.0.0:2380 \
143                     --listen-client-urls http://0.0.0.0:2379\
144                     --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
145                     --data-dir /var/run/etcd/default.etcd
146             fi
147
148             # etcd-SET_ID
149             SET_ID=${HOSTNAME##*[^0-9]}
150
151             # adding a new member to existing cluster (assuming all initial pods are available)
152             if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then
153                 export ETCDCTL_ENDPOINT=$(eps)
154
155                 # member already added?
156                 MEMBER_HASH=$(member_hash)
157                 if [ -n "${MEMBER_HASH}" ]; then
158                     # the member hash exists but for some reason etcd failed
159                     # as the datadir has not be created, we can remove the member
160                     # and retrieve new hash
161                     etcdctl member remove ${MEMBER_HASH}
162                 fi
163
164                 echo "Adding new member"
165                 etcdctl member add ${HOSTNAME} http://${HOSTNAME}.${SERVICE_NAME}:2380 | grep "^ETCD_" > /var/run/etcd/new_member_envs
166
167                 if [ $? -ne 0 ]; then
168                     echo "Exiting"
169                     rm -f /var/run/etcd/new_member_envs
170                     exit 1
171                 fi
172
173                 cat /var/run/etcd/new_member_envs
174                 . /var/run/etcd/new_member_envs
175
176                 collect_member &
177
178                 exec etcd --name ${HOSTNAME} \
179                     --listen-peer-urls http://0.0.0.0:2380 \
180                     --listen-client-urls http://0.0.0.0:2379 \
181                     --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
182                     --data-dir /var/run/etcd/default.etcd \
183                     --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
184                     --initial-cluster ${ETCD_INITIAL_CLUSTER} \
185                     --initial-cluster-state ${ETCD_INITIAL_CLUSTER_STATE}
186             fi
187
188             PEERS=""
189             for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
190                 PEERS="${PEERS}${PEERS:+,}${SET_NAME}-${i}=http://${SET_NAME}-${i}.${SERVICE_NAME}:2380"
191             done
192
193             collect_member &
194
195             # join member
196             exec etcd --name ${HOSTNAME} \
197                 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
198                 --listen-peer-urls http://0.0.0.0:2380 \
199                 --listen-client-urls http://0.0.0.0:2379 \
200                 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
201                 --initial-cluster-token etcd-cluster-1 \
202                 --initial-cluster ${PEERS} \
203                 --initial-cluster-state new \
204                 --data-dir /var/run/etcd/default.etcd
205         volumeMounts:
206         - name: {{ include "common.fullname" . }}-data
207           mountPath: /var/run/etcd
208   {{- if .Values.persistence.enabled }}
209   volumeClaimTemplates:
210   - metadata:
211       name: {{ include "common.fullname" . }}-data
212       labels:
213         name: {{ include "common.fullname" . }}
214         chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
215         release: "{{ include "common.release" . }}"
216         heritage: "{{ .Release.Service }}"
217     spec:
218       accessModes:
219       - "{{ .Values.persistence.accessMode }}"
220       storageClassName: {{ include "common.storageClass" . }}
221       resources:
222         requests:
223           # upstream recommended max is 700M
224           storage: "{{ .Values.persistence.storage }}"
225   {{- else }}
226       volumes:
227       - name: {{ include "common.fullname" . }}-data
228       {{- if .Values.memoryMode }}
229         emptyDir:
230           medium: Memory
231       {{- else }}
232         emptyDir: {}
233       {{- end }}
234   {{- end }}