# helm_charts_mancenter-statefulset.yaml
{{- if .Values.mancenter.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "mancenter.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ template "hazelcast.name" . }}
    helm.sh/chart: {{ template "hazelcast.chart" . }}
    app.kubernetes.io/instance: "{{ .Release.Name }}"
    app.kubernetes.io/managed-by: "{{ .Release.Service }}"
spec:
  serviceName: {{ template "mancenter.fullname" . }}
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "hazelcast.name" . }}
      app.kubernetes.io/instance: "{{ .Release.Name }}"
      role: mancenter
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ template "hazelcast.name" . }}
        helm.sh/chart: {{ template "hazelcast.chart" . }}
        app.kubernetes.io/instance: "{{ .Release.Name }}"
        app.kubernetes.io/managed-by: "{{ .Release.Service }}"
        role: mancenter
    spec:
      {{- if .Values.mancenter.image.pullSecrets }}
      imagePullSecrets:
        {{- range .Values.mancenter.image.pullSecrets }}
        - name: {{ . }}
        {{- end}}
      {{- end}}
      {{- if .Values.mancenter.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.mancenter.nodeSelector | indent 8 }}
      {{- end }}
      hostNetwork: false
      hostPID: false
      hostIPC: false
      {{- if .Values.securityContext.enabled }}
      securityContext:
        runAsNonRoot: {{ if eq (int .Values.securityContext.runAsUser) 0 }}false{{ else }}true{{ end }}
        runAsUser: {{ .Values.securityContext.runAsUser }}
        runAsGroup: {{ .Values.securityContext.runAsGroup }}
        fsGroup: {{ .Values.securityContext.fsGroup }}
      {{- end }}
      {{- if .Values.mancenter.affinity }}
      affinity:
{{ toYaml .Values.mancenter.affinity | indent 8 }}
      {{- end }}
      {{- if .Values.mancenter.tolerations }}
      tolerations:
{{ toYaml .Values.mancenter.tolerations | indent 8 }}
      {{- end }}
      containers:
      - name: {{ template "mancenter.fullname" . }}
        image: "{{ .Values.mancenter.image.repository }}:{{ .Values.mancenter.image.tag }}"
        imagePullPolicy: {{ .Values.mancenter.image.pullPolicy | quote }}
        resources:
{{ toYaml .Values.mancenter.resources | indent 10 }}
        ports:
        - name: mancenter
          containerPort: {{ if .Values.mancenter.ssl }}8443{{ else }}8080{{ end }}
        {{- if .Values.mancenter.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: {{ if .Values.mancenter.contextPath }}{{ .Values.mancenter.contextPath }}{{ end }}/health
            port: 8081
          initialDelaySeconds: {{ .Values.mancenter.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.mancenter.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.mancenter.livenessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.mancenter.livenessProbe.successThreshold }}
          failureThreshold: {{ .Values.mancenter.livenessProbe.failureThreshold }}
        {{- end }}
        {{- if .Values.mancenter.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: {{ if .Values.mancenter.contextPath }}{{ .Values.mancenter.contextPath }}{{ end }}/health
            port: 8081
          initialDelaySeconds: {{ .Values.mancenter.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.mancenter.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.mancenter.readinessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.mancenter.readinessProbe.successThreshold }}
          failureThreshold: {{ .Values.mancenter.readinessProbe.failureThreshold }}
        {{- end }}
        volumeMounts:
        - name: config
          mountPath: /config
        - name: mancenter-storage
          mountPath: /data
        {{- if .Values.mancenter.secretsMountName }}
        - name: mancenter-secrets
          mountPath: /secrets
        {{- end }}
        env:
        {{- if .Values.mancenter.contextPath }}
        - name: MC_CONTEXT_PATH
          value: {{ .Values.mancenter.contextPath }}
        {{- end }}
        - name: MC_LICENSE_KEY
        {{- if .Values.mancenter.licenseKeySecretName }}
          valueFrom:
            secretKeyRef:
              name: {{ .Values.mancenter.licenseKeySecretName }}
              key: key
        {{- else }}
          value: {{ .Values.mancenter.licenseKey }}
        {{- end }}
        - name: MC_INIT_CMD
          value: "./mc-conf.sh cluster add --lenient=true -H /data -cc /config/hazelcast-client.yaml"
        - name: JAVA_OPTS
          value: "{{ if or .Values.mancenter.licenseKey .Values.mancenter.licenseKeySecretName }}-Dhazelcast.mc.license=$(MC_LICENSE_KEY){{ end }} {{ if or .Values.mancenter.readinessProbe.enabled .Values.mancenter.livenessProbe.enabled }}-Dhazelcast.mc.healthCheck.enable=true{{ end }} -DserviceName={{ template "hazelcast.serviceName" . }} -Dhazelcast.mc.tls.enabled={{ .Values.mancenter.ssl }} {{ .Values.mancenter.javaOpts }}"
        {{- if .Values.securityContext.enabled }}
        securityContext:
          runAsNonRoot: {{ if eq (int .Values.securityContext.runAsUser) 0 }}false{{ else }}true{{ end }}
          runAsUser: {{ .Values.securityContext.runAsUser }}
          runAsGroup: {{ .Values.securityContext.runAsGroup }}
          privileged: false
          readOnlyRootFilesystem: false
          allowPrivilegeEscalation: false
          capabilities:
            drop:
            - ALL
        {{- end }}
      {{- if .Values.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.securityContext.fsGroup }}
      {{- end }}
      serviceAccountName: {{ template "hazelcast.serviceAccountName" . }}
      volumes:
      - name: config
        configMap:
           {{- if .Values.mancenter.existingConfigMap }}
           name: {{ .Values.mancenter.existingConfigMap }}
           {{- else }}
           name: {{ template "mancenter.fullname" . }}-configuration
           {{- end }}
      {{- if .Values.mancenter.secretsMountName }}
      - name: mancenter-secrets
        secret:
          secretName: {{ .Values.mancenter.secretsMountName }}
      {{- end }}
      - name: mancenter-storage
        {{- if and (eq .Values.mancenter.persistence.enabled true) .Values.mancenter.persistence.existingClaim }}
        persistentVolumeClaim:
          claimName: {{ .Values.mancenter.persistence.existingClaim }}
        {{- else if (eq .Values.mancenter.persistence.enabled false) }}
        emptyDir: {}
        {{- end }}
    {{ if and (eq .Values.mancenter.persistence.enabled true) (empty .Values.mancenter.persistence.existingClaim) }}
  volumeClaimTemplates:
  - metadata:
      name: mancenter-storage
      labels:
        app.kubernetes.io/name: {{ template "hazelcast.name" . }}
        helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
        app.kubernetes.io/instance: "{{ .Release.Name }}"
        app.kubernetes.io/managed-by: "{{ .Release.Service }}"
    spec:
      accessModes:
      {{- range .Values.mancenter.persistence.accessModes }}
        - {{ . | quote }}
      {{- end }}
      {{- if .Values.mancenter.persistence.storageClass }}
      {{- if (eq "-" .Values.mancenter.persistence.storageClass) }}
      storageClassName: ""
      {{- else }}
      storageClassName: "{{ .Values.mancenter.persistence.storageClass }}"
      {{- end }}
      {{- end }}
      resources:
        requests:
          storage: {{ .Values.mancenter.persistence.size | quote }}
  {{- end -}}
{{- end -}}