blob: e39b8c4ca2af27ad600a2bca26f86a19b2e554bd [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 }}
51 containers:
Kiran Kamineni56c00a82019-05-20 17:19:40 -070052 - name: {{ include "common.fullname" . }}
Sylvain Desbureauxc9f55332020-11-19 17:10:36 +010053 image: {{ include "repositoryGenerator.googleK8sRepository" . }}/{{ .Values.image }}
Ritu Sood23699102019-04-24 23:06:46 +000054 imagePullPolicy: "{{ .Values.pullPolicy }}"
55 ports:
56 - containerPort: {{ .Values.service.peerInternalPort }}
57 name: {{ .Values.service.peerPortName }}
58 - containerPort: {{ .Values.service.clientInternalPort }}
59 name: {{ .Values.service.clientPortName }}
60 {{- if eq .Values.liveness.enabled true }}
61 livenessProbe:
Kiran Kaminenif02245c2019-05-28 13:19:32 -070062 tcpSocket:
63 port: {{ .Values.service.clientInternalPort }}
64 initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
65 periodSeconds: {{ .Values.liveness.periodSeconds }}
66 timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
67 {{ end -}}
Ritu Sood23699102019-04-24 23:06:46 +000068 resources:
69{{ include "common.resources" . | indent 10 }}
70 env:
71 - name: INITIAL_CLUSTER_SIZE
72 value: {{ .Values.replicaCount | quote }}
73 - name: SET_NAME
Kiran Kamineni56c00a82019-05-20 17:19:40 -070074 value: {{ include "common.fullname" . }}
75 - name: SERVICE_NAME
Ritu Sood23699102019-04-24 23:06:46 +000076 value: {{ include "common.servicename" . }}
77{{- if .Values.extraEnv }}
78{{ toYaml .Values.extraEnv | indent 8 }}
79{{- end }}
80 lifecycle:
81 preStop:
82 exec:
83 command:
84 - "/bin/sh"
85 - "-ec"
86 - |
87 EPS=""
88 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -070089 EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
Ritu Sood23699102019-04-24 23:06:46 +000090 done
91
92 HOSTNAME=$(hostname)
93
94 member_hash() {
Kiran Kamineni56c00a82019-05-20 17:19:40 -070095 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
Ritu Sood23699102019-04-24 23:06:46 +000096 }
97
98 SET_ID=${HOSTNAME##*[^0-9]}
99
100 if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then
101 echo "Removing ${HOSTNAME} from etcd cluster"
102 ETCDCTL_ENDPOINT=${EPS} etcdctl member remove $(member_hash)
103 if [ $? -eq 0 ]; then
104 # Remove everything otherwise the cluster will no longer scale-up
105 rm -rf /var/run/etcd/*
106 fi
107 fi
108 command:
109 - "/bin/sh"
110 - "-ec"
111 - |
112 HOSTNAME=$(hostname)
113
114 # store member id into PVC for later member replacement
115 collect_member() {
116 while ! etcdctl member list &>/dev/null; do sleep 1; done
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700117 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 +0000118 exit 0
119 }
120
121 eps() {
122 EPS=""
123 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700124 EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
Ritu Sood23699102019-04-24 23:06:46 +0000125 done
126 echo ${EPS}
127 }
128
129 member_hash() {
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700130 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
Ritu Sood23699102019-04-24 23:06:46 +0000131 }
132
133 # we should wait for other pods to be up before trying to join
134 # otherwise we got "no such host" errors when trying to resolve other members
135 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Konrad Bańkae1d62ce2021-01-25 07:44:49 +0100136 if [ "${SET_NAME}-${i}" == "${HOSTNAME}" ]; then
137 echo "Skipping self-checking"
138 continue
139 fi
Ritu Sood23699102019-04-24 23:06:46 +0000140 while true; do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700141 echo "Waiting for ${SET_NAME}-${i}.${SERVICE_NAME} to come up"
142 ping -W 1 -c 1 ${SET_NAME}-${i}.${SERVICE_NAME} > /dev/null && break
Ritu Sood23699102019-04-24 23:06:46 +0000143 sleep 1s
144 done
145 done
146
147 # re-joining after failure?
Kiran Kaminenif02245c2019-05-28 13:19:32 -0700148 if [[ -e /var/run/etcd/default.etcd && -f /var/run/etcd/member_id ]]; then
Ritu Sood23699102019-04-24 23:06:46 +0000149 echo "Re-joining etcd member"
150 member_id=$(cat /var/run/etcd/member_id)
151
152 # re-join member
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700153 ETCDCTL_ENDPOINT=$(eps) etcdctl member update ${member_id} http://${HOSTNAME}.${SERVICE_NAME}:2380 | true
Ritu Sood23699102019-04-24 23:06:46 +0000154 exec etcd --name ${HOSTNAME} \
155 --listen-peer-urls http://0.0.0.0:2380 \
156 --listen-client-urls http://0.0.0.0:2379\
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700157 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000158 --data-dir /var/run/etcd/default.etcd
159 fi
160
161 # etcd-SET_ID
162 SET_ID=${HOSTNAME##*[^0-9]}
163
164 # adding a new member to existing cluster (assuming all initial pods are available)
165 if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then
166 export ETCDCTL_ENDPOINT=$(eps)
167
168 # member already added?
169 MEMBER_HASH=$(member_hash)
170 if [ -n "${MEMBER_HASH}" ]; then
171 # the member hash exists but for some reason etcd failed
172 # as the datadir has not be created, we can remove the member
173 # and retrieve new hash
174 etcdctl member remove ${MEMBER_HASH}
175 fi
176
177 echo "Adding new member"
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700178 etcdctl member add ${HOSTNAME} http://${HOSTNAME}.${SERVICE_NAME}:2380 | grep "^ETCD_" > /var/run/etcd/new_member_envs
Ritu Sood23699102019-04-24 23:06:46 +0000179
180 if [ $? -ne 0 ]; then
181 echo "Exiting"
182 rm -f /var/run/etcd/new_member_envs
183 exit 1
184 fi
185
186 cat /var/run/etcd/new_member_envs
187 source /var/run/etcd/new_member_envs
188
189 collect_member &
190
191 exec etcd --name ${HOSTNAME} \
192 --listen-peer-urls http://0.0.0.0:2380 \
193 --listen-client-urls http://0.0.0.0:2379 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700194 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000195 --data-dir /var/run/etcd/default.etcd \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700196 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
Ritu Sood23699102019-04-24 23:06:46 +0000197 --initial-cluster ${ETCD_INITIAL_CLUSTER} \
198 --initial-cluster-state ${ETCD_INITIAL_CLUSTER_STATE}
199 fi
200
201 PEERS=""
202 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700203 PEERS="${PEERS}${PEERS:+,}${SET_NAME}-${i}=http://${SET_NAME}-${i}.${SERVICE_NAME}:2380"
Ritu Sood23699102019-04-24 23:06:46 +0000204 done
205
206 collect_member &
207
208 # join member
209 exec etcd --name ${HOSTNAME} \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700210 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
Ritu Sood23699102019-04-24 23:06:46 +0000211 --listen-peer-urls http://0.0.0.0:2380 \
212 --listen-client-urls http://0.0.0.0:2379 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700213 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000214 --initial-cluster-token etcd-cluster-1 \
215 --initial-cluster ${PEERS} \
216 --initial-cluster-state new \
217 --data-dir /var/run/etcd/default.etcd
218 volumeMounts:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700219 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000220 mountPath: /var/run/etcd
221 {{- if .Values.persistence.enabled }}
222 volumeClaimTemplates:
223 - metadata:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700224 name: {{ include "common.fullname" . }}-data
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100225 labels:
226 name: {{ include "common.fullname" . }}
227 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
Krzysztof Opasiak137d7cc2020-01-24 23:49:11 +0100228 release: "{{ include "common.release" . }}"
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100229 heritage: "{{ .Release.Service }}"
Ritu Sood23699102019-04-24 23:06:46 +0000230 spec:
231 accessModes:
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100232 - "{{ .Values.persistence.accessMode }}"
233 storageClassName: {{ include "common.storageClass" . }}
Ritu Sood23699102019-04-24 23:06:46 +0000234 resources:
235 requests:
236 # upstream recommended max is 700M
237 storage: "{{ .Values.persistence.storage }}"
Ritu Sood23699102019-04-24 23:06:46 +0000238 {{- else }}
239 volumes:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700240 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000241 {{- if .Values.memoryMode }}
242 emptyDir:
243 medium: Memory
244 {{- else }}
245 emptyDir: {}
246 {{- end }}
247 {{- end }}