{{- include "nplus.init" $ -}}
# Component: {{ .component.chartName }}
#
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 }}
  {{- include "nplus.replicas" . | nindent 2 }}
  selector:
    matchLabels:
      {{- include "nplus.selectorLabels" . | nindent 6 }}
  podManagementPolicy: OrderedReady
  updateStrategy:
      type: OnDelete
  minReadySeconds: 10

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

      initContainers:
      # Copy Config currently disabled, because the official image has no config dir
      # include "nplus.copyConfig" . | nindent 6
      {{- include "nplus.initContainers" . | nindent 6 }}

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

        # -- POSTGRES Admin Credentials
          {{- include "nplus.envCredentials" (list
            "POSTGRES_USER"       ($.this.dbAdmin).account
            "POSTGRES_PASSWORD"   ($.this.dbAdmin).password
                                  ($.this.dbAdmin).secret
          ) | nindent 10 }}

        # -- NAPPL Postgres Connection Credentials
          {{- include "nplus.envCredentials" (list
            "NSCALE_USERNAME"   ($.this.database).account
            "NSCALE_PASSWORD"   ($.this.database).password
                                ($.this.database).secret
          ) | nindent 10 }}

          - name: NSCALE_DATABASE
            value: {{ (.this.database).name }}

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

        ports:
        {{- include "nplus.defaultContainerPorts" . | 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
        readinessProbe:
          exec:
            command: ["bash", "-c", "psql -w -U $NSCALE_USERNAME -d $NSCALE_DATABASE SELECT 1"]
          initialDelaySeconds: 15
          timeoutSeconds: 2
        livenessProbe:
          exec:
            command: ["bash", "-c", "psql -w -U $NSCALE_USERNAME -d $NSCALE_DATABASE SELECT 1"]
          initialDelaySeconds: 30
          timeoutSeconds: 2
        {{- end }}{{/* .Values.probes else */}}
        {{- end }}{{/* if .this.utils.maintenance else */}}
        volumeMounts:
        # Postgres requires this directory to be **completely** empty,
        # so also no lost&found directory.
        {{- include "nplus.defaultMounts" . | nindent 8 }}
        - name: custom-init-scripts
          mountPath: /docker-entrypoint-initdb.d/
      volumes:
      - name: custom-init-scripts
        configMap:
          name: {{ .component.fullName }}-config
      {{- include "nplus.defaultVolumes" . | nindent 6 }}
