{{- include "nplus.init" $ -}}

{{- if not ((.this.storage).conf).name -}}
  {{ fail "conf name must be set" }}
{{- end -}}
{{- if not ((.this.storage).ptemp).name -}}
  {{ fail "ptemp 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 }}
        kubectl.kubernetes.io/default-container: toolbox
    spec:
      serviceAccountName: {{ .component.fullName }}-svc-account
      {{- include "nplus.imagePullSecrets" . | nindent 6 }}
      {{- include "nplus.podSecurityContext" . | nindent 6 }}
      {{- include "nplus.securityIllumioReadinessGates" . | nindent 6 }}
      {{- include "nplus.terminationGracePeriodSeconds" . | nindent 6 }}
      
      initContainers:
      {{/*
      - name: deploy
        image: {{ .Values.toolboxImage }}
        imagePullPolicy: {{ .Values.toolboxImagePullPolicy }}
        {{- include "nplus.containerSecurityContext" . | nindent 8 }}
        command: [ "/bin/sh", "-c" ]
        args:
        - |
            echo "deploying to /nplus"
            cp -rnxv /opt/42i/nplus/* /nplus
        volumeMounts:
        - name: nplus
          mountPath: /nplus
      {{- end }} */}}
      - name: dirprepare
        image: {{ include "nplus.image"  (dict "global" .Values.global "image" .Values.image) }}
        imagePullPolicy: {{ include "nplus.imagePullPolicy" .Values.image }}
        {{- include "nplus.containerSecurityContext" . | nindent 8 }}
        {{- include "nplus.initResources" . | nindent 8 }}
        command: [ "/bin/sh", "-c" ]
        args:
        - |
            mkdir -p /conf/pool/{apps,fonts,snippets,scripts,snc} && \
            {{- if not ((.this.storage).conf).cifs }}
            chmod 775 -R /conf/pool && \
            {{- end }}
            echo "ok."
        volumeMounts:
        - name: conf
          mountPath: /conf
      - name: gitprepare
        image: {{ include "nplus.image"  (dict "global" .Values.global "image" .Values.image) }}
        imagePullPolicy: {{ include "nplus.imagePullPolicy" .Values.image }}
        {{- include "nplus.containerSecurityContext" . | nindent 8 }}
        {{- include "nplus.initResources" . | nindent 8 }}
        command: [ "/bin/sh", "-c" ]
        args:
        - |
            echo "checking git in /conf..."
            if [ ! -f "/conf/.gitignore" ]; then
            echo "writing .gitignore"
            echo "/{{ .Values.nstoreDownloader.target }}/*" > /conf/.gitignore
            fi
            if [ ! -d "/conf/.git" ]; then
            echo "init git in /conf with branch master"
            git -C "/conf" init -b master
            git -C "/conf" add .gitignore
            echo "first commit (with .gitignore)"
            git -C "/conf" commit -m "Initial commit for config of nplus environment {{ .Release.Namespace }}"
            fi
            echo "ok."
        volumeMounts:
        - name: conf
          mountPath: /conf
      containers:
      - name: toolbox
        image: {{ include "nplus.image"  (dict "global" .Values.global "image" .Values.image) }}
        imagePullPolicy: {{ include "nplus.imagePullPolicy" .Values.image }}
        {{- include "nplus.containerSecurityContext" . | nindent 8 }}
        command: [ "/bin/bash", "-c", "--" ]
        args: [ "while true; do sleep 30; done;" ]

        volumeMounts:
        - name: conf
          mountPath: /conf
        - name: ptemp
          mountPath: /ptemp
        {{- include "nplus.resources" . | nindent 8 }}

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

