{{- $fullName := include "common.names.fullname" . -}}
apiVersion: {{ include "common.capabilities.statefulset.apiVersion" . }}
kind: StatefulSet
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- 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:
  serviceName: {{ include "common.names.fullname" .  }}
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/configMap: {{ toYaml .Values.config | sha256sum }}
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      containers:
      - name: ephemerup
        image: {{ include "ephemerup.image" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy | quote}}
        command: ['/app/ephemerupd', '-c', '/config/ephemerup.hcl']
        env:
          {{- range $envVar := .Values.env }}
          - name: {{ $envVar.name }}
            value: {{ $envVar.value }}
          {{- end }}
        ports:
          - name: http
            containerPort: {{ .Values.service.ports.http }}
            protocol: TCP
        {{- if .Values.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            port: {{ .Values.service.ports.http }}
            path: /status
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.livenessProbe.successThreshold }}
          failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
        {{- else if .Values.customLivenessProbe }}
        livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
        {{- end }}
        {{- if .Values.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            port: {{ .Values.service.ports.http }}
            path: /status
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.readinessProbe.successThreshold }}
          failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
        {{- else if .Values.customReadinessProbe }}
        readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
        {{- end }}
        {{- if .Values.startupProbe.enabled }}
        startupProbe:
          tcpSocket:
            port: {{ .Values.service.ports.http }}
          initialDelaySeconds: {{ .Values.startupProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.startupProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.startupProbe.timeoutSeconds }}
          successThreshold: {{ .Values.startupProbe.successThreshold }}
          failureThreshold: {{ .Values.startupProbe.failureThreshold }}
        {{- else if .Values.customStartupProbe }}
        startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
        {{- end }}
        {{- if .Values.resources }}
        resources: {{- toYaml .Values.resources | nindent 12 }}
        {{- end }}
        {{- if .Values.containerSecurityContext.enabled }}
        securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | nindent 10 }}
        {{- end }}
        volumeMounts:
        - name: "ephemerup-storage"
          mountPath: "/data"
        - name: "ephemerup-tmp"
          mountPath: "/tmp"
        - mountPath: "/config/ephemerup.hcl"
          name: config
          subPath: "ephemerup.hcl"
        {{- range $secret := .Values.mountSecrets }}
        - mountPath: "/secret/{{ $secret.name }}"
          name: {{ $secret.name }}
        {{- end }}

      securityContext:
        {{ toYaml .Values.podSecurityContext | nindent 8 | trim }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      volumes:
      - name: config
        configMap:
          name: {{ template "common.names.fullname" . }}-config
          items:
          - key: ephemerup.hcl
            path: ephemerup.hcl
      {{- range $secret := .Values.mountSecrets }}
      - name: {{ $secret.name }}
        secret:
          secretName: {{ $secret.name }}
      {{- end }}
  volumeClaimTemplates:
  - metadata:
      name: {{ .Values.storage.longTerm.name }}
    spec:
      {{ toYaml .Values.storage.longTerm.spec | nindent 6 | trim }}
  - metadata:
      name: {{ .Values.storage.tmp.name }}
    spec:
      {{ toYaml .Values.storage.tmp.spec | nindent 6 | trim }}
