apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "fuellhorn.fullname" . }}
  labels:
    {{- include "fuellhorn.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "fuellhorn.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "fuellhorn.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if or .Values.initContainers.migrations.enabled .Values.initContainers.adminUser.enabled }}
      initContainers:
        {{- if .Values.initContainers.migrations.enabled }}
        - name: migrate
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["alembic", "upgrade", "head"]
          env:
            - name: DB_TYPE
              value: {{ .Values.database.type | quote }}
            {{- if eq .Values.database.type "postgresql" }}
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.database.external.existingSecret | default (printf "%s-db" (include "fuellhorn.fullname" .)) }}
                  key: url
            {{- end }}
          {{- if eq .Values.database.type "sqlite" }}
          volumeMounts:
            - name: data
              mountPath: /app/data
          {{- end }}
        {{- end }}
        {{- if .Values.initContainers.adminUser.enabled }}
        - name: create-admin
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["python", "create_admin.py"]
          env:
            - name: DB_TYPE
              value: {{ .Values.database.type | quote }}
            {{- if eq .Values.database.type "postgresql" }}
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.database.external.existingSecret | default (printf "%s-db" (include "fuellhorn.fullname" .)) }}
                  key: url
            {{- end }}
            - name: ADMIN_USERNAME
              value: {{ .Values.initContainers.adminUser.username | quote }}
            - name: ADMIN_EMAIL
              value: {{ .Values.initContainers.adminUser.email | quote }}
            - name: ADMIN_PASSWORD
              {{- if .Values.initContainers.adminUser.existingSecret }}
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.initContainers.adminUser.existingSecret }}
                  key: admin-password
              {{- else }}
              value: {{ .Values.initContainers.adminUser.password | quote }}
              {{- end }}
          {{- if eq .Values.database.type "sqlite" }}
          volumeMounts:
            - name: data
              mountPath: /app/data
          {{- end }}
        {{- end }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          env:
            - name: DB_TYPE
              value: {{ .Values.database.type | quote }}
            {{- if eq .Values.database.type "postgresql" }}
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.database.external.existingSecret | default (printf "%s-db" (include "fuellhorn.fullname" .)) }}
                  key: url
            {{- end }}
            - name: SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.secrets.existingSecret | default (printf "%s-secrets" (include "fuellhorn.fullname" .)) }}
                  key: secret-key
            - name: FUELLHORN_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.secrets.existingSecret | default (printf "%s-secrets" (include "fuellhorn.fullname" .)) }}
                  key: fuellhorn-secret
          livenessProbe:
            {{- toYaml .Values.livenessProbe | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.readinessProbe | nindent 12 }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- if eq .Values.database.type "sqlite" }}
          volumeMounts:
            - name: data
              mountPath: /app/data
          {{- end }}
      {{- if eq .Values.database.type "sqlite" }}
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: {{ include "fuellhorn.fullname" . }}-data
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
