[COMMON] Adding Common Template for cassa-operator
[oom.git] / kubernetes / common / cassandra / templates / statefulset.yaml
1 {{/*
2 # Copyright © 2022 Amdocs, AT&T, Bell Canada, Bitnami
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.
15 */}}
16
17 {{- if not .Values.k8ssandraOperator.enabled }}
18 apiVersion: apps/v1
19 kind: StatefulSet
20 metadata: {{- include "common.resourceMetadata" . | nindent 2 }}
21 spec:
22   selector: {{- include "common.selectors" . | nindent 4 }}
23   serviceName: {{ include "common.servicename" . }}
24   replicas: {{ .Values.replicaCount }}
25   podManagementPolicy: {{ .Values.podManagementPolicy }}
26   updateStrategy:
27     type: {{ .Values.updateStrategy.type }}
28   template:
29     metadata: {{- include "common.templateMetadata" . | nindent 6 }}
30       {{- if or .Values.podAnnotations (and .Values.metrics.serviceMonitor.enabled .Values.metrics.podAnnotations) }}
31       annotations:
32         {{- if .Values.podAnnotations }}
33         {{- include "common.tplValue" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
34         {{- end }}
35         {{- if and .Values.metrics.serviceMonitor.enabled .Values.metrics.podAnnotations }}
36         {{- include "common.tplValue" (dict "value" .Values.metrics.podAnnotations "context" $) | nindent 8 }}
37         {{- end }}
38       {{- end }}
39     spec:
40       hostNetwork: {{ .Values.hostNetwork }}
41       imagePullSecrets:
42       - name: "{{ include "common.namespace" . }}-docker-registry-key"
43       containers:
44       - name: {{ include "common.name" . }}
45         image: {{ include "repositoryGenerator.dockerHubRepository" . }}/{{ .Values.image }}
46         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
47         ports: {{ include "common.containerPorts" . | nindent 8  }}
48         volumeMounts:
49           - name: {{ include "common.fullname" . }}-data
50             mountPath: /var/lib/cassandra
51           - name: localtime
52             mountPath: /etc/localtime
53             readOnly: true
54           - name: cassandra-entrypoint
55             mountPath: /docker-entrypoint.sh
56             subPath: docker-entrypoint.sh
57           {{- range $key, $value := .Values.configOverrides }}
58           - name: cassandra-config-{{ $key | replace "." "-" }}
59             mountPath: /etc/cassandra/{{ $key }}
60             subPath: {{ $key }}
61           {{- end }}
62         {{- if eq .Values.liveness.enabled true }}
63         livenessProbe:
64           exec:
65             command:
66             - /bin/bash
67             - -c
68             - nodetool status | grep $POD_IP | awk '$1!="UN" { exit 1; }'
69           initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
70           periodSeconds: {{ .Values.liveness.periodSeconds }}
71           timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
72           successThreshold: {{ .Values.liveness.successThreshold }}
73           failureThreshold: {{ .Values.liveness.failureThreshold }}
74         {{ end }}
75         readinessProbe:
76           exec:
77             command:
78             - /bin/bash
79             - -c
80             - nodetool status | grep $POD_IP | awk '$1!="UN" { exit 1; }'
81           initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds }}
82           periodSeconds: {{ .Values.readiness.periodSeconds }}
83           timeoutSeconds: {{ .Values.readiness.timeoutSeconds }}
84           successThreshold: {{ .Values.readiness.successThreshold }}
85           failureThreshold: {{ .Values.readiness.failureThreshold }}
86         startupProbe:
87           exec:
88             command:
89             - /bin/bash
90             - -c
91             - nodetool status | grep $POD_IP | awk '$1!="UN" { exit 1; }'
92           initialDelaySeconds: {{ .Values.startup.initialDelaySeconds }}
93           periodSeconds: {{ .Values.startup.periodSeconds }}
94           timeoutSeconds: {{ .Values.startup.timeoutSeconds }}
95           successThreshold: {{ .Values.startup.successThreshold }}
96           failureThreshold: {{ .Values.startup.failureThreshold }}
97         env:
98           {{- $seed_size := default 1 .Values.replicaCount | int -}}
99           {{- $global := . }}
100           - name: CASSANDRA_SEEDS
101           {{- if .Values.hostNetwork }}
102             value: {{ required "You must fill \".Values.config.seeds\" with list of Cassandra seeds when hostNetwork is set to true" .Values.config.seeds | quote }}
103           {{- else }}
104             value: "{{- range $i, $e := until $seed_size }}{{ template "common.fullname" $global }}-{{ $i }}.{{ template "common.servicename" $global }}.{{ $global.Release.Namespace }}.svc.{{ $global.Values.config.cluster_domain }}{{- if (lt ( add1 $i ) $seed_size ) }},{{- end }}{{- end }}"
105           {{- end }}
106           - name: MAX_HEAP_SIZE
107             value: {{ .Values.config.heap.max }}
108           - name: HEAP_NEWSIZE
109             value: {{ .Values.config.heap.min }}
110           - name: JVM_OPTS
111             value: {{ .Values.config.jvmOpts | quote }}
112           - name: CASSANDRA_CLUSTER_NAME
113             value: {{ .Values.config.clusterName | quote }}
114           - name: CASSANDRA_DC
115             value: {{ .Values.config.dataCenter | quote }}
116           - name: CASSANDRA_RACK
117             value: {{ .Values.config.rackName | quote }}
118           - name: CASSANDRA_AUTO_BOOTSTRAP
119             value: {{ .Values.config.autoBootstrap | quote }}
120           - name: CASSANDRA_START_RPC
121             value: {{ default "true" .Values.config.start_rpc | quote }}
122           - name: CASSANDRA_ENDPOINT_SNITCH
123             value: {{ default "GossipingPropertyFileSnitch" .Values.config.endpoint_snitch | quote }}
124           - name: CASSANDRA_AUTHENTICATOR
125             value: {{ default "PasswordAuthenticator" .Values.config.authenticator | quote }}
126           - name: POD_IP
127             valueFrom:
128               fieldRef:
129                 fieldPath: status.podIP
130         lifecycle:
131           preStop:
132             exec:
133         {{- if not .Values.persistence.enabled }}
134               command: ["/bin/sh", "-c", "exec nodetool decommission"]
135         {{- else }}
136               command: ["/bin/sh", "-c", "PID=$(pidof java) && kill $PID && while ps -p $PID > /dev/null; do sleep 1; done"]
137         {{- end }}
138         resources: {{ toYaml .Values.resources | nindent 10 }}
139       {{- if .Values.metrics.serviceMonitor.enabled }}
140       - name: {{ include "common.name" . }}-metrics
141         image: {{ include "repositoryGenerator.dockerHubRepository" . }}/{{ .Values.metrics.image }}
142         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.metrics.pullPolicy | quote}}
143         {{- if (.Values.metrics.enabled) }}
144         ports:
145         {{- range $index, $metricPort := .Values.metrics.ports }}
146         - name: {{ $metricPort.name }}
147           containerPort: {{ $metricPort.port }}
148           protocol: TCP
149         {{- end }}
150         livenessProbe:
151           httpGet:
152             path: {{ .Values.metrics.livenessProbe.httpGet.path }}
153             port: {{ .Values.metrics.livenessProbe.httpGet.port }}
154           initialDelaySeconds: {{ .Values.metrics.livenessProbe.initialDelaySeconds }}
155           periodSeconds: {{ .Values.metrics.livenessProbe.periodSeconds }}
156           timeoutSeconds: {{ .Values.metrics.livenessProbe.timeoutSeconds }}
157           successThreshold: {{ .Values.metrics.livenessProbe.successThreshold }}
158           failureThreshold: {{ .Values.metrics.livenessProbe.failureThreshold }}
159         readinessProbe:
160           httpGet:
161             path: {{ .Values.metrics.readinessProbe.httpGet.path }}
162             port: {{ .Values.metrics.readinessProbe.httpGet.port }}
163           initialDelaySeconds: {{ .Values.metrics.readinessProbe.initialDelaySeconds }}
164           periodSeconds: {{ .Values.metrics.readinessProbe.periodSeconds }}
165           timeoutSeconds: {{ .Values.metrics.readinessProbe.timeoutSeconds }}
166           successThreshold: {{ .Values.metrics.readinessProbe.successThreshold }}
167           failureThreshold: {{ .Values.metrics.readinessProbe.failureThreshold }}
168         {{- end }}
169         {{ include "common.containerSecurityContext" . | indent 10 | trim }}
170         {{- if .Values.metrics.resources }}
171         resources: {{- toYaml .Values.metrics.resources | nindent 10 }}
172         {{- end }}
173       {{- end }}
174       {{- if .Values.nodeSelector }}
175       nodeSelector: {{ toYaml .Values.nodeSelector | nindent 8 }}
176       {{- end -}}
177       {{- if .Values.affinity }}
178       affinity: {{ toYaml .Values.affinity | nindent 8 }}
179       {{- end }}
180       serviceAccountName: {{ include "common.fullname" (dict "suffix" "nothing" "dot" . )}}
181       volumes:
182       - name: localtime
183         hostPath:
184           path: /etc/localtime
185       {{- range $key, $value := .Values.configOverrides }}
186       - name: cassandra-config-{{ $key | replace "." "-" }}
187         configMap:
188           name: {{ include "common.fullname" . }}-configOverrides
189       {{- end }}
190       - name: cassandra-entrypoint
191         configMap:
192           name: {{ include "common.fullname" . }}-entrypoint
193           defaultMode: 0755
194   {{- if not .Values.persistence.enabled }}
195       - name: {{ include "common.fullname" . }}-data
196         emptyDir: {}
197   {{- else }}
198   volumeClaimTemplates:
199     - metadata:
200         name: {{ include "common.fullname" . }}-data
201         labels:
202           name: {{ include "common.fullname" . }}
203           chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
204           release: "{{ include "common.release" . }}"
205           heritage: "{{ .Release.Service }}"
206       spec:
207         accessModes:
208         {{- if .Values.backup.enabled }}
209         - ReadWriteMany
210         {{- else }}
211         - ReadWriteOnce
212         {{- end }}
213         storageClassName: {{ include "common.storageClass" . }}
214         resources:
215           requests:
216             storage: {{ .Values.persistence.size | quote }}
217   {{- end }}
218 {{- end }}