{{- if .Values.explore.enabled }}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "cmem.name-explore" . }}
  labels:
    {{- include "cmem.labels-explore" . | nindent 4 }}
spec:
  {{- if not .Values.explore.autoscaling.enabled }}
  replicas: {{ .Values.explore.replicas }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "cmem.selectorLabels-explore" . | nindent 6 }}
  updateStrategy: {{ toYaml .Values.explore.updateStrategy | nindent 4 }}
  template:
    metadata:
      annotations:
        checksum/configFiles: {{ include (print $.Template.BasePath "/explore-configmap-files.yaml") . | sha256sum }}
        checksum/configEnvs: {{ include (print $.Template.BasePath "/explore-configmap.yaml") . | sha256sum }}
        checksum/secretEnvs: {{ include (print $.Template.BasePath "/explore-secret.yaml") . | sha256sum }}
        {{- if .Values.global.customCACerts }}
        checksum/configCustomCACert: {{ include (print $.Template.BasePath "/custom-cacert.yaml") . | sha256sum }}
        {{- end }}
      {{- with .Values.explore.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "cmem.selectorLabels-explore" . | nindent 8 }}
        {{- with .Values.global.additionalPodLabels }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      automountServiceAccountToken: false
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.explore.podSecurityContext | nindent 8 }}
      {{- if .Values.global.customCACerts }}
      initContainers:
        - name: add-custom-cacert
          image: {{ include "cmem.image" (dict "context" . "name" "explore" "values" .Values.explore.image) }}
          # image: {{ .Values.explore.image.repository }}:{{ .Values.explore.image.tag }}
          imagePullPolicy: {{ .Values.explore.image.pullPolicy }}
          command:
            - bash
            - -c
            - |
              set -x
              id
              ls -laF /cacerts /custom-cacert
              {{- range $i, $cert := .Values.global.customCACerts }}
              cp /custom-cacert/{{ $i }}.crt /cacerts/{{ $i }}.crt 
              {{- end }}
              ls -laF /cacerts /custom-cacert
          volumeMounts:
            - name: custom-cacert
              mountPath: /custom-cacert
            - name: cacerts
              mountPath: /cacerts
          {{- if .Values.global.initContainerResources }}
          resources: {{ toYaml .Values.global.initContainerResources | nindent 12 }}
          {{- end }}
          {{- with .Values.explore.initContainerSecurityContext }}
          securityContext: {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- end }}
      containers:
        {{- with .Values.explore.extraContainers }}
          {{ tpl (toYaml .) $ | nindent 8 }}
        {{- end }}
        - name: explore
          image: {{ include "cmem.image" (dict "context" . "name" "explore" "values" .Values.explore.image) }}
          imagePullPolicy: {{ .Values.explore.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.explore.service.port }}
            {{- if .Values.explore.config.metrics.enabled }}
            - name: metrics
              containerPort: {{ .Values.explore.config.metrics.port }}
            {{- end }}
          envFrom:
          - configMapRef:
              name: {{ template "cmem.name-explore" . }}
          - secretRef:
              name: {{ template "cmem.name-explore" . }}
          volumeMounts:
            - name: config-files-volume
              mountPath: /data
              readOnly: true
            {{- if .Values.global.license }}
            - name: license-volume
              mountPath: /license
              readOnly: true
            {{- end }}
            {{- if .Values.explore.cache.enabled }}
            - name: javatemp
              mountPath: {{ .Values.explore.cache.javaTempPath }}
            {{- end }}
            - name: cacerts
              mountPath: /cacerts
            {{- if .Values.explore.companion.enabled }}
            - name: companion-data
              mountPath: {{ .Values.explore.companion.persistence.mountPath }}
              {{- if .Values.explore.companion.persistence.subPath }}
              subPath: {{ .Values.explore.companion.persistence.subPath }}
              {{- end }}
            {{- end }}
            {{- if .Values.graphinsights.enabled }}
            - name: graphinsights-data
              mountPath: {{ .Values.graphinsights.explore.persistence.mountPath }}
              {{- if .Values.graphinsights.explore.persistence.subPath }}
              subPath: {{ .Values.graphinsights.explore.persistence.subPath }}
              {{- end }}
            {{- end }}
          livenessProbe:
            httpGet:
              path: {{ .Values.explore.config.contextPath }}{{ .Values.explore.probes.livenessProbe.path }}
              port: {{ .Values.explore.service.port }}
            initialDelaySeconds: {{ .Values.explore.probes.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.explore.probes.livenessProbe.periodSeconds }}
            failureThreshold: {{ .Values.explore.probes.livenessProbe.failureThreshold }}
          readinessProbe:
            httpGet:
              path: {{ .Values.explore.config.contextPath }}{{ .Values.explore.probes.readinessProbe.path }}
              port: {{ .Values.explore.service.port }}
            initialDelaySeconds: {{ .Values.explore.probes.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.explore.probes.readinessProbe.periodSeconds }}
            failureThreshold: {{ .Values.explore.probes.readinessProbe.failureThreshold }}
          {{- if .Values.explore.resources }}
          resources: {{ toYaml .Values.explore.resources | nindent 12 }}
          {{- end }}
          {{- with .Values.explore.securityContext }}
          securityContext: {{- toYaml . | nindent 12 }}
          {{- end }}
      restartPolicy: Always
      volumes:
      {{- if and .Values.explore.companion.enabled }}
      - name: companion-data
        {{- if .Values.explore.companion.persistence.enabled }}
        persistentVolumeClaim:
        {{- if .Values.explore.companion.persistence.existentPvc.enabled }}
          claimName: {{ .Values.companion.persistence.existentPvc.name }}
        {{- else }}
          claimName: {{ template "cmem.name-explore" . }}-companion
        {{- end }}
        {{- else }}
        emptyDir:
          sizeLimit: {{ .Values.explore.companion.persistence.emptyDirSize }}
        {{- end }}
      {{- end }}
      {{- if and .Values.graphinsights.enabled }}
      - name: graphinsights-data
        {{- if .Values.graphinsights.explore.persistence.enabled }}
        persistentVolumeClaim:
        {{- if .Values.graphinsights.explore.persistence.existentPvc.enabled }}
          claimName: {{ .Values.graphinsights.explore.persistence.existentPvc.name }}
        {{- else }}
          claimName: {{ template "cmem.name-explore" . }}-graphinsights
        {{- end }}
        {{- else }}
        emptyDir:
          sizeLimit: {{ .Values.graphinsights.explore.persistence.emptyDirSize }}
        {{- end }}
      {{- end }}
      {{- if .Values.explore.cache.enabled }}
      - name: javatemp
        emptyDir:
          sizeLimit: {{ .Values.explore.cache.cacheSize }}
      {{- end }}
      {{- if .Values.global.license }}
      - name: license-volume
        secret:
          secretName: {{ .Values.global.license }}
      {{- end }}
      {{- if .Values.global.customCACerts }}
      - name: custom-cacert
        configMap:
          name: {{ template "cmem.name" . }}-custom-cacert
      {{- end }}
      - name: cacerts
        emptyDir: {}
      - name: config-files-volume
        configMap:
          name: {{ template "cmem.name-explore" . }}-files
      {{- if .Values.explore.nodeSelector }}
      nodeSelector: {{ toYaml .Values.explore.nodeSelector | nindent 8 }}
      {{- end }}
      {{- with .Values.explore.tolerations }}
      tolerations: {{- tpl (toYaml .) $ | nindent 8 }}
      {{- end }}
      {{- if .Values.explore.affinity }}
      affinity: {{ toYaml .Values.explore.affinity | nindent 8 }}
      {{- end }}
      {{- if .Values.explore.extraHosts }}
      hostAliases: {{ toYaml .Values.explore.extraHosts | nindent 8 }}
      {{- end }}
{{- end }}
