blob: e228e6de43a9bea9475435aedbe71a202f60d57e [file] [log] [blame]
Jakub Latusek3659d9f2020-09-14 16:21:55 +02001{{/*
Ritu Sood23699102019-04-24 23:06:46 +00002# 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.
Jakub Latusek3659d9f2020-09-14 16:21:55 +020015*/}}
milaszkic03c3182020-07-07 12:13:44 +000016apiVersion: apps/v1
Ritu Sood23699102019-04-24 23:06:46 +000017kind: StatefulSet
18metadata:
Kiran Kamineni56c00a82019-05-20 17:19:40 -070019 name: {{ include "common.fullname" . }}
Ritu Sood23699102019-04-24 23:06:46 +000020 labels:
21 heritage: "{{ .Release.Service }}"
Krzysztof Opasiak137d7cc2020-01-24 23:49:11 +010022 release: "{{ include "common.release" . }}"
Ritu Sood23699102019-04-24 23:06:46 +000023 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
Kiran Kamineni56c00a82019-05-20 17:19:40 -070024 app: {{ include "common.name" . }}
Ritu Sood23699102019-04-24 23:06:46 +000025spec:
Kiran Kamineni56c00a82019-05-20 17:19:40 -070026 serviceName: {{ include "common.servicename" .}}
Ritu Sood23699102019-04-24 23:06:46 +000027 replicas: {{ .Values.replicaCount }}
milaszkic03c3182020-07-07 12:13:44 +000028 selector:
29 matchLabels:
30 app: {{ include "common.name" . }}
Ritu Sood23699102019-04-24 23:06:46 +000031 template:
32 metadata:
33 labels:
34 heritage: "{{ .Release.Service }}"
Krzysztof Opasiak137d7cc2020-01-24 23:49:11 +010035 release: "{{ include "common.release" . }}"
Ritu Sood23699102019-04-24 23:06:46 +000036 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
37 app: {{ include "common.name" . }}
38 spec:
39{{- if .Values.affinity }}
40 affinity:
41{{ toYaml .Values.affinity | indent 8 }}
42{{- end }}
43{{- if .Values.nodeSelector }}
44 nodeSelector:
45{{ toYaml .Values.nodeSelector | indent 8 }}
46{{- end }}
47{{- if .Values.tolerations }}
48 tolerations:
49{{ toYaml .Values.tolerations | indent 8 }}
50{{- end }}
andreas-geisslerf84cccd2021-07-07 15:40:41 +020051 imagePullSecrets:
52 - name: "{{ include "common.namespace" . }}-docker-registry-key"
Ritu Sood23699102019-04-24 23:06:46 +000053 containers:
krishnaa96544863d2021-05-11 18:29:49 +053054 - name: {{ include "common.name" . }}
Sylvain Desbureauxc9f55332020-11-19 17:10:36 +010055 image: {{ include "repositoryGenerator.googleK8sRepository" . }}/{{ .Values.image }}
Ritu Sood23699102019-04-24 23:06:46 +000056 imagePullPolicy: "{{ .Values.pullPolicy }}"
57 ports:
58 - containerPort: {{ .Values.service.peerInternalPort }}
59 name: {{ .Values.service.peerPortName }}
60 - containerPort: {{ .Values.service.clientInternalPort }}
61 name: {{ .Values.service.clientPortName }}
62 {{- if eq .Values.liveness.enabled true }}
63 livenessProbe:
Kiran Kaminenif02245c2019-05-28 13:19:32 -070064 tcpSocket:
65 port: {{ .Values.service.clientInternalPort }}
66 initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
67 periodSeconds: {{ .Values.liveness.periodSeconds }}
68 timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
69 {{ end -}}
miroslavmasaryka7ac7f02023-03-01 14:12:26 +010070 resources: {{ include "common.resources" . | nindent 10 }}
Ritu Sood23699102019-04-24 23:06:46 +000071 env:
72 - name: INITIAL_CLUSTER_SIZE
73 value: {{ .Values.replicaCount | quote }}
74 - name: SET_NAME
Kiran Kamineni56c00a82019-05-20 17:19:40 -070075 value: {{ include "common.fullname" . }}
76 - name: SERVICE_NAME
Sylvain Desbureauxc991f0b2021-10-29 15:51:58 +020077 value: {{ include "common.servicename" . }}.{{ include "common.namespace" . }}.svc.{{ .Values.global.clusterName }}
Ritu Sood23699102019-04-24 23:06:46 +000078{{- if .Values.extraEnv }}
79{{ toYaml .Values.extraEnv | indent 8 }}
80{{- end }}
81 lifecycle:
82 preStop:
83 exec:
84 command:
85 - "/bin/sh"
86 - "-ec"
87 - |
88 EPS=""
89 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -070090 EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
Ritu Sood23699102019-04-24 23:06:46 +000091 done
92
93 HOSTNAME=$(hostname)
94
95 member_hash() {
Kiran Kamineni56c00a82019-05-20 17:19:40 -070096 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
Ritu Sood23699102019-04-24 23:06:46 +000097 }
98
99 SET_ID=${HOSTNAME##*[^0-9]}
100
101 if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then
102 echo "Removing ${HOSTNAME} from etcd cluster"
103 ETCDCTL_ENDPOINT=${EPS} etcdctl member remove $(member_hash)
104 if [ $? -eq 0 ]; then
105 # Remove everything otherwise the cluster will no longer scale-up
106 rm -rf /var/run/etcd/*
107 fi
108 fi
109 command:
110 - "/bin/sh"
111 - "-ec"
112 - |
113 HOSTNAME=$(hostname)
114
115 # store member id into PVC for later member replacement
116 collect_member() {
117 while ! etcdctl member list &>/dev/null; do sleep 1; done
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700118 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1 > /var/run/etcd/member_id
Ritu Sood23699102019-04-24 23:06:46 +0000119 exit 0
120 }
121
122 eps() {
123 EPS=""
124 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700125 EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
Ritu Sood23699102019-04-24 23:06:46 +0000126 done
127 echo ${EPS}
128 }
129
130 member_hash() {
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700131 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
Ritu Sood23699102019-04-24 23:06:46 +0000132 }
133
134 # we should wait for other pods to be up before trying to join
135 # otherwise we got "no such host" errors when trying to resolve other members
136 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Konrad Bańkae1d62ce2021-01-25 07:44:49 +0100137 if [ "${SET_NAME}-${i}" == "${HOSTNAME}" ]; then
138 echo "Skipping self-checking"
139 continue
140 fi
Ritu Sood23699102019-04-24 23:06:46 +0000141 while true; do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700142 echo "Waiting for ${SET_NAME}-${i}.${SERVICE_NAME} to come up"
143 ping -W 1 -c 1 ${SET_NAME}-${i}.${SERVICE_NAME} > /dev/null && break
Ritu Sood23699102019-04-24 23:06:46 +0000144 sleep 1s
145 done
146 done
147
148 # re-joining after failure?
Kiran Kaminenif02245c2019-05-28 13:19:32 -0700149 if [[ -e /var/run/etcd/default.etcd && -f /var/run/etcd/member_id ]]; then
Ritu Sood23699102019-04-24 23:06:46 +0000150 echo "Re-joining etcd member"
151 member_id=$(cat /var/run/etcd/member_id)
152
153 # re-join member
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700154 ETCDCTL_ENDPOINT=$(eps) etcdctl member update ${member_id} http://${HOSTNAME}.${SERVICE_NAME}:2380 | true
Ritu Sood23699102019-04-24 23:06:46 +0000155 exec etcd --name ${HOSTNAME} \
156 --listen-peer-urls http://0.0.0.0:2380 \
157 --listen-client-urls http://0.0.0.0:2379\
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700158 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000159 --data-dir /var/run/etcd/default.etcd
160 fi
161
162 # etcd-SET_ID
163 SET_ID=${HOSTNAME##*[^0-9]}
164
165 # adding a new member to existing cluster (assuming all initial pods are available)
166 if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then
167 export ETCDCTL_ENDPOINT=$(eps)
168
169 # member already added?
170 MEMBER_HASH=$(member_hash)
171 if [ -n "${MEMBER_HASH}" ]; then
172 # the member hash exists but for some reason etcd failed
173 # as the datadir has not be created, we can remove the member
174 # and retrieve new hash
175 etcdctl member remove ${MEMBER_HASH}
176 fi
177
178 echo "Adding new member"
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700179 etcdctl member add ${HOSTNAME} http://${HOSTNAME}.${SERVICE_NAME}:2380 | grep "^ETCD_" > /var/run/etcd/new_member_envs
Ritu Sood23699102019-04-24 23:06:46 +0000180
181 if [ $? -ne 0 ]; then
182 echo "Exiting"
183 rm -f /var/run/etcd/new_member_envs
184 exit 1
185 fi
186
187 cat /var/run/etcd/new_member_envs
Guillaume Lambert2b6f82c2021-03-02 21:45:00 +0100188 . /var/run/etcd/new_member_envs
Ritu Sood23699102019-04-24 23:06:46 +0000189
190 collect_member &
191
192 exec etcd --name ${HOSTNAME} \
193 --listen-peer-urls http://0.0.0.0:2380 \
194 --listen-client-urls http://0.0.0.0:2379 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700195 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000196 --data-dir /var/run/etcd/default.etcd \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700197 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
Ritu Sood23699102019-04-24 23:06:46 +0000198 --initial-cluster ${ETCD_INITIAL_CLUSTER} \
199 --initial-cluster-state ${ETCD_INITIAL_CLUSTER_STATE}
200 fi
201
202 PEERS=""
203 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700204 PEERS="${PEERS}${PEERS:+,}${SET_NAME}-${i}=http://${SET_NAME}-${i}.${SERVICE_NAME}:2380"
Ritu Sood23699102019-04-24 23:06:46 +0000205 done
206
207 collect_member &
208
209 # join member
210 exec etcd --name ${HOSTNAME} \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700211 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
Ritu Sood23699102019-04-24 23:06:46 +0000212 --listen-peer-urls http://0.0.0.0:2380 \
213 --listen-client-urls http://0.0.0.0:2379 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700214 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000215 --initial-cluster-token etcd-cluster-1 \
216 --initial-cluster ${PEERS} \
217 --initial-cluster-state new \
218 --data-dir /var/run/etcd/default.etcd
219 volumeMounts:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700220 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000221 mountPath: /var/run/etcd
222 {{- if .Values.persistence.enabled }}
223 volumeClaimTemplates:
224 - metadata:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700225 name: {{ include "common.fullname" . }}-data
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100226 labels:
227 name: {{ include "common.fullname" . }}
228 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
Krzysztof Opasiak137d7cc2020-01-24 23:49:11 +0100229 release: "{{ include "common.release" . }}"
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100230 heritage: "{{ .Release.Service }}"
Ritu Sood23699102019-04-24 23:06:46 +0000231 spec:
232 accessModes:
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100233 - "{{ .Values.persistence.accessMode }}"
234 storageClassName: {{ include "common.storageClass" . }}
Ritu Sood23699102019-04-24 23:06:46 +0000235 resources:
236 requests:
237 # upstream recommended max is 700M
238 storage: "{{ .Values.persistence.storage }}"
Ritu Sood23699102019-04-24 23:06:46 +0000239 {{- else }}
240 volumes:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700241 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000242 {{- if .Values.memoryMode }}
243 emptyDir:
244 medium: Memory
245 {{- else }}
246 emptyDir: {}
247 {{- end }}
248 {{- end }}