{{- include "nplus.init" $ -}}
{{- if not ((.this.storage).conf).name -}}
  {{ fail "conf name must be set" }}
{{- end -}}

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .component.fullName }}
  {{- if .this.utils.includeNamespace }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  labels:
    {{- include "nplus.instanceLabels" . | nindent 4 }}
  annotations:
    {{- include "nplus.annotations" . | nindent 4 }}
    {{- include "nplus.securityAnnotations" . | nindent 4 }}

spec:
  serviceName: {{ .component.fullName }}
  selector:
    matchLabels:
      {{- include "nplus.selectorLabels" . | nindent 6 }}
  replicas: 1
  podManagementPolicy: OrderedReady
  updateStrategy:
      type: RollingUpdate
  minReadySeconds: 30

  template:
    metadata:
      labels:
        {{- include "nplus.templateLabels" . | nindent 8 }}
      annotations:
        {{- include "nplus.templateAnnotations" . | nindent 8 }}
        {{- include "nplus.securityAnnotations" . | nindent 8 }}
    spec:
      {{- include "nplus.imagePullSecrets" . | nindent 6 }}
      {{- include "nplus.podSecurityContext" . | nindent 6 }}
      {{- include "nplus.securityIllumioReadinessGates" . | nindent 6 }}
      {{- include "nplus.terminationGracePeriodSeconds" . | nindent 6 }}
      
      containers:

      - name: dav
        image: {{ include "nplus.image"  (dict "global" .Values.global "image" .Values.image) }}
        imagePullPolicy: {{ include "nplus.imagePullPolicy" .Values.image }}
        {{- include "nplus.containerSecurityContext" . | nindent 8 }}
        command: [ "/nplus/davserver" ]
        volumeMounts:
        - name: conf
          mountPath: /webdav/conf
        - name: ptemp
          mountPath: /webdav/ptemp
        {{- include "nplus.resources" . | nindent 8 }}

        env:
        # -- DAV Connection Credentials
          {{- include "nplus.envCredentials" (list
            "DAV_USER"      $.this.account
            "DAV_PASSWORD"  $.this.password
                            $.this.secret
          ) | nindent 10 }}

          - name: DAV_ROOT
            value: "/dav"

        ports:
        {{- include "nplus.defaultContainerPorts" . | nindent 8 }}       

        {{- if .Values.probes }}
        # -- custom probes as defined in values
          {{- toYaml .Values.probes | nindent 8 }}
        {{- else }}{{/* if .Values.probes */}}
        # -- default nplus probes, can be overridden by probes key in values
        readinessProbe:
          tcpSocket:
            port: {{ include "nplus.backendPort" . }}
          initialDelaySeconds: 15
          periodSeconds: 10
        livenessProbe:
          tcpSocket:
            port: {{ include "nplus.backendPort" . }}
          initialDelaySeconds: 15
          periodSeconds: 10
        {{- end }}{{/* .Values.probes else */}}

      volumes:
      - name: conf
        persistentVolumeClaim:
          claimName: conf
      - name: ptemp
        persistentVolumeClaim:
          claimName: ptemp