{{- include "nplus.init" $ -}}
# Component: {{ .component.chartName }}
# will connect to:
{{- if (.this.nappl).host }}
#   nappl: {{ if ($.this.nappl).ssl -}}https{{- else -}}http{{- end -}}://{{ ($.this.nappl).host }}:{{ (.this.nappl).port }}/{{ (.this.nappl).instance }}
{{- else }}
#   defined by config file in conf PV.
{{- end }}
#
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .component.fullName }}
  {{- if .this.utils.includeNamespace }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  labels:
    {{- include "nplus.instanceLabels" . | nindent 4 }}
  annotations:
    {{- include "nplus.argoWave" . | nindent 4 }}
    {{- include "nplus.annotations" . | nindent 4 }}
    {{- include "nplus.securityAnnotations" . | nindent 4 }}

spec:
  selector:
    matchLabels:
      {{- include "nplus.selectorLabels" . | nindent 6 }}
  replicas: {{ .Values.replicaCount }}
  strategy:
    type: RollingUpdate

  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.securityIllumioReadinessGates" . | nindent 6 }}
      {{- include "nplus.podSecurityContext" . | nindent 6 }}
      {{- include "nplus.templateAffinity" . | nindent 6 }}
      {{- include "nplus.terminationGracePeriodSeconds" . | nindent 6 }}
      
      initContainers:
      {{- include "nplus.waitFor" . | nindent 6 }}
      {{- include "nplus.copyConfig" . | nindent 6 }}

      containers:
      - name: webdav-connector
        image: {{ include "nplus.image"  (dict "global" .Values.global "image" .Values.image) }}
        imagePullPolicy: {{ include "nplus.imagePullPolicy" .Values.image }}
        {{- include "nplus.containerSecurityContext" . | nindent 8 }}
        env:

          {{- if ($.this.nappl).host }}
          - name: WEBDAV_AL_HOST
            value: {{ ($.this.nappl).host | quote }}
          {{- end }}
          {{- if ($.this.nappl).port }}
          - name: WEBDAV_AL_PORT
            value: {{ ($.this.nappl).port | quote }}
          {{- end }}
          {{- if ($.this.nappl).ssl }}
          - name: WEBDAV_AL_SSL
            value: {{ ($.this.nappl).ssl | quote }}
          {{- end }}
          {{- if ($.this.nappl).instance }}
          - name: WEBDAV_AL_INSTANCE
            value: {{ ($.this.nappl).instance | quote }}
          {{- end }}

        {{- include "nplus.environment" . | nindent 8 }}

        {{- if .this.utils.maintenance }}
          {{- include "nplus.idle" . | nindent 8 }}
        {{- else }}
# TODO Das hier funktioniert nicht. Es kommt eine 401 zurück. Das ist gut, wird aber als Fehler gesehen.
# Die Ceyoniq hat hier auch noch die Probe auskommentiert: 
# https://github.com/ceyoniq/container/blob/main/kubernetes/kustomize/nscale/base/webdav-connector.yaml
#         readinessProbe:
#           httpGet:
#             path: /dav
#             port: 8088
#           initialDelaySeconds: 20
#           periodSeconds: 10
        # -- Ceyoniq does currently not define an *official* startupProbe, so we use
        # one that quickly checks the main socket on Layer 4.
        startupProbe:
          initialDelaySeconds: 10
          failureThreshold: 30
          periodSeconds: 10
          timeoutSeconds: 5
          tcpSocket:
            port: {{ include "nplus.backendPort" . }}
        # -- Ceyoniq does currently not define an *official* readynessProbe, so we use
        # one that quickly checks the main socket on Layer 4.
        readinessProbe:
          tcpSocket:
            port: {{ include "nplus.backendPort" . }}
          # initialDelaySeconds: 10
          periodSeconds: 10
        # -- Ceyoniq does currently not define an *official* livenessProbe, so we use
        # one that quickly checks the main socket on Layer 4.
        livenessProbe:
          tcpSocket:
            port: {{ include "nplus.backendPort" . }}
          # initialDelaySeconds: 10
          periodSeconds: 10
        {{- end }}

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

        {{- include "nplus.resources" . | nindent 8 }}

        volumeMounts:
        {{- include "nplus.defaultMounts" . | nindent 8 }}

      volumes:
      {{- include "nplus.defaultVolumes" . | nindent 6 }}

