blob: 722a27d791b589a361b2280fb15d2fff45cba027 [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 Geisslerbd0d31a2024-03-20 09:51:32 +010038 {{- include "common.imagePullSecrets" . | nindent 6 }}
Ritu Sood23699102019-04-24 23:06:46 +000039 containers:
krishnaa96544863d2021-05-11 18:29:49 +053040 - name: {{ include "common.name" . }}
Sylvain Desbureauxc9f55332020-11-19 17:10:36 +010041 image: {{ include "repositoryGenerator.googleK8sRepository" . }}/{{ .Values.image }}
Ritu Sood23699102019-04-24 23:06:46 +000042 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:
Kiran Kaminenif02245c2019-05-28 13:19:32 -070050 tcpSocket:
51 port: {{ .Values.service.clientInternalPort }}
52 initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
53 periodSeconds: {{ .Values.liveness.periodSeconds }}
54 timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
55 {{ end -}}
miroslavmasaryka7ac7f02023-03-01 14:12:26 +010056 resources: {{ include "common.resources" . | nindent 10 }}
Ritu Sood23699102019-04-24 23:06:46 +000057 env:
58 - name: INITIAL_CLUSTER_SIZE
59 value: {{ .Values.replicaCount | quote }}
60 - name: SET_NAME
Kiran Kamineni56c00a82019-05-20 17:19:40 -070061 value: {{ include "common.fullname" . }}
62 - name: SERVICE_NAME
Sylvain Desbureauxc991f0b2021-10-29 15:51:58 +020063 value: {{ include "common.servicename" . }}.{{ include "common.namespace" . }}.svc.{{ .Values.global.clusterName }}
Ritu Sood23699102019-04-24 23:06:46 +000064{{- 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
Kiran Kamineni56c00a82019-05-20 17:19:40 -070076 EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
Ritu Sood23699102019-04-24 23:06:46 +000077 done
78
79 HOSTNAME=$(hostname)
80
81 member_hash() {
Kiran Kamineni56c00a82019-05-20 17:19:40 -070082 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
Ritu Sood23699102019-04-24 23:06:46 +000083 }
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
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700104 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 +0000105 exit 0
106 }
107
108 eps() {
109 EPS=""
110 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700111 EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
Ritu Sood23699102019-04-24 23:06:46 +0000112 done
113 echo ${EPS}
114 }
115
116 member_hash() {
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700117 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
Ritu Sood23699102019-04-24 23:06:46 +0000118 }
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
Konrad Bańkae1d62ce2021-01-25 07:44:49 +0100123 if [ "${SET_NAME}-${i}" == "${HOSTNAME}" ]; then
124 echo "Skipping self-checking"
125 continue
126 fi
Ritu Sood23699102019-04-24 23:06:46 +0000127 while true; do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700128 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
Ritu Sood23699102019-04-24 23:06:46 +0000130 sleep 1s
131 done
132 done
133
134 # re-joining after failure?
Kiran Kaminenif02245c2019-05-28 13:19:32 -0700135 if [[ -e /var/run/etcd/default.etcd && -f /var/run/etcd/member_id ]]; then
Ritu Sood23699102019-04-24 23:06:46 +0000136 echo "Re-joining etcd member"
137 member_id=$(cat /var/run/etcd/member_id)
138
139 # re-join member
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700140 ETCDCTL_ENDPOINT=$(eps) etcdctl member update ${member_id} http://${HOSTNAME}.${SERVICE_NAME}:2380 | true
Ritu Sood23699102019-04-24 23:06:46 +0000141 exec etcd --name ${HOSTNAME} \
142 --listen-peer-urls http://0.0.0.0:2380 \
143 --listen-client-urls http://0.0.0.0:2379\
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700144 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000145 --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"
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700165 etcdctl member add ${HOSTNAME} http://${HOSTNAME}.${SERVICE_NAME}:2380 | grep "^ETCD_" > /var/run/etcd/new_member_envs
Ritu Sood23699102019-04-24 23:06:46 +0000166
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
Guillaume Lambert2b6f82c2021-03-02 21:45:00 +0100174 . /var/run/etcd/new_member_envs
Ritu Sood23699102019-04-24 23:06:46 +0000175
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 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700181 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000182 --data-dir /var/run/etcd/default.etcd \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700183 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
Ritu Sood23699102019-04-24 23:06:46 +0000184 --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
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700190 PEERS="${PEERS}${PEERS:+,}${SET_NAME}-${i}=http://${SET_NAME}-${i}.${SERVICE_NAME}:2380"
Ritu Sood23699102019-04-24 23:06:46 +0000191 done
192
193 collect_member &
194
195 # join member
196 exec etcd --name ${HOSTNAME} \
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 --listen-peer-urls http://0.0.0.0:2380 \
199 --listen-client-urls http://0.0.0.0:2379 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700200 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000201 --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:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700206 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000207 mountPath: /var/run/etcd
208 {{- if .Values.persistence.enabled }}
209 volumeClaimTemplates:
210 - metadata:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700211 name: {{ include "common.fullname" . }}-data
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100212 labels:
213 name: {{ include "common.fullname" . }}
214 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
Krzysztof Opasiak137d7cc2020-01-24 23:49:11 +0100215 release: "{{ include "common.release" . }}"
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100216 heritage: "{{ .Release.Service }}"
Ritu Sood23699102019-04-24 23:06:46 +0000217 spec:
218 accessModes:
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100219 - "{{ .Values.persistence.accessMode }}"
220 storageClassName: {{ include "common.storageClass" . }}
Ritu Sood23699102019-04-24 23:06:46 +0000221 resources:
222 requests:
223 # upstream recommended max is 700M
224 storage: "{{ .Values.persistence.storage }}"
Ritu Sood23699102019-04-24 23:06:46 +0000225 {{- else }}
226 volumes:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700227 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000228 {{- if .Values.memoryMode }}
229 emptyDir:
230 medium: Memory
231 {{- else }}
232 emptyDir: {}
233 {{- end }}
234 {{- end }}