blob: 829e17b5cbfc78e44c49a00523af3c4f808a4dcd [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
Andreas Geissler8c55fdd2023-07-20 11:15:42 +020018metadata: {{- include "common.resourceMetadata" . | nindent 2 }}
Ritu Sood23699102019-04-24 23:06:46 +000019spec:
Andreas Geissler8c55fdd2023-07-20 11:15:42 +020020 selector: {{- include "common.selectors" . | nindent 4 }}
21 serviceName: {{ include "common.servicename" . }}
Ritu Sood23699102019-04-24 23:06:46 +000022 replicas: {{ .Values.replicaCount }}
23 template:
Andreas Geissler8c55fdd2023-07-20 11:15:42 +020024 metadata: {{- include "common.templateMetadata" . | nindent 6 }}
Ritu Sood23699102019-04-24 23:06:46 +000025 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 }}
andreas-geisslerf84cccd2021-07-07 15:40:41 +020038 imagePullSecrets:
39 - name: "{{ include "common.namespace" . }}-docker-registry-key"
Ritu Sood23699102019-04-24 23:06:46 +000040 containers:
krishnaa96544863d2021-05-11 18:29:49 +053041 - name: {{ include "common.name" . }}
Sylvain Desbureauxc9f55332020-11-19 17:10:36 +010042 image: {{ include "repositoryGenerator.googleK8sRepository" . }}/{{ .Values.image }}
Ritu Sood23699102019-04-24 23:06:46 +000043 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:
Kiran Kaminenif02245c2019-05-28 13:19:32 -070051 tcpSocket:
52 port: {{ .Values.service.clientInternalPort }}
53 initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
54 periodSeconds: {{ .Values.liveness.periodSeconds }}
55 timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
56 {{ end -}}
miroslavmasaryka7ac7f02023-03-01 14:12:26 +010057 resources: {{ include "common.resources" . | nindent 10 }}
Ritu Sood23699102019-04-24 23:06:46 +000058 env:
59 - name: INITIAL_CLUSTER_SIZE
60 value: {{ .Values.replicaCount | quote }}
61 - name: SET_NAME
Kiran Kamineni56c00a82019-05-20 17:19:40 -070062 value: {{ include "common.fullname" . }}
63 - name: SERVICE_NAME
Sylvain Desbureauxc991f0b2021-10-29 15:51:58 +020064 value: {{ include "common.servicename" . }}.{{ include "common.namespace" . }}.svc.{{ .Values.global.clusterName }}
Ritu Sood23699102019-04-24 23:06:46 +000065{{- 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
Kiran Kamineni56c00a82019-05-20 17:19:40 -070077 EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
Ritu Sood23699102019-04-24 23:06:46 +000078 done
79
80 HOSTNAME=$(hostname)
81
82 member_hash() {
Kiran Kamineni56c00a82019-05-20 17:19:40 -070083 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
Ritu Sood23699102019-04-24 23:06:46 +000084 }
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
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700105 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 +0000106 exit 0
107 }
108
109 eps() {
110 EPS=""
111 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700112 EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
Ritu Sood23699102019-04-24 23:06:46 +0000113 done
114 echo ${EPS}
115 }
116
117 member_hash() {
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700118 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
Ritu Sood23699102019-04-24 23:06:46 +0000119 }
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
Konrad Bańkae1d62ce2021-01-25 07:44:49 +0100124 if [ "${SET_NAME}-${i}" == "${HOSTNAME}" ]; then
125 echo "Skipping self-checking"
126 continue
127 fi
Ritu Sood23699102019-04-24 23:06:46 +0000128 while true; do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700129 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
Ritu Sood23699102019-04-24 23:06:46 +0000131 sleep 1s
132 done
133 done
134
135 # re-joining after failure?
Kiran Kaminenif02245c2019-05-28 13:19:32 -0700136 if [[ -e /var/run/etcd/default.etcd && -f /var/run/etcd/member_id ]]; then
Ritu Sood23699102019-04-24 23:06:46 +0000137 echo "Re-joining etcd member"
138 member_id=$(cat /var/run/etcd/member_id)
139
140 # re-join member
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700141 ETCDCTL_ENDPOINT=$(eps) etcdctl member update ${member_id} http://${HOSTNAME}.${SERVICE_NAME}:2380 | true
Ritu Sood23699102019-04-24 23:06:46 +0000142 exec etcd --name ${HOSTNAME} \
143 --listen-peer-urls http://0.0.0.0:2380 \
144 --listen-client-urls http://0.0.0.0:2379\
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700145 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000146 --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"
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700166 etcdctl member add ${HOSTNAME} http://${HOSTNAME}.${SERVICE_NAME}:2380 | grep "^ETCD_" > /var/run/etcd/new_member_envs
Ritu Sood23699102019-04-24 23:06:46 +0000167
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
Guillaume Lambert2b6f82c2021-03-02 21:45:00 +0100175 . /var/run/etcd/new_member_envs
Ritu Sood23699102019-04-24 23:06:46 +0000176
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 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700182 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000183 --data-dir /var/run/etcd/default.etcd \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700184 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
Ritu Sood23699102019-04-24 23:06:46 +0000185 --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
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700191 PEERS="${PEERS}${PEERS:+,}${SET_NAME}-${i}=http://${SET_NAME}-${i}.${SERVICE_NAME}:2380"
Ritu Sood23699102019-04-24 23:06:46 +0000192 done
193
194 collect_member &
195
196 # join member
197 exec etcd --name ${HOSTNAME} \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700198 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
Ritu Sood23699102019-04-24 23:06:46 +0000199 --listen-peer-urls http://0.0.0.0:2380 \
200 --listen-client-urls http://0.0.0.0:2379 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700201 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000202 --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:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700207 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000208 mountPath: /var/run/etcd
209 {{- if .Values.persistence.enabled }}
210 volumeClaimTemplates:
211 - metadata:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700212 name: {{ include "common.fullname" . }}-data
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100213 labels:
214 name: {{ include "common.fullname" . }}
215 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
Krzysztof Opasiak137d7cc2020-01-24 23:49:11 +0100216 release: "{{ include "common.release" . }}"
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100217 heritage: "{{ .Release.Service }}"
Ritu Sood23699102019-04-24 23:06:46 +0000218 spec:
219 accessModes:
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100220 - "{{ .Values.persistence.accessMode }}"
221 storageClassName: {{ include "common.storageClass" . }}
Ritu Sood23699102019-04-24 23:06:46 +0000222 resources:
223 requests:
224 # upstream recommended max is 700M
225 storage: "{{ .Values.persistence.storage }}"
Ritu Sood23699102019-04-24 23:06:46 +0000226 {{- else }}
227 volumes:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700228 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000229 {{- if .Values.memoryMode }}
230 emptyDir:
231 medium: Memory
232 {{- else }}
233 emptyDir: {}
234 {{- end }}
235 {{- end }}