{{ $postgres_values := (. | mustDeepCopy) }}
{{ $_ := set $postgres_values "common" (dict "nameSuffix" "postgres") }}
{{ include "common.deployment.common_config" . | nindent 0 }}
spec: {{ include "common.deployment.common_spec" . | nindent 2 }}
  template: {{ include "common.deployment.pod.metadata" . | nindent 4 }}
    spec:
      initContainers:
        - name: init-postgresdb
          image: {{ template "postgres.imageName" . }}
          command: ['sh', '-c', "until pg_isready -h {{ template "common.names.fullname" $postgres_values }}; do echo waiting for postgres; sleep 2; done"]
          imagePullPolicy: {{ .Values.image.pullPolicy }}
      containers:
      - name: {{ .Chart.Name }}
        {{ include "common.containers.imageConfig" .Values.image | nindent 8 }}
        env: {{ include "postgres.envVariableConfiguration" $postgres_values | nindent 10 }}
        {{ $envList := list }}
        {{ $secretName := (include "common.names.fullname" .) }}
        {{ $envList = mustAppend $envList (dict "name" "POSTGRES_HOST" "value" (printf "%s:5432" (include "common.names.fullname" $postgres_values))) }}
        {{ $envList = mustAppend $envList (dict "name" "POSTGRES_DB" "value" (include "postgres.DatabaseName" .)) }}
        {{ $envList = mustAppend $envList (dict "name" "NEXTCLOUD_DATA_DIR" "value" .Values.nextcloud.datadir) }}
        {{ $envList = mustAppend $envList (dict "name" "NEXTCLOUD_TRUSTED_DOMAINS" "value" .Values.nextcloud.host) }}
        {{ $envList = mustAppend $envList (dict "name" "NEXTCLOUD_ADMIN_USER" "valueFromSecret" true "secretName" $secretName "secretKey" "nextcloud-username") }}
        {{ $envList = mustAppend $envList (dict "name" "NEXTCLOUD_ADMIN_PASSWORD" "valueFromSecret" true "secretName" $secretName "secretKey" "nextcloud-password") }}
        {{ include "common.containers.environmentVariables" (dict "environmentVariables" $envList) | nindent 10 }}
        ports:
        - name: http
          containerPort: 80
          protocol: TCP
        volumeMounts:
        - name: nextcloud-data
          mountPath: /var/www/
          subPath: "root"
        - name: nextcloud-data
          mountPath: /var/www/html
          subPath: "html"
        - name: nextcloud-data
          mountPath: {{ .Values.nextcloud.datadir }}
          subPath: "data"
        - name: nextcloud-data
          mountPath: /var/www/html/config
          subPath: "config"
        - name: nextcloud-data
          mountPath: /var/www/html/custom_apps
          subPath: "custom_apps"
        - name: nextcloud-data
          mountPath: /var/www/tmp
          subPath: "tmp"
        - name: nextcloud-data
          mountPath: /var/www/html/themes
          subPath: "themes"
        {{ range $index, $hostPathConfiguration := .Values.extraAppVolumeMounts }}
        - name: extrappvolume-{{ $index }}
          mountPath: {{ $hostPathConfiguration.mountPath }}
        {{ end }}
{{ include "common.networking.dnsConfiguration" .Values | nindent 6 }}
{{ include "common.storage.allAppVolumes" .Values | nindent 6 }}
      {{ range $index, $hostPathConfiguration := .Values.extraAppVolumeMounts }}
        - name: extrappvolume-{{ $index }}
          hostPath:
            path: {{ $hostPathConfiguration.hostPath }}
      {{ end }}
      # Will mount configuration files as www-data (id: 33) for nextcloud
      securityContext:
        fsGroup: 33
