apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "garage-ui.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "garage-ui.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "garage-ui.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
      {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "garage-ui.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
      - name: {{ .Chart.Name }}
        securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
        image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
        - name: http
          containerPort: {{ .Values.config.server.port }}
          protocol: TCP
        env:
        - name: GARAGE_UI_GARAGE_ADMIN_TOKEN
          valueFrom:
            secretKeyRef:
              {{- if .Values.config.garage.existingSecret.name }}
              name: {{ .Values.config.garage.existingSecret.name }}
              key: {{ .Values.config.garage.existingSecret.key }}
              {{- else }}
              name: {{ include "garage-ui.fullname" . }}-admin-token
              key: admin-token
              {{- end }}
        - name: GARAGE_UI_AUTH_JWT_PRIVATE_KEY
          valueFrom:
            secretKeyRef:
              {{- if .Values.config.auth.jwt_private_key_secret.name }}
              name: {{ .Values.config.auth.jwt_private_key_secret.name }}
              key: {{ .Values.config.auth.jwt_private_key_secret.key }}
              {{- else }}
              name: {{ include "garage-ui.fullname" . }}-jwt-key
              key: jwt-key.pem
              {{- end }}
        {{- if .Values.config.auth.oidc.enabled }}
        - name: GARAGE_UI_AUTH_OIDC_CLIENT_SECRET
          valueFrom:
            secretKeyRef:
              {{- if .Values.config.auth.oidc.existingSecret.name }}
              name: {{ .Values.config.auth.oidc.existingSecret.name }}
              key: {{ .Values.config.auth.oidc.existingSecret.key }}
              {{- else }}
              name: {{ include "garage-ui.fullname" . }}-oidc-client-secret
              key: client-secret
              {{- end }}
        {{- end }}
        {{- if .Values.config.auth.admin.enabled }}
        - name: GARAGE_UI_AUTH_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              {{- if .Values.config.auth.admin.existingSecret.name }}
              name: {{ .Values.config.auth.admin.existingSecret.name }}
              key: {{ .Values.config.auth.admin.existingSecret.key }}
              {{- else }}
              name: {{ include "garage-ui.fullname" . }}-admin-password
              key: admin-password
              {{- end }}
        {{- end }}
        {{- if .Values.livenessProbe.enabled }}
        livenessProbe:
          httpGet:
            path: {{ .Values.livenessProbe.httpGet.path }}
            port: {{ .Values.livenessProbe.httpGet.port }}
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
          failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
        {{- end }}
        {{- if .Values.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: {{ .Values.readinessProbe.httpGet.path }}
            port: {{ .Values.readinessProbe.httpGet.port }}
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
          failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
        {{- end }}
        resources:
          {{- toYaml .Values.resources | nindent 12 }}
        volumeMounts:
        - name: config
          mountPath: /app/config.yaml
          subPath: config.yaml
          readOnly: true
      volumes:
      - name: config
        configMap:
          name: {{ include "garage-ui.fullname" . }}-config
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
