apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "brand-master.fullname" . }}-backend
  labels:
    {{- include "brand-master.labels" . | nindent 4 }}
    app.kubernetes.io/component: backend
spec:
  replicas: {{ .Values.backend.replicaCount }}
  selector:
    matchLabels:
      {{- include "brand-master.selectorLabels" . | nindent 6 }}
      app.kubernetes.io/component: backend
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "brand-master.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: backend
    spec:
      {{- with .Values.global.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "brand-master.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
      - name: wait-for-postgres
        image: harbor.dvirlabs.com/base-images/busybox:1.35
        command: ['sh', '-c', 'until nc -z {{ include "brand-master.fullname" . }}-db-headless {{ .Values.postgres.port | default 5432 }}; do echo waiting for postgres; sleep 2; done;']
      containers:
      - name: backend
        securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
        image: "{{ .Values.backend.image.repository }}:{{ .Values.backend.image.tag | default .Chart.AppVersion }}"
        imagePullPolicy: {{ .Values.backend.image.pullPolicy }}
        ports:
        - name: http
          containerPort: {{ .Values.backend.service.targetPort }}
          protocol: TCP
        env:
        - name: DATABASE_URL
          valueFrom:
            secretKeyRef:
              name: {{ include "brand-master.fullname" . }}-secrets
              key: database-url
        - name: JWT_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: {{ include "brand-master.fullname" . }}-secrets
              key: jwt-secret-key
        - name: JWT_ALGORITHM
          valueFrom:
            secretKeyRef:
              name: {{ include "brand-master.fullname" . }}-secrets
              key: jwt-algorithm
        - name: ACCESS_TOKEN_EXPIRE_MINUTES
          valueFrom:
            secretKeyRef:
              name: {{ include "brand-master.fullname" . }}-secrets
              key: jwt-expire-minutes
        {{- range $key, $value := .Values.backend.env }}
        - name: {{ $key }}
          value: {{ $value | quote }}
        {{- end }}
        volumeMounts:
        {{- if .Values.backend.persistence.enabled }}
        - name: uploads
          mountPath: {{ .Values.backend.persistence.mountPath }}
        {{- end }}
        livenessProbe:
          httpGet:
            path: /
            port: http
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /
            port: http
          initialDelaySeconds: 10
          periodSeconds: 5
        resources:
          {{- toYaml .Values.backend.resources | nindent 12 }}
      volumes:
      {{- if .Values.backend.persistence.enabled }}
      - name: uploads
        persistentVolumeClaim:
          claimName: {{ include "brand-master.fullname" . }}-uploads-pvc
      {{- 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 }}
