apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "umap.fullname" . }}
  labels:
    {{- include "umap.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "umap.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/secret-config.yaml") . | sha256sum }}
        checksum/env: {{ include (print $.Template.BasePath "/secret-env.yaml") . | sha256sum }}
      {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "umap.labels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "umap.serviceAccountName" . }}
      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.service.port }}
              protocol: TCP
          startupProbe:
            httpGet:
              path: /stats
              port: http
          livenessProbe:
            httpGet:
              path: /stats
              port: http
          readinessProbe:
            httpGet:
              path: /stats
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- if .Values.cnpg.enabled }}
          env:
          - name: DATABASE_URL
            valueFrom:
              secretKeyRef:
                name: {{ .Release.Name }}-cnpg-app
                key: uri
          - name: UMAP_SETTINGS
            value: /etc/umap/umap.conf
          {{- end }}
          envFrom:
          - secretRef:
              name: {{ include "umap.fullname" . }}-env
          volumeMounts:
            - name: config
              mountPath: /etc/umap/
              readOnly: true
            - name: statics
              mountPath: /srv/umap/static
          {{- if .Values.persistence.enabled }}
            - name: data
              mountPath: /srv/umap/uploads/
          {{- end }}
      volumes:
        - name: config
          secret:
            secretName: {{ include "umap.fullname" . }}-config
        - name: statics
          emptyDir: {}
      {{- if .Values.persistence.enabled }}
        - name: data
          persistentVolumeClaim:
            claimName: {{ include "umap.pvcName" . }}
      {{- 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 }}
