apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "assist.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "assist.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "assist.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "assist.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "assist.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      shareProcessNamespace: true
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          {{- if .Values.global.enterpriseEditionLicense }}
          image: "{{ tpl .Values.image.repository . }}:{{ .Values.image.tag | default .Chart.AppVersion }}-ee"
          {{- else }}
          image: "{{ tpl .Values.image.repository . }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          {{- end }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.healthCheck}}
          {{- .Values.healthCheck | toYaml | nindent 10}}
          {{- end}}
          env:
            - name: ASSIST_JWT_SECRET
              value: {{ .Values.global.assistJWTSecret }}
            - name: ASSIST_KEY
              value: {{ .Values.global.assistKey }}
            - name: AWS_DEFAULT_REGION
              value: "{{ .Values.global.s3.region }}"
            - name: S3_HOST
              {{- if contains "minio" .Values.global.s3.endpoint }}
              value: '{{ ternary "https" "http" .Values.global.ORSecureAccess}}://{{ .Values.global.domainName }}:{{ ternary .Values.global.ingress.controller.service.ports.https .Values.global.ingress.controller.service.ports.http .Values.global.ORSecureAccess }}'
              {{- else}}
              value: '{{ .Values.global.s3.endpoint }}'
              {{- end}}
            - name: S3_KEY
              {{- if .Values.global.s3.existingSecret }}
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.global.s3.existingSecret }}
                  key: access-key
              {{- else }}
              value: {{ .Values.global.s3.accessKey }}
              {{- end }}
            - name: S3_SECRET
              {{- if .Values.global.s3.existingSecret }}
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.global.s3.existingSecret }}
                  key: secret-key
              {{- else }}
              value: {{ .Values.global.s3.secretKey }}
              {{- end }}
            - name: REDIS_URL
              value: {{ .Values.global.redis.redisHost }}
            {{- range $key, $val := .Values.global.env }}
            - name: {{ $key }}
              value: '{{ $val }}'
            {{- end }}
            {{- range $key, $val := .Values.env }}
            - name: {{ $key }}
              value: '{{ $val }}'
            {{- end}}
          ports:
            {{- range $key, $val := .Values.service.ports }}
            - name: {{ $key }}
              containerPort: {{ $val }}
            {{- end }}
              protocol: TCP
          {{- with .Values.persistence.mounts  }}
          volumeMounts:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.persistence.volumes  }}
      volumes:
        {{- toYaml . | nindent 8 }}
      {{- 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 }}
