apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "nemo-evaluator.fullname" . }}
  labels:
    {{- include "nemo-evaluator.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "nemo-evaluator.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "nemo-evaluator.labels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      imagePullSecrets:
        {{ include "nemo-common.imagepullsecrets" . | nindent 8 | trim }}
      serviceAccountName: {{ include "nemo-evaluator.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        {{- if .Values.postgresql.enabled }}
        - name: wait-for-postgres
          image: busybox
          command: ['sh', '-c', 'until nc -z {{ include "nemo-common.postgresql.host" . }} {{ include "nemo-common.postgresql.port" . }}; do echo "Waiting for Postgres to start"; sleep 5; done;']
        {{- end }}
        {{- include "nemo-evaluator.dbMigration" . | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: {{ include "nemo-common.mainimage" . | quote }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          envFrom:
          - configMapRef:
              name: evaluation-to-image-config
          env:
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: HOST_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
            - name: EVALUATOR_HOST
              value: {{ .Values.evaluator.host }}
            - name: EVALUATOR_PORT
              value: {{ .Values.service.port | quote }}
            {{ include "nemo-common.postgresql.password" . | nindent 12 | trim }}
            - name: POSTGRES_URI
              {{ include "nemo-common.postgresql.uri" . | nindent 14 | trim }}
            {{- if .Values.argoWorkflows.enabled }}
            - name: ARGO_HOST
              value: "https://{{ .Release.Name }}-{{ .Values.argoWorkflows.serviceName }}:{{ .Values.argoWorkflows.server.servicePort }}"
            {{- else }}
            - name: ARGO_HOST
              value: "{{ .Values.external.argoWorkflows.endpoint}}"
            {{- end }}
            {{- if .Values.milvus.enabled }}
            - name: MILVUS_URL
              value: "http://{{ .Release.Name }}-{{ .Values.milvus.serviceName }}:{{ .Values.milvus.service.port }}"
            {{- else }}
            - name: MILVUS_URL
              value: "{{ .Values.external.milvus.endpoint}}"
            {{- end }}
            - name: SERVICE_ACCOUNT
              value: "{{ .Release.Name }}-{{ .Values.argoServiceAccount.name }}"
            - name: DATA_STORE_URL
              value: "{{ .Values.external.dataStore.endpoint }}"
            - name: ENTITY_STORE_URL
              value: "{{ .Values.external.entityStore.endpoint }}"
            - name: NIM_PROXY_URL
              value: "{{ .Values.external.nimProxy.endpoint }}"
            - name: EVAL_CONTAINER
              value: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
            - name: EVAL_ENABLE_VALIDATION
              value: "True"
            {{ include "nemo-common.env" . | nindent 12 | trim }}
            {{- if .Values.envVars }}
            {{- range $k, $v := .Values.envVars }}
            - name: "{{ $k }}"
              value: "{{ $v }}"
            {{- end }}
            {{- end }}

            # OpenTelemetry
            {{- if .Values.otelExporterEnabled }}
            {{- range $k, $v := .Values.otelEnvVars }}
            - name: "{{ $k }}"
              value: "{{ $v }}"
            {{- end }}
            {{- if and .Values.otelExporterEnabled ( not ( index .Values.otelEnvVars "OTEL_EXPORTER_OTLP_ENDPOINT" ) ) }}
            - name: OTEL_EXPORTER_OTLP_ENDPOINT
              value: "http://{{ .Release.Name }}-opentelemetry-collector:4317"
            {{- end }}
            - name: CONSOLE_LOG_LEVEL
              value: "{{ .Values.logLevel }}"
            - name: OTEL_LOG_LEVEL
              value: "{{ .Values.logLevel }}"
            {{- else }}
            - name: OTEL_TRACES_EXPORTER
              value: none
            - name: OTEL_METRICS_EXPORTER
              value: none
            - name: OTEL_LOGS_EXPORTER
              value: none
            - name: OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED
              value: "false"
            - name: LOG_HANDLERS
              value: console
            - name: CONSOLE_LOG_LEVEL
              value: "{{ .Values.logLevel }}"
            {{- end }}
            - name: EVAL_LOG_LEVEL
              value: {{ default "INFO" .Values.logLevel | quote }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          livenessProbe:
            {{- toYaml .Values.livenessProbe | nindent 12 }}
          readinessProbe:
            {{- toYaml .Values.readinessProbe | nindent 12 }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- with .Values.volumeMounts }}
          volumeMounts:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.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 }}
