blob: fcab51cb59654f0d537e5634794bb0364f2a1401 [file] [log] [blame]
Ritu Sood23699102019-04-24 23:06:46 +00001# Copyright © 2019 Intel Corporation Inc
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
milaszkic03c3182020-07-07 12:13:44 +000014apiVersion: apps/v1
Ritu Sood23699102019-04-24 23:06:46 +000015kind: StatefulSet
16metadata:
Kiran Kamineni56c00a82019-05-20 17:19:40 -070017 name: {{ include "common.fullname" . }}
Ritu Sood23699102019-04-24 23:06:46 +000018 labels:
19 heritage: "{{ .Release.Service }}"
Krzysztof Opasiak137d7cc2020-01-24 23:49:11 +010020 release: "{{ include "common.release" . }}"
Ritu Sood23699102019-04-24 23:06:46 +000021 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
Kiran Kamineni56c00a82019-05-20 17:19:40 -070022 app: {{ include "common.name" . }}
Ritu Sood23699102019-04-24 23:06:46 +000023spec:
Kiran Kamineni56c00a82019-05-20 17:19:40 -070024 serviceName: {{ include "common.servicename" .}}
Ritu Sood23699102019-04-24 23:06:46 +000025 replicas: {{ .Values.replicaCount }}
milaszkic03c3182020-07-07 12:13:44 +000026 selector:
27 matchLabels:
28 app: {{ include "common.name" . }}
Ritu Sood23699102019-04-24 23:06:46 +000029 template:
30 metadata:
31 labels:
32 heritage: "{{ .Release.Service }}"
Krzysztof Opasiak137d7cc2020-01-24 23:49:11 +010033 release: "{{ include "common.release" . }}"
Ritu Sood23699102019-04-24 23:06:46 +000034 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
35 app: {{ include "common.name" . }}
36 spec:
37{{- if .Values.affinity }}
38 affinity:
39{{ toYaml .Values.affinity | indent 8 }}
40{{- end }}
41{{- if .Values.nodeSelector }}
42 nodeSelector:
43{{ toYaml .Values.nodeSelector | indent 8 }}
44{{- end }}
45{{- if .Values.tolerations }}
46 tolerations:
47{{ toYaml .Values.tolerations | indent 8 }}
48{{- end }}
49 containers:
Kiran Kamineni56c00a82019-05-20 17:19:40 -070050 - name: {{ include "common.fullname" . }}
Ritu Sood23699102019-04-24 23:06:46 +000051 image: "{{ .Values.repository }}/{{ .Values.image }}"
52 imagePullPolicy: "{{ .Values.pullPolicy }}"
53 ports:
54 - containerPort: {{ .Values.service.peerInternalPort }}
55 name: {{ .Values.service.peerPortName }}
56 - containerPort: {{ .Values.service.clientInternalPort }}
57 name: {{ .Values.service.clientPortName }}
58 {{- if eq .Values.liveness.enabled true }}
59 livenessProbe:
Kiran Kaminenif02245c2019-05-28 13:19:32 -070060 tcpSocket:
61 port: {{ .Values.service.clientInternalPort }}
62 initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
63 periodSeconds: {{ .Values.liveness.periodSeconds }}
64 timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
65 {{ end -}}
Ritu Sood23699102019-04-24 23:06:46 +000066 resources:
67{{ include "common.resources" . | indent 10 }}
68 env:
69 - name: INITIAL_CLUSTER_SIZE
70 value: {{ .Values.replicaCount | quote }}
71 - name: SET_NAME
Kiran Kamineni56c00a82019-05-20 17:19:40 -070072 value: {{ include "common.fullname" . }}
73 - name: SERVICE_NAME
Ritu Sood23699102019-04-24 23:06:46 +000074 value: {{ include "common.servicename" . }}
75{{- if .Values.extraEnv }}
76{{ toYaml .Values.extraEnv | indent 8 }}
77{{- end }}
78 lifecycle:
79 preStop:
80 exec:
81 command:
82 - "/bin/sh"
83 - "-ec"
84 - |
85 EPS=""
86 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -070087 EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
Ritu Sood23699102019-04-24 23:06:46 +000088 done
89
90 HOSTNAME=$(hostname)
91
92 member_hash() {
Kiran Kamineni56c00a82019-05-20 17:19:40 -070093 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
Ritu Sood23699102019-04-24 23:06:46 +000094 }
95
96 SET_ID=${HOSTNAME##*[^0-9]}
97
98 if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then
99 echo "Removing ${HOSTNAME} from etcd cluster"
100 ETCDCTL_ENDPOINT=${EPS} etcdctl member remove $(member_hash)
101 if [ $? -eq 0 ]; then
102 # Remove everything otherwise the cluster will no longer scale-up
103 rm -rf /var/run/etcd/*
104 fi
105 fi
106 command:
107 - "/bin/sh"
108 - "-ec"
109 - |
110 HOSTNAME=$(hostname)
111
112 # store member id into PVC for later member replacement
113 collect_member() {
114 while ! etcdctl member list &>/dev/null; do sleep 1; done
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700115 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 +0000116 exit 0
117 }
118
119 eps() {
120 EPS=""
121 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700122 EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379"
Ritu Sood23699102019-04-24 23:06:46 +0000123 done
124 echo ${EPS}
125 }
126
127 member_hash() {
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700128 etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1
Ritu Sood23699102019-04-24 23:06:46 +0000129 }
130
131 # we should wait for other pods to be up before trying to join
132 # otherwise we got "no such host" errors when trying to resolve other members
133 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
134 while true; do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700135 echo "Waiting for ${SET_NAME}-${i}.${SERVICE_NAME} to come up"
136 ping -W 1 -c 1 ${SET_NAME}-${i}.${SERVICE_NAME} > /dev/null && break
Ritu Sood23699102019-04-24 23:06:46 +0000137 sleep 1s
138 done
139 done
140
141 # re-joining after failure?
Kiran Kaminenif02245c2019-05-28 13:19:32 -0700142 if [[ -e /var/run/etcd/default.etcd && -f /var/run/etcd/member_id ]]; then
Ritu Sood23699102019-04-24 23:06:46 +0000143 echo "Re-joining etcd member"
144 member_id=$(cat /var/run/etcd/member_id)
145
146 # re-join member
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700147 ETCDCTL_ENDPOINT=$(eps) etcdctl member update ${member_id} http://${HOSTNAME}.${SERVICE_NAME}:2380 | true
Ritu Sood23699102019-04-24 23:06:46 +0000148 exec etcd --name ${HOSTNAME} \
149 --listen-peer-urls http://0.0.0.0:2380 \
150 --listen-client-urls http://0.0.0.0:2379\
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700151 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000152 --data-dir /var/run/etcd/default.etcd
153 fi
154
155 # etcd-SET_ID
156 SET_ID=${HOSTNAME##*[^0-9]}
157
158 # adding a new member to existing cluster (assuming all initial pods are available)
159 if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then
160 export ETCDCTL_ENDPOINT=$(eps)
161
162 # member already added?
163 MEMBER_HASH=$(member_hash)
164 if [ -n "${MEMBER_HASH}" ]; then
165 # the member hash exists but for some reason etcd failed
166 # as the datadir has not be created, we can remove the member
167 # and retrieve new hash
168 etcdctl member remove ${MEMBER_HASH}
169 fi
170
171 echo "Adding new member"
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700172 etcdctl member add ${HOSTNAME} http://${HOSTNAME}.${SERVICE_NAME}:2380 | grep "^ETCD_" > /var/run/etcd/new_member_envs
Ritu Sood23699102019-04-24 23:06:46 +0000173
174 if [ $? -ne 0 ]; then
175 echo "Exiting"
176 rm -f /var/run/etcd/new_member_envs
177 exit 1
178 fi
179
180 cat /var/run/etcd/new_member_envs
181 source /var/run/etcd/new_member_envs
182
183 collect_member &
184
185 exec etcd --name ${HOSTNAME} \
186 --listen-peer-urls http://0.0.0.0:2380 \
187 --listen-client-urls http://0.0.0.0:2379 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700188 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000189 --data-dir /var/run/etcd/default.etcd \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700190 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
Ritu Sood23699102019-04-24 23:06:46 +0000191 --initial-cluster ${ETCD_INITIAL_CLUSTER} \
192 --initial-cluster-state ${ETCD_INITIAL_CLUSTER_STATE}
193 fi
194
195 PEERS=""
196 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700197 PEERS="${PEERS}${PEERS:+,}${SET_NAME}-${i}=http://${SET_NAME}-${i}.${SERVICE_NAME}:2380"
Ritu Sood23699102019-04-24 23:06:46 +0000198 done
199
200 collect_member &
201
202 # join member
203 exec etcd --name ${HOSTNAME} \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700204 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
Ritu Sood23699102019-04-24 23:06:46 +0000205 --listen-peer-urls http://0.0.0.0:2380 \
206 --listen-client-urls http://0.0.0.0:2379 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700207 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000208 --initial-cluster-token etcd-cluster-1 \
209 --initial-cluster ${PEERS} \
210 --initial-cluster-state new \
211 --data-dir /var/run/etcd/default.etcd
212 volumeMounts:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700213 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000214 mountPath: /var/run/etcd
215 {{- if .Values.persistence.enabled }}
216 volumeClaimTemplates:
217 - metadata:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700218 name: {{ include "common.fullname" . }}-data
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100219 labels:
220 name: {{ include "common.fullname" . }}
221 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
Krzysztof Opasiak137d7cc2020-01-24 23:49:11 +0100222 release: "{{ include "common.release" . }}"
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100223 heritage: "{{ .Release.Service }}"
Ritu Sood23699102019-04-24 23:06:46 +0000224 spec:
225 accessModes:
Sylvain Desbureauxb7ed2ee2019-11-29 11:35:13 +0100226 - "{{ .Values.persistence.accessMode }}"
227 storageClassName: {{ include "common.storageClass" . }}
Ritu Sood23699102019-04-24 23:06:46 +0000228 resources:
229 requests:
230 # upstream recommended max is 700M
231 storage: "{{ .Values.persistence.storage }}"
Ritu Sood23699102019-04-24 23:06:46 +0000232 {{- else }}
233 volumes:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700234 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000235 {{- if .Values.memoryMode }}
236 emptyDir:
237 medium: Memory
238 {{- else }}
239 emptyDir: {}
240 {{- end }}
241 {{- end }}