{{- $locatorsReplicaCount := int .Values.locator.replicaCount }}
{{- $fullname := include "common.names.fullname" . }}
{{- $releaseNamespace := .Release.Namespace }}
{{- $clusterDomain := .Values.clusterDomain }}
{{- $locatorPort := int .Values.locator.service.ports.locator }}
{{- $locators := list }}
{{- range $e, $i := until $locatorsReplicaCount }}
{{- $locators = append $locators (printf "%s-locator-%d.%s-locator-hl.%s.svc.%s[%d]" $fullname $i $fullname $releaseNamespace $clusterDomain $locatorPort) }}
{{- end }}
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ printf "%s-locator" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: locator
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.locator.replicaCount }}
  podManagementPolicy: {{ .Values.locator.podManagementPolicy }}
  selector:
    matchLabels: {{ include "common.labels.matchLabels" . | nindent 6 }}
      app.kubernetes.io/component: locator
  serviceName: {{ printf "%s-locator-hl" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  {{- if .Values.locator.updateStrategy }}
  updateStrategy: {{- toYaml .Values.locator.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      annotations:
        {{- if .Values.locator.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.locator.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if (include "geode.createSecret" .) }}
        checksum/auth-secret: {{ include (print $.Template.BasePath "/secrets.yaml") . | sha256sum }}
        {{- end }}
        {{- if (include "geode.locator.createConfigmap" .) }}
        checksum/configuration: {{ include (print $.Template.BasePath "/locator/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if (include "geode.locator.log4j.createConfigmap" .) }}
        checksum/log4j-configuration: {{ include (print $.Template.BasePath "/locator/log4j-configmap.yaml") . | sha256sum }}
        {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        app.kubernetes.io/component: locator
        {{- if .Values.locator.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.locator.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ include "geode.serviceAccountName" . }}
      {{- include "geode.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.locator.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.locator.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.locator.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.locator.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.locator.podAffinityPreset "component" "locator" "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.locator.podAntiAffinityPreset "component" "locator" "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.locator.nodeAffinityPreset.type "key" .Values.locator.nodeAffinityPreset.key "values" .Values.locator.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.locator.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.locator.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.locator.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.locator.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.locator.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.locator.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.locator.priorityClassName }}
      priorityClassName: {{ .Values.locator.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.locator.schedulerName }}
      schedulerName: {{ .Values.locator.schedulerName }}
      {{- end }}
      {{- if .Values.locator.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.locator.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if .Values.locator.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.locator.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if or .Values.volumePermissions.enabled .Values.locator.initContainers }}
      initContainers:
        {{- if .Values.volumePermissions.enabled }}
        - name: volume-permissions
          image: {{ include "geode.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p /bitnami/geode/config /bitnami/geode/data
              chmod 700 /bitnami/geode/config /bitnami/geode/data
              find /bitnami/geode -mindepth 1 -maxdepth 1 -not -name "config" -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R {{ .Values.locator.containerSecurityContext.runAsUser }}:{{ .Values.locator.podSecurityContext.fsGroup }}
          {{- if .Values.volumePermissions.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.volumePermissions.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /bitnami/geode
        {{- end }}
        {{- if .Values.locator.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.locator.initContainers "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        - name: geode
          image: {{ include "geode.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.locator.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.locator.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.locator.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.locator.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - /bin/bash
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.locator.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.locator.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - -ec
            - |
              {{- if .Values.auth.tls.enabled }}
              ID="${MY_POD_NAME#"{{ $fullname }}-"}"
              mkdir -p /opt/bitnami/geode/config/certs
              if [[ -f "/certs/geode.truststore.jks" ]] && [[ -f "/certs/geode-locator-${ID}.truststore.jks" ]]; then
                  cp "/certs/geode.truststore.jks" "/opt/bitnami/geode/config/certs/geode-truststore.jks
                  cp "/certs/geode-locator-${ID}.truststore.jks" "/opt/bitnami/geode/config/certs/geode.keystore.jks"
              else
                  echo "Couldn't find the expected Java Key Stores (JKS) files! They are mandatory when encryption via TLS is enabled."
                  exit 1
              fi
              {{- end }}
              /opt/bitnami/scripts/geode/entrypoint.sh /opt/bitnami/scripts/geode/run.sh {{ join " " .Values.locator.extraFlags }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: MY_POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: GEODE_NODE_NAME
              value: "$(MY_POD_NAME)"
            - name: GEODE_NODE_TYPE
              value: "locator"
            - name: GEODE_ADVERTISED_HOSTNAME
              value: "$(MY_POD_NAME).{{ $fullname }}-locator-hl.{{ $releaseNamespace }}.svc.{{ $clusterDomain }}"
            {{- if gt $locatorsReplicaCount 1 }}
            - name: GEODE_LOCATORS
              value: {{ join "," $locators | quote }}
            {{- end }}
            {{- if .Values.groups }}
            - name: GEODE_GROUPS
              value: {{ join "," .Values.groups | quote }}
            {{- end }}
            - name: GEODE_LOCATOR_PORT_NUMBER
              value: {{ .Values.locator.containerPorts.locator | quote }}
            - name: GEODE_HTTP_PORT_NUMBER
              value: {{ .Values.locator.containerPorts.http | quote }}
            - name: GEODE_RMI_PORT_NUMBER
              value: {{ .Values.locator.containerPorts.rmi | quote }}
            - name: GEODE_ENABLE_METRICS
              value: {{ ternary "yes" "no" .Values.metrics.enabled | quote }}
            {{- if .Values.metrics.enabled }}
            - name: GEODE_METRICS_PORT_NUMBER
              value: {{ .Values.locator.containerPorts.metrics | quote }}
            {{- end }}
            - name: GEODE_LOG_LEVEL
              value: {{ .Values.locator.logLevel | quote }}
            {{- if .Values.locator.initialHeapSize }}
            - name: GEODE_INITIAL_HEAP_SIZE
              value: {{ .Values.locator.initialHeapSize | quote }}
            {{- end }}
            {{- if .Values.locator.maxHeapSize }}
            - name: GEODE_MAX_HEAP_SIZE
              value: {{ .Values.locator.maxHeapSize | quote }}
            {{- end }}
            - name: GEODE_ENABLE_SECURITY
              value: {{ ternary "yes" "no" (or .Values.auth.enabled .Values.auth.tls.enabled) | quote }}
            {{- if .Values.auth.enabled }}
            - name: GEODE_SECURITY_MANAGER
              value: {{ .Values.auth.securityManager | quote }}
            - name: GEODE_SECURITY_USERNAME
              valueFrom:
                secretKeyRef:
                  name: {{ include "geode.secretName" . }}
                  key: username
            - name: GEODE_SECURITY_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "geode.secretName" . }}
                  key: password
            {{- end }}
            {{- if .Values.auth.tls.enabled }}
            - name: GEODE_SECURITY_TLS_COMPONENTS
              value: {{ join "," .Values.auth.tls.components | quote }}
            - name: GEODE_SECURITY_TLS_REQUIRE_AUTHENTICATION
              value: {{ ternary "yes" "no" .Values.auth.tls.requireAuthentication | quote }}
            - name: GEODE_SECURITY_TLS_ENDPOINT_IDENTIFICATION_ENABLED
              value: {{ ternary "yes" "no" .Values.auth.tls.endpointIdentificationEnabled | quote }}
            {{- if not (empty .Values.auth.tls.keystorePassword) }}
            - name: GEODE_SECURITY_TLS_KEYSTORE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "geode.secretName" . }}
                  key: keystore-password
            {{- end }}
            {{- if not (empty .Values.auth.tls.truststorePassword) }}
            - name: GEODE_SECURITY_TLS_TRUSTSTORE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "geode.secretName" . }}
                  key: truststore-password
            {{- end }}
            {{- end }}
            {{- if .Values.locator.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.locator.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          {{- if or .Values.locator.extraEnvVarsCM .Values.locator.extraEnvVarsSecret }}
          envFrom:
            {{- if .Values.locator.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.locator.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.locator.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.locator.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- end }}
          {{- if .Values.locator.resources }}
          resources: {{- toYaml .Values.locator.resources | nindent 12 }}
          {{- end }}
          ports:
            - name: locator
              containerPort: {{ .Values.locator.containerPorts.locator }}
            - name: http
              containerPort: {{ .Values.locator.containerPorts.http }}
            - name: rmi
              containerPort: {{ .Values.locator.containerPorts.rmi }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.locator.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.locator.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - /bin/bash
                - -ec
                - |
                  gfsh -e "connect --locator=localhost[$GEODE_LOCATOR_PORT_NUMBER]{{ if .Values.auth.enabled }} --user=$GEODE_SECURITY_USERNAME --password=$GEODE_SECURITY_PASSWORD{{ end }}" || exit 1
          {{- else if .Values.locator.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.locator.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.locator.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.locator.readinessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - /bin/bash
                - -ec
                - |
                  gfsh -e "connect --locator=localhost[$GEODE_LOCATOR_PORT_NUMBER]{{ if .Values.auth.enabled }} --user=$GEODE_SECURITY_USERNAME --password=$GEODE_SECURITY_PASSWORD{{ end }}" || exit 1
          {{- else if .Values.locator.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.locator.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.locator.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.locator.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: locator
          {{- else if .Values.locator.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.locator.customStartupProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- end }}
          {{- if .Values.locator.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.locator.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /bitnami/geode
            {{- if or .Values.locator.configuration .Values.locator.existingConfigmap }}
            - name: configuration
              mountPath: /bitnami/geode/config/gemfire.properties
              subPath: gemfire.properties
            {{- end }}
            {{- if or .Values.locator.log4j .Values.locator.existingLog4jConfigMap }}
            - name: log4j-configuration
              mountPath: /bitnami/geode/config/log4j2.xml
              subPath: log4j2.xml
            {{- end }}
            {{- if .Values.auth.tls.enabled }}
            - name: geode-tls-certificates
              mountPath: /certs
              readOnly: true
            {{- end }}
          {{- if .Values.locator.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.locator.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ include "geode.metrics.image" . }}
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy }}
          {{- if .Values.metrics.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.metrics.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: haproxy-configuration
              mountPath: /bitnami/haproxy/conf/haproxy.cfg
              subPath: haproxy.cfg
          {{- if .Values.metrics.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.metrics.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - pgrep
                - haproxy
          {{- else if .Values.metrics.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.metrics.customLivenessProbe "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.metrics.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.metrics.readinessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - pgrep
                - haproxy
          {{- else if .Values.metrics.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.metrics.customReadinessProbe "context" $) | nindent 12 }}
          {{- end }}
          ports:
            - name: metrics
              containerPort: {{ .Values.metrics.containerPort }}
        {{- end }}
        {{- if .Values.locator.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.locator.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if or .Values.locator.configuration .Values.locator.existingConfigmap }}
        - name: configuration
          configMap:
            name: {{ include "geode.locator.configmapName" . }}
        {{- end }}
        {{- if or .Values.locator.log4j .Values.locator.existingLog4jConfigMap }}
        - name: log4j-configuration
          configMap:
            name: {{ include "geode.locator.log4j.configmapName" . }}
        {{ end }}
        {{- if .Values.auth.tls.enabled }}
        - name: geode-tls-certificates
          secret:
            secretName: {{ printf "%s" (tpl .Values.auth.tls.existingSecret $) }}
            defaultMode: 256
        {{- end }}
        {{- if .Values.metrics.enabled }}
        - name: haproxy-configuration
          configMap:
            name: {{ printf "%s-haproxy-conf" (include "common.names.fullname" .) }}
        {{- end }}
        {{- if .Values.locator.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.locator.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
  {{- if not .Values.locator.persistence.enabled }}
        - name: data
          emptyDir: {}
  {{- else }}
  volumeClaimTemplates:
    - metadata:
        name: data
        labels: {{- include "common.labels.matchLabels" . | nindent 10 }}
          app.kubernetes.io/component: locator
        {{- if .Values.locator.persistence.annotations }}
        annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.locator.persistence.annotations "context" $ ) | nindent 10 }}
        {{- end }}
      spec:
        accessModes:
          {{- range .Values.locator.persistence.accessModes }}
          - {{ . | quote }}
          {{- end }}
        resources:
          requests:
            storage: {{ .Values.locator.persistence.size | quote }}
        {{- include "common.storage.class" (dict "persistence" .Values.locator.persistence "global" .Values.global) | nindent 8 }}
  {{- end }}
