{{- $fullName := include "library-chart.fullname" . -}}
{{- $initContainerName := printf "%s-%s" $fullName "init-data" | trunc 63 | trimSuffix "-" -}}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "library-chart.fullname" . }}
  labels:
    {{- include "library-chart.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
{{- end }}
  selector:
    matchLabels:
      {{- include "library-chart.selectorLabels" . | nindent 6 }}
  template:
    metadata:
    {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
        {{- include "library-chart.selectorLabels" . | nindent 8 }}
    spec:
      {{- if .Values.security.enabled }}
      volumes:
      - name: secret-auth-ini-volume
        secret:
          secretName: {{ .Release.Name }}-secret-mlflow-auth
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          {{- if .Values.service.image.custom.enabled }}
          image: "{{ .Values.service.image.custom.version }}"
          {{- else }}
          image: "{{ .Values.service.image.version }}"
          {{- end }}
          args:
            - --host=0.0.0.0
            - --port=8000
            - --backend-store-uri=postgresql://{{ .Values.postgresql.auth.username }}:{{ .Values.postgresql.auth.password }}@{{ .Values.postgresql.fullnameOverride }}:5432/{{ .Values.postgresql.auth.database}}
            {{ if .Values.service.proxyArtifact.enabled -}}
            - --artifacts-destination={{ .Values.service.directory }}
            {{- else -}}
            - --default-artifact-root={{ .Values.service.directory }}
            {{- end }}
            {{ if .Values.security.enabled -}}
            - --app-name=basic-auth
            {{- end }}
            {{  range .Values.service.customArgs }}
            - {{ . }}
            {{- end }}
          imagePullPolicy: {{ .Values.service.image.pullPolicy }}
          envFrom: 
            {{- if .Values.s3.enabled }}
            - secretRef:
                name: {{ include "library-chart.secretNameS3" . }}
            {{- end }}
          {{- if .Values.security.enabled }}
          volumeMounts:
          - name: secret-auth-ini-volume
            mountPath: /home/oyxia/work/basic_auth.ini
            subPath: basic_auth.ini 
          {{- end }}
          env:
            - name: MLFLOW_S3_ENDPOINT_URL
              value: "https://{{ .Values.s3.endpoint }}"
            {{- if .Values.security.enabled }}
            - name: MLFLOW_AUTH_CONFIG_PATH
              value: /home/oyxia/work/basic_auth.ini
            {{- end }}
            - name: GUNICORN_CMD_ARGS
              value: "--timeout {{ .Values.service.gunicornTimeout }}"
          ports:
            - name: http
              containerPort: 8000
              protocol: TCP
          readinessProbe:
            tcpSocket:
              port: 8000
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            tcpSocket:
              port: 8000
            initialDelaySeconds: 15
            periodSeconds: 20
          resources:
            {{- toYaml .Values.resources | nindent 12 }}         

      initContainers:
        - name: wait-for-postgresql
          image: "alpine"
          imagePullPolicy: IfNotPresent
          command:
            - sh
            - -c
            - |
              until printf "." && nc -z -w 2 {{ .Values.postgresql.fullnameOverride }} 5432; do
                  sleep 2;
              done;
              echo 'PostgreSQL OK ✓'
          resources:
            {{- toYaml .Values.resources_init | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
