apiVersion: {{ template "statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ include "elasticsearch.master.fullname" . }}
  labels: {{- include "elasticsearch.labels" . | nindent 4 }}
    app.kubernetes.io/component: master
    ## Istio Labels: https://istio.io/docs/ops/deployment/requirements/
    app: master
spec:
  updateStrategy:
    type: {{ .Values.master.updateStrategy.type }}
    {{- if (eq "OnDelete" .Values.master.updateStrategy.type) }}
    rollingUpdate: null
    {{- end }}
  selector:
    matchLabels: {{- include "elasticsearch.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: master
  podManagementPolicy: Parallel
  replicas: {{ .Values.master.replicas }}
  serviceName: {{ template "elasticsearch.master.fullname" . }}
  template:
    metadata:
      labels: {{- include "elasticsearch.labels" . | nindent 8 }}
        app.kubernetes.io/component: master
        ## Istio Labels: https://istio.io/docs/ops/deployment/requirements/
        app: master
        {{- if .Values.master.podLabels }}
        {{- include "elasticsearch.tplValue" (dict "value" .Values.master.podLabels "context" $) | nindent 8 }}
        {{- end }}
      {{- with .Values.master.podAnnotations }}
      annotations: {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
{{- include "elasticsearch.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.master.affinity }}
      affinity: {{- include "elasticsearch.tplValue" (dict "value" .Values.master.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.master.podAffinityPreset "component" "master" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.master.podAntiAffinityPreset "component" "master" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.master.nodeAffinityPreset.type "key" .Values.master.nodeAffinityPreset.key "values" .Values.master.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.master.nodeSelector }}
      nodeSelector: {{- include "elasticsearch.tplValue" (dict "value" .Values.master.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.master.tolerations }}
      tolerations: {{- include "elasticsearch.tplValue" (dict "value" .Values.master.tolerations "context" $) | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ template "elasticsearch.master.serviceAccountName" . }}
      {{- if .Values.master.securityContext.enabled }}
      securityContext:
        fsGroup: {{ .Values.master.securityContext.fsGroup }}
      {{- end }}
      {{- if or .Values.sysctlImage.enabled (and .Values.volumePermissions.enabled .Values.master.persistence.enabled) }}
      initContainers:
        {{- if .Values.sysctlImage.enabled }}
        ## Image that performs the sysctl operation to modify Kernel settings (needed sometimes to avoid boot errors)
        - name: sysctl
          image: {{ include "elasticsearch.sysctl.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          command:
            - /scripts/sysctl.sh
          securityContext:
            privileged: true
          {{- if .Values.sysctlImage.resources }}
          resources: {{- toYaml .Values.sysctlImage.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: initcontainer-script
              mountPath: /scripts/sysctl.sh
              subPath: sysctl.sh
        {{- end }}
        {{- if and .Values.volumePermissions.enabled .Values.master.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "elasticsearch.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              chown -R {{ .Values.master.securityContext.runAsUser }}:{{ .Values.master.securityContext.fsGroup }} //bitnami/elasticsearch/data
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: "/bitnami/elasticsearch/data"
        {{- end }}
      {{- end }}
      containers:
        - name: elasticsearch
          image: {{ include "elasticsearch.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.master.securityContext.enabled }}
          securityContext:
            runAsUser: {{ .Values.master.securityContext.runAsUser }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" .Values.image.debug | quote }}
            - name: ELASTICSEARCH_CLUSTER_NAME
              value: {{ .Values.name | quote }}
            - name: ELASTICSEARCH_CLUSTER_HOSTS
              value: {{ include "elasticsearch.hosts" . | quote }}
            - name: ELASTICSEARCH_TOTAL_NODES
              value: {{ add .Values.master.replicas .Values.data.replicas | quote }}
            - name: ELASTICSEARCH_CLUSTER_MASTER_HOSTS
              {{- $elasticsearchMasterFullname := include "elasticsearch.master.fullname" . }}
              {{- $replicas := int .Values.master.replicas }}
              value: {{range $i, $e := until $replicas }}{{ $elasticsearchMasterFullname }}-{{ $e }} {{ end }}
            - name: ELASTICSEARCH_MINIMUM_MASTER_NODES
              value: {{ add (div .Values.master.replicas 2) 1 | quote }}
            {{- if .Values.plugins }}
            - name: ELASTICSEARCH_PLUGINS
              value: {{ .Values.plugins | quote }}
            {{- end }}
            {{- if .Values.snapshotRepoPath }}
            - name: ELASTICSEARCH_FS_SNAPSHOT_REPO_PATH
              value: {{ .Values.snapshotRepoPath | quote }}
            {{- end }}
            - name: ELASTICSEARCH_HEAP_SIZE
              value: {{ .Values.master.heapSize | quote }}
            - name: ELASTICSEARCH_IS_DEDICATED_NODE
              value: "yes"
            - name: ELASTICSEARCH_NODE_TYPE
              value: "master"
            {{- if .Values.extraEnvVars }}
            {{- include "elasticsearch.tplValue" ( dict "value" .Values.extraEnvVars "context" $ ) | nindent 12 }}
            {{- end }}
          {{- if or .Values.extraEnvVarsConfigMap .Values.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.extraEnvVarsConfigMap }}
            - configMapRef:
                name: {{ include "elasticsearch.tplValue" ( dict "value" .Values.extraEnvVarsConfigMap "context" $ ) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "elasticsearch.tplValue" ( dict "value" .Values.extraEnvVarsSecret "context" $ ) }}
            {{- end }}
          {{- end }}
          ports:
            - name: http
              containerPort: 9200
            - name: transport
              containerPort: 9300
          {{- if .Values.master.livenessProbe.enabled }}
          livenessProbe:
            initialDelaySeconds: {{ .Values.master.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.master.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.master.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.master.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.master.livenessProbe.failureThreshold }}
            httpGet:
              path: /_cluster/health?local=true
              port: 9200
          {{- end }}
          {{- if .Values.master.readinessProbe.enabled }}
          readinessProbe:
            initialDelaySeconds: {{ .Values.master.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.master.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.master.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.master.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.master.livenessProbe.failureThreshold }}
            httpGet:
              path: /_cluster/health?local=true
              port: 9200
          {{- end }}
          {{- if .Values.master.resources }}
          resources: {{- toYaml .Values.master.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            {{- if .Values.config }}
            - mountPath: /opt/bitnami/elasticsearch/config/elasticsearch.yml
              name: config
              subPath: elasticsearch.yml
            {{- end }}
            - name: data
              mountPath: /bitnami/elasticsearch/data
            {{- if .Values.extraVolumeMounts }}
            {{- toYaml .Values.extraVolumeMounts | nindent 12 }}
            {{- end }}
            {{- if .Values.initScripts }}
            - name: custom-init-scripts
              mountPath: /docker-entrypoint-initdb.d/init-scripts
            {{- end }}
            {{- if .Values.initScriptsCM }}
            - name: custom-init-scripts-cm
              mountPath: /docker-entrypoint-initdb.d/init-scripts-cm
            {{- end }}
            {{- if .Values.initScriptsSecret }}
            - name: custom-init-scripts-secret
              mountPath: /docker-entrypoint-initdb.d/init-scripts-secret
            {{- end }}
      volumes:
        {{- if .Values.sysctlImage.enabled }}
        - name: initcontainer-script
          configMap:
            name: {{ include "elasticsearch.fullname" . }}-initcontainer
            defaultMode: 0755
        {{- end }}
        {{- if .Values.config }}
        - name: config
          configMap:
            name: {{ include "elasticsearch.fullname" . }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- toYaml .Values.extraVolumes | nindent 8 }}
        {{- end }}
        {{- if .Values.initScripts }}
        - name: custom-init-scripts
          configMap:
            name: {{ template "elasticsearch.initScripts" . }}
        {{- end }}
        {{- if .Values.initScriptsCM }}
        - name: custom-init-scripts-cm
          configMap:
            name: {{ template "elasticsearch.initScriptsCM" . }}
        {{- end }}
        {{- if .Values.initScriptsSecret }}
        - name: custom-init-scripts-secret
          secret:
            secretName: {{ template "elasticsearch.initScriptsSecret" . }}
            defaultMode: 0755
        {{- end }}
{{- if not .Values.master.persistence.enabled }}
        - name: "data"
          emptyDir: {}
{{- else if .Values.master.persistence.existingClaim }}
        - name: "data"
          persistentVolumeClaim:
            claimName: {{ .Values.master.persistence.existingClaim }}
{{- else }}
  volumeClaimTemplates:
    - metadata:
        name: "data"
        {{- if .Values.master.persistence.annotations }}
        annotations: {{- toYaml .Values.master.persistence.annotations | nindent 10 }}
        {{- end }}
      spec:
        accessModes: {{- toYaml .Values.master.persistence.accessModes | nindent 10 }}
        {{ $storage := dict "global" .Values.global "local" .Values.master }}
        {{ include "elasticsearch.storageClass" $storage  }}
        resources:
          requests:
            storage: {{ .Values.master.persistence.size | quote }}
        {{- if .Values.master.persistence.existingVolume }}
        selector:
          matchLabels:
            volume: {{ .Values.master.persistence.existingVolume }}
        {{- end }}
{{- end }}
