{{- if include "nextcloud.host" . -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "nextcloud.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "nextcloud.name" . }}
    helm.sh/chart: {{ include "nextcloud.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "nextcloud.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "nextcloud.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end}}
      {{- end }}
      containers:
      - name: {{ .Chart.Name }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        env:
        {{- if .Values.internalDatabase.enabled }}
        - name: SQLITE_DATABASE
          value: {{ .Values.internalDatabase.name | quote }}
        {{- else if .Values.mariadb.enabled }}
        - name: MYSQL_HOST
          value: {{ template "nextcloud.mariadb.fullname" . }}
        - name: MYSQL_DATABASE
          value: {{ .Values.mariadb.db.name | quote }}
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: {{ printf "%s-%s" .Release.Name "db"  }}
              key: db-username
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ printf "%s-%s" .Release.Name "db"  }}
              key: db-password
        {{- else }}
        - name: MYSQL_HOST
          value: {{ .Values.externalDatabase.host | quote }}
        - name: MYSQL_DATABASE
          value: {{ .Values.externalDatabase.database | quote }}
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: {{ printf "%s-%s" .Release.Name "db" }}
              key: db-username
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ printf "%s-%s" .Release.Name "db" }}
              key: db-password
        {{- end }}
        - name: NEXTCLOUD_ADMIN_USER
          valueFrom:
            secretKeyRef:
              name: {{ template "nextcloud.fullname" . }}
              key: nextcloud-username
        - name: NEXTCLOUD_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: {{ template "nextcloud.fullname" . }}
              key: nextcloud-password
        - name: NEXTCLOUD_TRUSTED_DOMAINS
          value: {{ .Values.nextcloud.host }}
        ports:
        - name: http
          containerPort: 80
          protocol: TCP
        livenessProbe:
          httpGet:
            path: /status.php
            port: http
            httpHeaders:
            - name: Host
              value: {{ include "nextcloud.host" . | quote }}
          initialDelaySeconds: 30
          timeoutSeconds: 5
          failureThreshold: 6
        readinessProbe:
          httpGet:
            path: /status.php
            port: http
            httpHeaders:
            - name: Host
              value: {{ include "nextcloud.host" . | quote }}
          initialDelaySeconds: 30
          timeoutSeconds: 3
          periodSeconds: 5
        resources:
{{ toYaml .Values.resources | indent 10 }}
        volumeMounts:
        - name: nextcloud-data
          mountPath: /var/www/html/
          subPath: root
        - name: nextcloud-data
          mountPath: /var/www/html/data
          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/html/themes
          subPath: themes
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      volumes:
      - name: nextcloud-data
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ if .Values.persistence.existingClaim }}{{ .Values.persistence.existingClaim }}{{- else }}{{ template "nextcloud.fullname" . }}-nextcloud{{- end }}
      {{- else }}
        emptyDir: {}
      {{- end }}
{{- end -}}
