blob: 8b6a53454e4b544551b4b8d27ee97b25628b4901 [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.
14
15apiVersion: apps/v1beta1
16kind: StatefulSet
17metadata:
Kiran Kamineni56c00a82019-05-20 17:19:40 -070018 name: {{ include "common.fullname" . }}
Ritu Sood23699102019-04-24 23:06:46 +000019 labels:
20 heritage: "{{ .Release.Service }}"
21 release: "{{ .Release.Name }}"
22 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
Kiran Kamineni56c00a82019-05-20 17:19:40 -070023 app: {{ include "common.name" . }}
Ritu Sood23699102019-04-24 23:06:46 +000024spec:
Kiran Kamineni56c00a82019-05-20 17:19:40 -070025 serviceName: {{ include "common.servicename" .}}
Ritu Sood23699102019-04-24 23:06:46 +000026 replicas: {{ .Values.replicaCount }}
27 template:
28 metadata:
29 labels:
30 heritage: "{{ .Release.Service }}"
31 release: "{{ .Release.Name }}"
32 chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
33 app: {{ include "common.name" . }}
34 spec:
35{{- if .Values.affinity }}
36 affinity:
37{{ toYaml .Values.affinity | indent 8 }}
38{{- end }}
39{{- if .Values.nodeSelector }}
40 nodeSelector:
41{{ toYaml .Values.nodeSelector | indent 8 }}
42{{- end }}
43{{- if .Values.tolerations }}
44 tolerations:
45{{ toYaml .Values.tolerations | indent 8 }}
46{{- end }}
47 containers:
Kiran Kamineni56c00a82019-05-20 17:19:40 -070048 - name: {{ include "common.fullname" . }}
Ritu Sood23699102019-04-24 23:06:46 +000049 image: "{{ .Values.repository }}/{{ .Values.image }}"
50 imagePullPolicy: "{{ .Values.pullPolicy }}"
51 ports:
52 - containerPort: {{ .Values.service.peerInternalPort }}
53 name: {{ .Values.service.peerPortName }}
54 - containerPort: {{ .Values.service.clientInternalPort }}
55 name: {{ .Values.service.clientPortName }}
56 {{- if eq .Values.liveness.enabled true }}
57 livenessProbe:
58 exec:
59 command: ["/bin/sh", "-c", "etcdctl cluster-health | grep -w healthy" ]
60 initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
61 periodSeconds: {{ .Values.liveness.periodSeconds }}
62 timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
63 {{ end -}}
64 readinessProbe:
65 exec:
66 command: ["/bin/sh", "-c", "etcdctl cluster-health | grep -w healthy" ]
67 initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds }}
68 periodSeconds: {{ .Values.readiness.periodSeconds }}
69 resources:
70{{ include "common.resources" . | indent 10 }}
71 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
Ritu Sood23699102019-04-24 23:06:46 +000077 value: {{ include "common.servicename" . }}
78{{- 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
137 while true; do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700138 echo "Waiting for ${SET_NAME}-${i}.${SERVICE_NAME} to come up"
139 ping -W 1 -c 1 ${SET_NAME}-${i}.${SERVICE_NAME} > /dev/null && break
Ritu Sood23699102019-04-24 23:06:46 +0000140 sleep 1s
141 done
142 done
143
144 # re-joining after failure?
145 if [ -e /var/run/etcd/default.etcd ]; then
146 echo "Re-joining etcd member"
147 member_id=$(cat /var/run/etcd/member_id)
148
149 # re-join member
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700150 ETCDCTL_ENDPOINT=$(eps) etcdctl member update ${member_id} http://${HOSTNAME}.${SERVICE_NAME}:2380 | true
Ritu Sood23699102019-04-24 23:06:46 +0000151 exec etcd --name ${HOSTNAME} \
152 --listen-peer-urls http://0.0.0.0:2380 \
153 --listen-client-urls http://0.0.0.0:2379\
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700154 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000155 --data-dir /var/run/etcd/default.etcd
156 fi
157
158 # etcd-SET_ID
159 SET_ID=${HOSTNAME##*[^0-9]}
160
161 # adding a new member to existing cluster (assuming all initial pods are available)
162 if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then
163 export ETCDCTL_ENDPOINT=$(eps)
164
165 # member already added?
166 MEMBER_HASH=$(member_hash)
167 if [ -n "${MEMBER_HASH}" ]; then
168 # the member hash exists but for some reason etcd failed
169 # as the datadir has not be created, we can remove the member
170 # and retrieve new hash
171 etcdctl member remove ${MEMBER_HASH}
172 fi
173
174 echo "Adding new member"
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700175 etcdctl member add ${HOSTNAME} http://${HOSTNAME}.${SERVICE_NAME}:2380 | grep "^ETCD_" > /var/run/etcd/new_member_envs
Ritu Sood23699102019-04-24 23:06:46 +0000176
177 if [ $? -ne 0 ]; then
178 echo "Exiting"
179 rm -f /var/run/etcd/new_member_envs
180 exit 1
181 fi
182
183 cat /var/run/etcd/new_member_envs
184 source /var/run/etcd/new_member_envs
185
186 collect_member &
187
188 exec etcd --name ${HOSTNAME} \
189 --listen-peer-urls http://0.0.0.0:2380 \
190 --listen-client-urls http://0.0.0.0:2379 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700191 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000192 --data-dir /var/run/etcd/default.etcd \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700193 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
Ritu Sood23699102019-04-24 23:06:46 +0000194 --initial-cluster ${ETCD_INITIAL_CLUSTER} \
195 --initial-cluster-state ${ETCD_INITIAL_CLUSTER_STATE}
196 fi
197
198 PEERS=""
199 for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700200 PEERS="${PEERS}${PEERS:+,}${SET_NAME}-${i}=http://${SET_NAME}-${i}.${SERVICE_NAME}:2380"
Ritu Sood23699102019-04-24 23:06:46 +0000201 done
202
203 collect_member &
204
205 # join member
206 exec etcd --name ${HOSTNAME} \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700207 --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \
Ritu Sood23699102019-04-24 23:06:46 +0000208 --listen-peer-urls http://0.0.0.0:2380 \
209 --listen-client-urls http://0.0.0.0:2379 \
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700210 --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \
Ritu Sood23699102019-04-24 23:06:46 +0000211 --initial-cluster-token etcd-cluster-1 \
212 --initial-cluster ${PEERS} \
213 --initial-cluster-state new \
214 --data-dir /var/run/etcd/default.etcd
215 volumeMounts:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700216 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000217 mountPath: /var/run/etcd
218 {{- if .Values.persistence.enabled }}
219 volumeClaimTemplates:
220 - metadata:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700221 name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000222 spec:
223 accessModes:
224 - "{{ .Values.persistence.accessMode }}"
225 resources:
226 requests:
227 # upstream recommended max is 700M
228 storage: "{{ .Values.persistence.storage }}"
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700229 storageClassName: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000230 {{- else }}
231 volumes:
Kiran Kamineni56c00a82019-05-20 17:19:40 -0700232 - name: {{ include "common.fullname" . }}-data
Ritu Sood23699102019-04-24 23:06:46 +0000233 {{- if .Values.memoryMode }}
234 emptyDir:
235 medium: Memory
236 {{- else }}
237 emptyDir: {}
238 {{- end }}
239 {{- end }}
240