{{ include "common.storage.hostPathValidate" .Values }}
{{ $elastic_search := (. | mustDeepCopy) }}
{{ $_ := set $elastic_search "common" (dict "nameSuffix" "elasticsearch") }}

apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "common.names.fullname" . }}
  labels:
    app: {{ template "common.names.name" . }}
    chart: {{ template "common.names.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  annotations:
    rollme: {{ randAlphaNum 5 | quote }}
spec:
  replicas: {{ (default 1 .Values.replicas) }}
  strategy:
    type: "Recreate"
  selector:
    matchLabels:
      app: {{ template "common.names.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      name: {{ template "common.names.fullname" . }}
      labels:
        app: {{ template "common.names.name" . }}
        release: {{ .Release.Name }}
        {{- include "common.labels.selectorLabels" . | nindent 8 }}
      annotations: {{ include "common.annotations" . | nindent 8 }}
    spec:
      initContainers:
        - name: init-config
          {{ include "common.containers.imageConfig" .Values.python.image | nindent 10 }}
          command: ["python3", "/init_scripts/init_config.py"]
          env:
            {{ $envList := (default list .Values.environmentVariables) }}
            {{ $envList = mustAppend $envList (dict "name" "TZ" "value" .Values.timezone) }}
            {{ $envList = mustAppend $envList (dict "name" "DS_USER" "valueFromSecret" true "secretName" "diskover-credentials" "secretKey" "username") }}
            {{ $envList = mustAppend $envList (dict "name" "DS_PASS" "valueFromSecret" true "secretName" "diskover-credentials" "secretKey" "password") }}
            {{ include "common.containers.environmentVariables" (dict "environmentVariables" $envList) | nindent 12 }}
            {{ include "elasticsearch.IP" $elastic_search | nindent 12 }}
            {{ include "elasticsearch.credentials" . | nindent 12  }}
            {{ $configPath := (dict "mountPath" (printf "%s/diskover-web.conf.d/" .Values.appVolumeMounts.config.mountPath) "configFile" "Constants.php") }}
            {{ include "config.file.path" $configPath | nindent 12 }}

          volumeMounts: {{ include "common.storage.configureAppVolumeMountsInContainer" .Values | nindent 12 }}
            - name: diskover-initial-scripts
              mountPath: /init_scripts/
        - name: wait-es-search
          {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          env:
            {{ include "elasticsearch.IP" $elastic_search | nindent 12 }}
          command: ["python3", "/init_scripts/wait_for_elastic_search.py"]

          volumeMounts:
            - name: diskover-initial-scripts
              mountPath: /init_scripts/
        - name: init-es-config
          {{ include "common.containers.imageConfig" .Values.python.image | nindent 10 }}
          command: ["python3", "/init_scripts/initial_es_config.py"]
          env:
            {{ $envListConfig := (default list .Values.environmentVariables) }}
            {{ include "elasticsearch.IP" $elastic_search | nindent 12 }}
            {{ include "elasticsearch.credentials" . | nindent 12  }}
            {{ $configPathES := (dict "mountPath" (printf "%s/diskover.conf.d/diskover/" .Values.appVolumeMounts.config.mountPath) "configFile" "config.yaml") }}
            {{ include "config.file.path" $configPathES | nindent 12 }}
            {{ include "common.containers.environmentVariables" (dict "environmentVariables" $envListConfig) | nindent 12 }}

          volumeMounts: {{ include "common.storage.configureAppVolumeMountsInContainer" .Values | nindent 12 }}
            - name: diskover-initial-scripts
              mountPath: /init_scripts/

      containers:
        - name: {{ .Chart.Name }}
          {{ include "common.resources.limitation" . | nindent 10 }}
          {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          volumeMounts: {{ include "common.storage.configureAppVolumeMountsInContainer" .Values | nindent 12 }}
            - name: diskover-initial-scripts
              mountPath: /init_scripts/
          {{ range $index, $hostPathConfiguration := .Values.extraAppVolumeMounts }}
            - name: extrappvolume-{{ $index }}
              mountPath: {{ $hostPathConfiguration.mountPath }}
          {{ end }}
          {{ range $index, $hostPathConfiguration := .Values.extraDataVolumeMounts }}
            - name: extradatavolume-{{ $index }}
              mountPath: {{ $hostPathConfiguration.mountPath }}
          {{ end }}

          ports:
            - name: web
              containerPort: 80
          {{ $cronjobSchedule := .Values.cronjobSchedule }}
          lifecycle:
            postStart:
              exec:
                command:
                  - /bin/sh
                  - -c
                  - |
                   ./init_scripts/.default_crawler.sh /app/diskover/diskover.py /data;
                {{ range $index, $hostPathConfiguration := .Values.extraDataVolumeMounts }}
                   ./init_scripts/.default_crawler.sh /app/diskover/diskover.py {{ $hostPathConfiguration.mountPath }};
                {{ end }}
                {{ range $index, $hostPathConfiguration := .Values.extraDataVolumeMounts }}
                   echo "{{$cronjobSchedule}} python3 /app/diskover/diskover.py {{ $hostPathConfiguration.mountPath }}" >> /config/crontab;
                {{ end }}
                   echo "{{.Values.cronjobSchedule}} python3 /app/diskover/diskover.py /data" >> /config/crontab;
                   crontab /config/crontab;
          env:
            {{ $envListDiskover := (default list .Values.environmentVariables) }}
            {{ $envListDiskover = mustAppend $envListDiskover (dict "name" "PUID" "value" .Values.ownerUID) }}
            {{ $envListDiskover = mustAppend $envListDiskover (dict "name" "PGID" "value" .Values.ownerGID) }}
            {{ include "common.containers.environmentVariables" (dict "environmentVariables" $envListDiskover) | nindent 12 }}
          livenessProbe:
            httpGet:
              path: /login.php
              port: 80
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 5
            successThreshold: 1
          readinessProbe:
            httpGet:
              path: /login.php
              port: 80
            initialDelaySeconds: 10
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 5
            successThreshold: 2
          startupProbe:
            httpGet:
              path: /login.php
              port: 80
            initialDelaySeconds: 10
            periodSeconds: 5
            timeoutSeconds: 2
            failureThreshold: 60
            successThreshold: 1

{{ include "common.networking.dnsConfiguration" .Values | nindent 6 }}
      volumes: {{ include "common.storage.configureAppVolumes" .Values | nindent 8 }}
      {{ range $index, $hostPathConfiguration := .Values.extraAppVolumeMounts }}
        - name: extrappvolume-{{ $index }}
          hostPath:
            path: {{ $hostPathConfiguration.hostPath }}
      {{ end }}
      {{ range $index, $hostPathConfiguration := .Values.extraDataVolumeMounts }}
        - name: extradatavolume-{{ $index }}
          hostPath:
            path: {{ $hostPathConfiguration.hostPath }}
      {{ end }}

        - name: diskover-initial-scripts
          configMap:
            defaultMode: 0700
            name: "diskover-initial-scripts"
