apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "sapl.fullname" . }}
  labels:
    {{- include "sapl.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
{{- end }}
  selector:
    matchLabels:
      {{- include "sapl.selectorLabels" . | nindent 6 }}
  template:
    metadata:
    {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
        {{- include "sapl.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: 80
              protocol: TCP
          env:
            - name: DATABASE_URL
              value: {{ printf "postgresql://%s:%s@%s:5432/%s" .Values.postgresql.postgresqlUsername .Values.postgresql.postgresqlPassword (include "postgresql.fullname" .) .Values.postgresql.postgresqlDatabase| quote }}
            - name: ADMIN_PASSWORD
              value: "{{ .Values.sapl.adminPassword }}"
            - name: ADMIN_EMAIL
              value: "{{ .Values.sapl.adminEmail }}"
            - name: EMAIL_SEND_USER
              value: "{{ .Values.sapl.emailSendUser }}"
            - name: DEBUG
              value: "{{ .Values.sapl.debug }}"
            - name: USE_TLS
              value: "{{ .Values.sapl.useTls }}"
            - name: EMAIL_PORT
              value: "{{ .Values.sapl.emailPort }}"
            - name: EMAIL_HOST
              value: "{{ .Values.sapl.emailHost }}"
            - name: TZ
              value: "{{ .Values.sapl.timeZone }}"
            - name: USE_SOLR
              value: "{{ .Values.sapl.useSolr }}"
            - name: SOLR_COLLECTION
              value: "{{ .Values.sapl.solrCollection }}"
            - name: SOLR_URL
              value: "{{ .Values.sapl.solrUrl }}"
            - name: NUM_SHARDS
              value: "{{ .Values.sapl.solrNumShards }}"
            - name: RF
              value: "{{ .Values.sapl.solrReplFactor }}"
            - name: MAX_SHARDS_PER_NODE
              value: "{{ .Values.sapl.solrMaxShardsPerNode }}"
          volumeMounts:
            - mountPath: /var/interlegis/sapl/data
              name: data
            - mountPath: /var/interlegis/sapl/media
              name: media
          livenessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 60
            failureThreshold: 3
            periodSeconds: 60
            successThreshold: 1
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: /
              port: http
            initialDelaySeconds: 60
            failureThreshold: 3
            periodSeconds: 30
            successThreshold: 1
            timeoutSeconds: 5      
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      volumes:
      - name: data
        secret:
          defaultMode: 440
          secretName: {{ include "sapl.fullname" . }}-secretkey
      - name: media
      {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ include "sapl.fullname" . }}-media
      {{- else }}
        emptyDir: {}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      affinity:
        podAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                  - key: app.kubernetes.io/name
                    operator: In
                    values:
                    - {{ include "sapl.name" . }}
                  - key: app.kubernetes.io/instance
                    operator: In
                    values:
                    - {{ .Release.Name }}
                topologyKey: kubernetes.io/hostname
            - weight: 50
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                  - key: app.kubernetes.io/name
                    operator: In
                    values:
                    - postgresql
                  - key: app.kubernetes.io/instance
                    operator: In
                    values:
                    - {{ .Release.Name }}
                topologyKey: kubernetes.io/hostname
      {{- with .Values.affinity }}
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}