{{- 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 }}{{/* if ($.this.nappl).ssl - */}}
#   defined by config file in conf PV.
{{- 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.argoWave" . | nindent 4 }}
    {{- include "nplus.annotations" . | nindent 4 }}
    {{- include "nplus.securityAnnotations" . | nindent 4 }}

spec:
  serviceName: {{ .component.fullName }}
  selector:
    matchLabels:
      {{- include "nplus.selectorLabels" . | nindent 6 }}
  {{- include "nplus.replicas" . | nindent 2 }}
  podManagementPolicy: OrderedReady
  updateStrategy:
    type: {{ .Values.updateStrategy | default "OnDelete" }}  
  minReadySeconds: 5

  template:
    metadata:
      labels:
        {{- include "nplus.templateLabels" . | nindent 8 }}
      annotations:
        {{- include "nplus.templateAnnotations" . | nindent 8 }}
        {{- include "nplus.securityAnnotations" . | nindent 8 }}
    spec:
      {{- include "nplus.priorityClassName" . | nindent 6 }}
      {{- include "nplus.imagePullSecrets" . | nindent 6 }}
      {{- include "nplus.securityIllumioReadinessGates" . | nindent 6 }}
      {{- include "nplus.podSecurityContext" . | nindent 6 }}
      {{- include "nplus.terminationGracePeriodSeconds" . | nindent 6 }}
            
      initContainers:
      {{- include "nplus.waitFor" . | nindent 6 }}
      {{- include "nplus.initContainers" . | nindent 6 }}
      
      containers:
      - name: administrator
        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: APPLICATION_LAYER_HOST
            value: {{ ($.this.nappl).host | quote }}
          {{- end }}
          {{- if ($.this.nappl).port }}
          - name: APPLICATION_LAYER_PORT
            value: {{ ($.this.nappl).port | quote }}
          {{- end }}
          {{- if ($.this.nappl).ssl }}
          - name: APPLICATION_LAYER_SSL
            value: {{ ($.this.nappl).ssl | quote }}
          {{- end }}
          {{- if ($.this.nappl).instance }}
          - name: APPLICATION_LAYER_INSTANCE
            value: {{ ($.this.nappl).instance | quote }}
          {{- end }}

        {{- include "nplus.environment" . | nindent 8 }}
        
        {{- if .this.utils.maintenance }}
          {{- include "nplus.idle" . | nindent 8 }}
        {{- else }}{{/* if .this.utils.maintenance */}}
        {{- 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
        startupProbe:
          httpGet:
            path: /rapadm/
            port: {{ include "nplus.backendPort" . }}
            scheme: {{ include "nplus.backendProtocol" . | upper }}
          initialDelaySeconds: 10
          failureThreshold: 12
          periodSeconds: 10
          timeoutSeconds: 5
        livenessProbe:
          httpGet:
            path: /rapadm/
            port: {{ include "nplus.backendPort" . }}
            scheme: {{ include "nplus.backendProtocol" . | upper }}
          periodSeconds: 30
          timeoutSeconds: 1
        readinessProbe:
          httpGet:
            path: /rapadm/
            port: {{ include "nplus.backendPort" . }}
            scheme: {{ include "nplus.backendProtocol" . | upper }}
          periodSeconds: 10
          timeoutSeconds: 1
        {{- end }}{{/* .Values.probes else */}}
        {{- end }}{{/* if .this.utils.maintenance else */}}

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

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

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

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