apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "peikarband.fullname" . }}
  labels:
    {{- include "peikarband.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "peikarband.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        {{- include "peikarband.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "peikarband.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: backend
          containerPort: {{ .Values.service.backend.targetPort }}
          protocol: TCP
        - name: frontend
          containerPort: {{ .Values.service.frontend.targetPort }}
          protocol: TCP
        livenessProbe:
          {{- toYaml .Values.livenessProbe | nindent 12 }}
        readinessProbe:
          {{- toYaml .Values.readinessProbe | nindent 12 }}
        resources:
          {{- toYaml .Values.resources | nindent 12 }}
        env:
          {{- toYaml .Values.env | nindent 12 }}
          {{- if .Values.postgresql.enabled }}
          - name: DATABASE_HOST
            value: {{ .Values.postgresql.external.host }}
          - name: DATABASE_PORT
            value: {{ .Values.postgresql.external.port | quote }}
          - name: DATABASE_NAME
            value: {{ .Values.postgresql.external.database }}
          - name: DATABASE_USER
            valueFrom:
              secretKeyRef:
                name: {{ .Values.postgresql.external.usernameSecret.name }}
                key: {{ .Values.postgresql.external.usernameSecret.key }}
          - name: DATABASE_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ .Values.postgresql.external.passwordSecret.name }}
                key: {{ .Values.postgresql.external.passwordSecret.key }}
          {{- end }}
          {{- if .Values.redis.enabled }}
          - name: REDIS_HOST
            value: {{ .Values.redis.external.host }}
          - name: REDIS_PORT
            value: {{ .Values.redis.external.port | quote }}
          - name: REDIS_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ .Values.redis.external.passwordSecret.name }}
                key: {{ .Values.redis.external.passwordSecret.key }}
          {{- end }}
        envFrom:
        - configMapRef:
            name: {{ include "peikarband.fullname" . }}
        {{- with .Values.envFrom }}
        {{- toYaml . | nindent 10 }}
        {{- 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 }}

