apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "trek.fullname" . }}
  labels:
    app: {{ include "trek.name" . }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ include "trek.name" . }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
      labels:
        app: {{ include "trek.name" . }}
    spec:
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- range .Values.imagePullSecrets }}
        - name: {{ .name }}
        {{- end }}
      {{- end }}
      securityContext:
        fsGroup: 1000
      containers:
        - name: trek
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- with .Values.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          ports:
            - containerPort: 3000
          envFrom:
            - configMapRef:
                name: {{ include "trek.fullname" . }}-config
          env:
            - name: ENCRYPTION_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ default (printf "%s-secret" (include "trek.fullname" .)) .Values.existingSecret }}
                  key: {{ .Values.existingSecretKey | default "ENCRYPTION_KEY" }}
                  optional: true
            - name: ADMIN_EMAIL
              valueFrom:
                secretKeyRef:
                  name: {{ default (printf "%s-secret" (include "trek.fullname" .)) .Values.existingSecret }}
                  key: ADMIN_EMAIL
                  optional: true
            - name: ADMIN_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ default (printf "%s-secret" (include "trek.fullname" .)) .Values.existingSecret }}
                  key: ADMIN_PASSWORD
                  optional: true
            - name: OIDC_CLIENT_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ default (printf "%s-secret" (include "trek.fullname" .)) .Values.existingSecret }}
                  key: OIDC_CLIENT_SECRET
                  optional: true
          volumeMounts:
            - name: data
              mountPath: /app/data
            - name: uploads
              mountPath: /app/uploads
          livenessProbe:
            httpGet:
              path: /api/health
              port: 3000
            initialDelaySeconds: 15
            periodSeconds: 30
          readinessProbe:
            httpGet:
              path: /api/health
              port: 3000
            initialDelaySeconds: 5
            periodSeconds: 10
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: {{ include "trek.fullname" . }}-data
        - name: uploads
          persistentVolumeClaim:
            claimName: {{ include "trek.fullname" . }}-uploads
