{{- if .Values.graphinsights.enabled }}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "cmem.name-graphinsights" . }}
  labels:
    {{- include "cmem.labels-graphinsights" . | nindent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      {{- include "cmem.selectorLabels-graphinsights" . | nindent 6 }}
  {{- if .Values.graphinsights.updateStrategy }}
  updateStrategy: {{ toYaml .Values.graphinsights.updateStrategy | nindent 4 }}
  {{- end }}
  template:
    metadata:
      annotations:
        checksum/configFiles: {{ include (print $.Template.BasePath "/graphinsights-configmap-files.yaml") . | sha256sum }}
        checksum/configEnvs: {{ include (print $.Template.BasePath "/graphinsights-configmap.yaml") . | sha256sum }}
        checksum/secretEnvs: {{ include (print $.Template.BasePath "/graphinsights-secret.yaml") . | sha256sum }}
        {{- if .Values.global.customCACerts }}
        checksum/configCustomCACert: {{ include (print $.Template.BasePath "/custom-cacert.yaml") . | sha256sum }}
        {{- end }}
      {{- with .Values.graphinsights.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "cmem.selectorLabels-graphinsights" . | nindent 8 }}
        {{- with .Values.global.additionalPodLabels }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      automountServiceAccountToken: false
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.graphinsights.podSecurityContext }}
      securityContext:
        {{- toYaml .Values.graphinsights.podSecurityContext | nindent 8 }}
      {{- end }}
      {{- if .Values.global.customCACerts }}
      initContainers:
        - name: add-custom-cacert
          image: {{ include "cmem.image" (dict "context" . "name" "graphinsights" "values" .Values.graphinsights.image) }}
          imagePullPolicy: {{ .Values.graphinsights.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 }}
          {{- if .Values.graphinsights.initContainerSecurityContext }}
          securityContext: {{- toYaml .Values.graphinsights.initContainerSecurityContext | nindent 12 }}
          {{- end }}
      {{- end }}
      containers:
        {{- with .Values.graphinsights.extraContainers }}
          {{ tpl (toYaml .) $ | nindent 8 }}
        {{- end }}
        - name: graphinsights
          image: {{ include "cmem.image" (dict "context" . "name" "graphinsights" "values" .Values.graphinsights.image) }}
          imagePullPolicy: {{ .Values.graphinsights.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.graphinsights.service.port }}
          envFrom:
          - configMapRef:
              name: {{ template "cmem.name-graphinsights" . }}
          - secretRef:
              name: {{ template "cmem.name-graphinsights" . }}
          volumeMounts:
            - name: config-files-volume
              mountPath: /opt/graphinsights/cmem.integration.config.yml
              subPath: cmem.integration.config.yml
              readOnly: true
            - name: license-volume
              mountPath: /opt/license.lic
              subPath: graphinsights.lic
              readOnly: true
            - name: cacerts
              mountPath: /cacerts
            - name: graphinsights-data
              mountPath: {{ .Values.graphinsights.persistence.mountPath | default "/data" }}
              {{- if .Values.graphinsights.persistence.subPath }}
              subPath: {{ .Values.graphinsights.persistence.subPath }}
              {{- end }}
            - name: tmp-volume
              mountPath: /tmp
          {{- if .Values.graphinsights.probes.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: {{ .Values.graphinsights.service.contextPath }}{{ .Values.graphinsights.probes.livenessProbe.path }}
              port: {{ .Values.graphinsights.service.port }}
            initialDelaySeconds: {{ .Values.graphinsights.probes.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.graphinsights.probes.livenessProbe.periodSeconds }}
            failureThreshold: {{ .Values.graphinsights.probes.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.graphinsights.probes.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: {{ .Values.graphinsights.service.contextPath }}{{ .Values.graphinsights.probes.readinessProbe.path }}
              port: {{ .Values.graphinsights.service.port }}
            initialDelaySeconds: {{ .Values.graphinsights.probes.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.graphinsights.probes.readinessProbe.periodSeconds }}
            failureThreshold: {{ .Values.graphinsights.probes.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.graphinsights.resources }}
          resources: {{ toYaml .Values.graphinsights.resources | nindent 12 }}
          {{- end }}
          {{- if .Values.graphinsights.securityContext }}
          securityContext: {{- toYaml .Values.graphinsights.securityContext | nindent 12 }}
          {{- end }}
      restartPolicy: Always
      volumes:
      - name: tmp-volume
        emptyDir:
          sizeLimit: {{ .Values.graphinsights.persistence.tempDirSize }}
      - name: graphinsights-data
      {{- if .Values.graphinsights.persistence.enabled }}
        persistentVolumeClaim:
        {{- if .Values.graphinsights.persistence.existentPvc.enabled }}
          claimName: {{ .Values.graphinsights.persistence.existentPvc.name }}
        {{- else }}
          claimName: {{ template "cmem.name-graphinsights" . }}-data
        {{- end }}
      {{- else }}
        emptyDir:
          sizeLimit: {{ .Values.graphinsights.persistence.emptyDirSize }}
      {{- end }}
      - name: license-volume
        secret:
          secretName: {{ .Values.graphinsights.license }}
      {{- 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-graphinsights" . }}-files
      {{- if .Values.graphinsights.nodeSelector }}
      nodeSelector: {{ toYaml .Values.graphinsights.nodeSelector | nindent 8 }}
      {{- end }}
      {{- if .Values.graphinsights.tolerations }}
      tolerations: {{- tpl (toYaml .Values.graphinsights.tolerations) $ | nindent 8 }}
      {{- end }}
      {{- if .Values.graphinsights.affinity }}
      affinity: {{ toYaml .Values.graphinsights.affinity | nindent 8 }}
      {{- end }}
      {{- if .Values.graphinsights.extraHosts }}
      hostAliases: {{ toYaml .Values.graphinsights.extraHosts | nindent 8 }}
      {{- end }}
{{- end }}
