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