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 |
| 18 | metadata: |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 19 | name: {{ include "common.fullname" . }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 20 | labels: |
| 21 | heritage: "{{ .Release.Service }}" |
Krzysztof Opasiak | 137d7cc | 2020-01-24 23:49:11 +0100 | [diff] [blame] | 22 | release: "{{ include "common.release" . }}" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 23 | chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 24 | app: {{ include "common.name" . }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 25 | spec: |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 26 | serviceName: {{ include "common.servicename" .}} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 27 | replicas: {{ .Values.replicaCount }} |
milaszki | c03c318 | 2020-07-07 12:13:44 +0000 | [diff] [blame] | 28 | selector: |
| 29 | matchLabels: |
| 30 | app: {{ include "common.name" . }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 31 | template: |
| 32 | metadata: |
| 33 | labels: |
| 34 | heritage: "{{ .Release.Service }}" |
Krzysztof Opasiak | 137d7cc | 2020-01-24 23:49:11 +0100 | [diff] [blame] | 35 | release: "{{ include "common.release" . }}" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 36 | 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 Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 52 | - name: {{ include "common.fullname" . }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 53 | image: "{{ .Values.repository }}/{{ .Values.image }}" |
| 54 | 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 Kamineni | f02245c | 2019-05-28 13:19:32 -0700 | [diff] [blame] | 62 | tcpSocket: |
| 63 | port: {{ .Values.service.clientInternalPort }} |
| 64 | initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }} |
| 65 | periodSeconds: {{ .Values.liveness.periodSeconds }} |
| 66 | timeoutSeconds: {{ .Values.liveness.timeoutSeconds }} |
| 67 | {{ end -}} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 68 | resources: |
| 69 | {{ include "common.resources" . | indent 10 }} |
| 70 | env: |
| 71 | - name: INITIAL_CLUSTER_SIZE |
| 72 | value: {{ .Values.replicaCount | quote }} |
| 73 | - name: SET_NAME |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 74 | value: {{ include "common.fullname" . }} |
| 75 | - name: SERVICE_NAME |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 76 | 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 Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 89 | EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 90 | done |
| 91 | |
| 92 | HOSTNAME=$(hostname) |
| 93 | |
| 94 | member_hash() { |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 95 | 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] | 96 | } |
| 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 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 > /var/run/etcd/member_id |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 118 | exit 0 |
| 119 | } |
| 120 | |
| 121 | eps() { |
| 122 | EPS="" |
| 123 | for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 124 | EPS="${EPS}${EPS:+,}http://${SET_NAME}-${i}.${SERVICE_NAME}:2379" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 125 | done |
| 126 | echo ${EPS} |
| 127 | } |
| 128 | |
| 129 | member_hash() { |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 130 | 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] | 131 | } |
| 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 |
| 136 | while true; do |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 137 | echo "Waiting for ${SET_NAME}-${i}.${SERVICE_NAME} to come up" |
| 138 | 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] | 139 | sleep 1s |
| 140 | done |
| 141 | done |
| 142 | |
| 143 | # re-joining after failure? |
Kiran Kamineni | f02245c | 2019-05-28 13:19:32 -0700 | [diff] [blame] | 144 | 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] | 145 | echo "Re-joining etcd member" |
| 146 | member_id=$(cat /var/run/etcd/member_id) |
| 147 | |
| 148 | # re-join member |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 149 | 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] | 150 | exec etcd --name ${HOSTNAME} \ |
| 151 | --listen-peer-urls http://0.0.0.0:2380 \ |
| 152 | --listen-client-urls http://0.0.0.0:2379\ |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 153 | --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \ |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 154 | --data-dir /var/run/etcd/default.etcd |
| 155 | fi |
| 156 | |
| 157 | # etcd-SET_ID |
| 158 | SET_ID=${HOSTNAME##*[^0-9]} |
| 159 | |
| 160 | # adding a new member to existing cluster (assuming all initial pods are available) |
| 161 | if [ "${SET_ID}" -ge ${INITIAL_CLUSTER_SIZE} ]; then |
| 162 | export ETCDCTL_ENDPOINT=$(eps) |
| 163 | |
| 164 | # member already added? |
| 165 | MEMBER_HASH=$(member_hash) |
| 166 | if [ -n "${MEMBER_HASH}" ]; then |
| 167 | # the member hash exists but for some reason etcd failed |
| 168 | # as the datadir has not be created, we can remove the member |
| 169 | # and retrieve new hash |
| 170 | etcdctl member remove ${MEMBER_HASH} |
| 171 | fi |
| 172 | |
| 173 | echo "Adding new member" |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 174 | 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] | 175 | |
| 176 | if [ $? -ne 0 ]; then |
| 177 | echo "Exiting" |
| 178 | rm -f /var/run/etcd/new_member_envs |
| 179 | exit 1 |
| 180 | fi |
| 181 | |
| 182 | cat /var/run/etcd/new_member_envs |
| 183 | source /var/run/etcd/new_member_envs |
| 184 | |
| 185 | collect_member & |
| 186 | |
| 187 | exec etcd --name ${HOSTNAME} \ |
| 188 | --listen-peer-urls http://0.0.0.0:2380 \ |
| 189 | --listen-client-urls http://0.0.0.0:2379 \ |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 190 | --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \ |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 191 | --data-dir /var/run/etcd/default.etcd \ |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 192 | --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \ |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 193 | --initial-cluster ${ETCD_INITIAL_CLUSTER} \ |
| 194 | --initial-cluster-state ${ETCD_INITIAL_CLUSTER_STATE} |
| 195 | fi |
| 196 | |
| 197 | PEERS="" |
| 198 | for i in $(seq 0 $((${INITIAL_CLUSTER_SIZE} - 1))); do |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 199 | 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] | 200 | done |
| 201 | |
| 202 | collect_member & |
| 203 | |
| 204 | # join member |
| 205 | exec etcd --name ${HOSTNAME} \ |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 206 | --initial-advertise-peer-urls http://${HOSTNAME}.${SERVICE_NAME}:2380 \ |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 207 | --listen-peer-urls http://0.0.0.0:2380 \ |
| 208 | --listen-client-urls http://0.0.0.0:2379 \ |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 209 | --advertise-client-urls http://${HOSTNAME}.${SERVICE_NAME}:2379 \ |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 210 | --initial-cluster-token etcd-cluster-1 \ |
| 211 | --initial-cluster ${PEERS} \ |
| 212 | --initial-cluster-state new \ |
| 213 | --data-dir /var/run/etcd/default.etcd |
| 214 | volumeMounts: |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 215 | - name: {{ include "common.fullname" . }}-data |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 216 | mountPath: /var/run/etcd |
| 217 | {{- if .Values.persistence.enabled }} |
| 218 | volumeClaimTemplates: |
| 219 | - metadata: |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 220 | name: {{ include "common.fullname" . }}-data |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 221 | labels: |
| 222 | name: {{ include "common.fullname" . }} |
| 223 | chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" |
Krzysztof Opasiak | 137d7cc | 2020-01-24 23:49:11 +0100 | [diff] [blame] | 224 | release: "{{ include "common.release" . }}" |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 225 | heritage: "{{ .Release.Service }}" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 226 | spec: |
| 227 | accessModes: |
Sylvain Desbureaux | b7ed2ee | 2019-11-29 11:35:13 +0100 | [diff] [blame] | 228 | - "{{ .Values.persistence.accessMode }}" |
| 229 | storageClassName: {{ include "common.storageClass" . }} |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 230 | resources: |
| 231 | requests: |
| 232 | # upstream recommended max is 700M |
| 233 | storage: "{{ .Values.persistence.storage }}" |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 234 | {{- else }} |
| 235 | volumes: |
Kiran Kamineni | 56c00a8 | 2019-05-20 17:19:40 -0700 | [diff] [blame] | 236 | - name: {{ include "common.fullname" . }}-data |
Ritu Sood | 2369910 | 2019-04-24 23:06:46 +0000 | [diff] [blame] | 237 | {{- if .Values.memoryMode }} |
| 238 | emptyDir: |
| 239 | medium: Memory |
| 240 | {{- else }} |
| 241 | emptyDir: {} |
| 242 | {{- end }} |
| 243 | {{- end }} |