Jakub Latusek | 3659d9f | 2020-09-14 16:21:55 +0200 | [diff] [blame] | 1 | {{/* |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 2 | # 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 Latusek | 3659d9f | 2020-09-14 16:21:55 +0200 | [diff] [blame] | 15 | */}} |
milaszki | c03c318 | 2020-07-07 12:13:44 +0000 | [diff] [blame] | 16 | apiVersion: apps/v1 |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 17 | kind: StatefulSet |
Andreas Geissler | 8c55fdd | 2023-07-20 11:15:42 +0200 | [diff] [blame] | 18 | metadata: {{- include "common.resourceMetadata" . | nindent 2 }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 19 | spec: |
Andreas Geissler | 8c55fdd | 2023-07-20 11:15:42 +0200 | [diff] [blame] | 20 | selector: {{- include "common.selectors" . | nindent 4 }} |
| 21 | serviceName: {{ include "common.servicename" . }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 22 | replicas: {{ .Values.replicaCount }} |
| 23 | template: |
Andreas Geissler | 8c55fdd | 2023-07-20 11:15:42 +0200 | [diff] [blame] | 24 | metadata: {{- include "common.templateMetadata" . | nindent 6 }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 25 | 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-geissler | f84cccd | 2021-07-07 15:40:41 +0200 | [diff] [blame] | 38 | imagePullSecrets: |
| 39 | - name: "{{ include "common.namespace" . }}-docker-registry-key" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 40 | containers: |
krishnaa96 | 544863d | 2021-05-11 18:29:49 +0530 | [diff] [blame] | 41 | - name: {{ include "common.name" . }} |
Sylvain Desbureaux | c9f5533 | 2020-11-19 17:10:36 +0100 | [diff] [blame] | 42 | image: {{ include "repositoryGenerator.googleK8sRepository" . }}/{{ .Values.image }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 43 | 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 Kamineni | f02245c | 2019-05-28 13:19:32 -0700 | [diff] [blame] | 51 | tcpSocket: |
| 52 | port: {{ .Values.service.clientInternalPort }} |
| 53 | initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }} |
| 54 | periodSeconds: {{ .Values.liveness.periodSeconds }} |
| 55 | timeoutSeconds: {{ .Values.liveness.timeoutSeconds }} |
| 56 | {{ end -}} |
miroslavmasaryk | a7ac7f0 | 2023-03-01 14:12:26 +0100 | [diff] [blame] | 57 | resources: {{ include "common.resources" . | nindent 10 }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 58 | env: |
| 59 | - name: INITIAL_CLUSTER_SIZE |
| 60 | value: {{ .Values.replicaCount | quote }} |
| 61 | - name: SET_NAME |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 62 | value: {{ include "common.fullname" . }} |
| 63 | - name: SERVICE_NAME |
Sylvain Desbureaux | c991f0b | 2021-10-29 15:51:58 +0200 | [diff] [blame] | 64 | value: {{ include "common.servicename" . }}.{{ include "common.namespace" . }}.svc.{{ .Values.global.clusterName }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 65 | {{- 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 Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 77 | EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 78 | done |
| 79 | |
| 80 | HOSTNAME=$(hostname) |
| 81 | |
| 82 | member_hash() { |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 83 | etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1 |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 84 | } |
| 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 Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 105 | etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1 > /var/run/etcd/member_id |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 106 | exit 0 |
| 107 | } |
| 108 | |
| 109 | eps() { |
| 110 | EPS="" |
| 111 | for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 112 | EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 113 | done |
| 114 | echo ${EPS} |
| 115 | } |
| 116 | |
| 117 | member_hash() { |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 118 | etcdctl member list | grep http://${HOSTNAME}.${SERVICE_NAME}:2380 | cut -d':' -f1 | cut -d'[' -f1 |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 119 | } |
| 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ńka | e1d62ce | 2021-01-25 07:44:49 +0100 | [diff] [blame] | 124 | if [ "${SET_NAME}-${i}" == "${HOSTNAME}" ]; then |
| 125 | echo "Skipping self-checking" |
| 126 | continue |
| 127 | fi |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 128 | while true; do |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 129 | 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 Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 131 | sleep 1s |
| 132 | done |
| 133 | done |
| 134 | |
| 135 | # re-joining after failure? |
Kiran Kamineni | f02245c | 2019-05-28 13:19:32 -0700 | [diff] [blame] | 136 | if [[ -e /var/run/etcd/default.etcd && -f /var/run/etcd/member_id ]]; then |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 137 | echo "Re-joining etcd member" |
| 138 | member_id=$(cat /var/run/etcd/member_id) |
| 139 | |
| 140 | # re-join member |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 141 | ETCDCTL_ENDPOINT=$(eps) etcdctl member update ${member_id} http://${HOSTNAME}.${SERVICE_NAME}:2380 | true |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 142 | 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 Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 145 | --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \ |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 146 | --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 Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 166 | etcdctl member add ${HOSTNAME} http://${HOSTNAME}.${SERVICE_NAME}:2380 | grep "^ETCD_" > /var/run/etcd/new_member_envs |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 167 | |
| 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 Lambert | 2b6f82c | 2021-03-02 21:45:00 +0100 | [diff] [blame] | 175 | . /var/run/etcd/new_member_envs |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 176 | |
| 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 Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 182 | --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \ |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 183 | --data-dir /var/run/etcd/default.etcd \ |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 184 | --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \ |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 185 | --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 Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 191 | PEERS="${PEERS}${PEERS:+,}${SET_NAME}-${i}=http://${SET_NAME}-${i}.${SERVICE_NAME}:2380" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 192 | done |
| 193 | |
| 194 | collect_member & |
| 195 | |
| 196 | # join member |
| 197 | exec etcd --name ${HOSTNAME} \ |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 198 | --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \ |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 199 | --listen-peer-urls http://0.0.0.0:2380 \ |
| 200 | --listen-client-urls http://0.0.0.0:2379 \ |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 201 | --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \ |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 202 | --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 Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 207 | - name: {{ include "common.fullname" . }}-data |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 208 | mountPath: /var/run/etcd |
| 209 | {{- if .Values.persistence.enabled }} |
| 210 | volumeClaimTemplates: |
| 211 | - metadata: |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 212 | name: {{ include "common.fullname" . }}-data |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 213 | labels: |
| 214 | name: {{ include "common.fullname" . }} |
| 215 | chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" |
Krzysztof Opasiak | 137d7cc | 2020-01-24 23:49:11 +0100 | [diff] [blame] | 216 | release: "{{ include "common.release" . }}" |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 217 | heritage: "{{ .Release.Service }}" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 218 | spec: |
| 219 | accessModes: |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 220 | - "{{ .Values.persistence.accessMode }}" |
| 221 | storageClassName: {{ include "common.storageClass" . }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 222 | resources: |
| 223 | requests: |
| 224 | # upstream recommended max is 700M |
| 225 | storage: "{{ .Values.persistence.storage }}" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 226 | {{- else }} |
| 227 | volumes: |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 228 | - name: {{ include "common.fullname" . }}-data |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 229 | {{- if .Values.memoryMode }} |
| 230 | emptyDir: |
| 231 | medium: Memory |
| 232 | {{- else }} |
| 233 | emptyDir: {} |
| 234 | {{- end }} |
| 235 | {{- end }} |