{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if .Values.coordinator.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "milvus.coordinator.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: milvus
    app.kubernetes.io/component: coordinator
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.coordinator.autoscaling.hpa.enabled }}
  replicas: {{ .Values.coordinator.replicaCount }}
  {{- end }}
  {{- if .Values.coordinator.updateStrategy }}
  strategy: {{- toYaml .Values.coordinator.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.coordinator.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/part-of: milvus
      app.kubernetes.io/component: coordinator
  template:
    metadata:
      annotations:
        checksum/common-config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/common-config-extra: {{ include (print $.Template.BasePath "/extra-configmap.yaml") . | sha256sum }}
        checksum/config-extra: {{ include (print $.Template.BasePath "/coordinator/extra-configmap.yaml") . | sha256sum }}
        {{- if .Values.coordinator.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: milvus
        app.kubernetes.io/component: coordinator
    spec:
      enableServiceLinks: {{ .Values.enableServiceLinks }}
      serviceAccountName: {{ template "milvus.coordinator.serviceAccountName" . }}
      {{- include "milvus.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.coordinator.automountServiceAccountToken }}
      {{- if .Values.coordinator.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.coordinator.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.coordinator.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.coordinator.podAffinityPreset "component" "coordinator" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.coordinator.podAntiAffinityPreset "component" "coordinator" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.coordinator.nodeAffinityPreset.type "key" .Values.coordinator.nodeAffinityPreset.key "values" .Values.coordinator.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.coordinator.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.coordinator.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.coordinator.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.coordinator.runtimeClassName }}
      runtimeClassName: {{ .Values.coordinator.runtimeClassName | quote }}
      {{- end }}
      {{- if .Values.coordinator.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.coordinator.priorityClassName }}
      priorityClassName: {{ .Values.coordinator.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.coordinator.schedulerName }}
      schedulerName: {{ .Values.coordinator.schedulerName }}
      {{- end }}
      {{- if .Values.coordinator.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.coordinator.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.coordinator.enableDefaultInitContainers }}
        {{- include "milvus.waitForETCDInitContainer" . | nindent 8 }}
        {{- include "milvus.waitForKafkaInitContainer" . | nindent 8 }}
        {{- include "milvus.waitForS3InitContainer" . | nindent 8 }}
        {{- include "milvus.prepareMilvusInitContainer" (dict "component" "coordinator" "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.coordinator.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: milvus
          image: {{ template "milvus.image" . }}
          imagePullPolicy: {{ .Values.milvus.image.pullPolicy }}
          {{- if .Values.coordinator.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.coordinator.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.coordinator.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.coordinator.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - run
            - mixcoord
          {{- end }}
          env:
            - name: METRICS_PORT
              value: {{ .Values.coordinator.containerPorts.metrics | quote }}
            {{- if .Values.coordinator.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.coordinator.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.coordinator.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.coordinator.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.coordinator.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.coordinator.containerPorts.metrics }}
              name: http-metrics
          {{- if .Values.coordinator.resources }}
          resources: {{- toYaml .Values.coordinator.resources | nindent 12 }}
          {{- else if ne .Values.coordinator.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.coordinator.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.coordinator.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.coordinator.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.coordinator.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http-metrics
          {{- end }}
          {{- if .Values.coordinator.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.coordinator.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.coordinator.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: http-metrics
          {{- end }}
          {{- if .Values.coordinator.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.coordinator.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.coordinator.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http-metrics
          {{- end }}
          {{- end }}
          {{- if .Values.coordinator.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: empty-dir
              mountPath: /opt/bitnami/milvus/configs
              subPath: app-rendered-conf-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /opt/bitnami/milvus/tmp
              subPath: app-tmp-dir
              # We are using a s3 backend, so this data dir is temporary
            - name: empty-dir
              mountPath: /bitnami/milvus/data
              subPath: app-data-dir
            {{- if and (not .Values.etcd.enabled) .Values.externalEtcd.tls.enabled .Values.externalEtcd.tls.existingSecret }}
            - name: etcd-client-certs
              mountPath: /opt/bitnami/milvus/configs/cert/etcd/client
              readOnly: true
            {{- end }}
            {{- if and (not .Values.kafka.enabled) .Values.externalKafka.tls.enabled .Values.externalKafka.tls.existingSecret }}
            - name: kafka-client-certs
              mountPath: /opt/bitnami/milvus/configs/cert/kafka/client
              readOnly: true
            {{- end }}
            {{- if and (not .Values.minio.enabled) .Values.externalS3.tls.enabled .Values.externalS3.tls.existingSecret }}
            - name: minio-client-certs
              mountPath: /opt/bitnami/milvus/configs/cert/minio/client
              readOnly: true
            {{- end }}
          {{- if .Values.coordinator.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.coordinator.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.coordinator.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: config-common
          configMap:
            name: {{ template "milvus.configmapName" . }}
        {{- if and .Values.usePasswordFiles (or (include "milvus.s3.deployed" .) (and (include "milvus.kafka.deployed" .) (or (include "milvus.kafka.authEnabled" .) (and .Values.externalKafka.tls.enabled .Values.externalKafka.tls.keyPassword .Values.externalKafka.tls.existingSecret)))) }}
        - name: milvus-secrets
          projected:
            sources:
              {{- if and (include "milvus.kafka.deployed" .) (include "milvus.kafka.authEnabled" .)}}
              - secret:
                  name: {{ include "milvus.kafka.secretName" . }}
              {{- end }}
              {{- if and (include "milvus.kafka.deployed" .) .Values.externalKafka.tls.enabled .Values.externalKafka.tls.keyPassword .Values.externalKafka.tls.existingSecret }}
              - secret:
                  name: {{ printf "%s-external-kafka-tls-passwords" (include "common.names.fullname" .) }}
              {{- end }}
              {{- if (include "milvus.s3.deployed" .) }}
              - secret:
                  name: {{ include "milvus.s3.secretName" . }}
              {{- end }}
        {{- end }}
        {{- if or .Values.milvus.extraConfig .Values.milvus.extraConfigExistingConfigMap }}
        - name: extra-config-common
          configMap:
            name: {{ template "milvus.extraConfigmapName" . }}
        {{- end }}
        - name: component-config-default
          configMap:
            name: {{ template "milvus.coordinator.configmapName" . }}
        {{- if or .Values.coordinator.extraConfig .Values.coordinator.extraConfigExistingConfigMap }}
        - name: component-extra-config
          configMap:
            name: {{ template "milvus.coordinator.extraConfigmapName" . }}
        {{- end }}
        {{- if and (not .Values.etcd.enabled) .Values.externalEtcd.tls.enabled .Values.externalEtcd.tls.existingSecret }}
        - name: etcd-client-certs
          secret:
            secretName: {{ .Values.externalEtcd.tls.existingSecret }}
            defaultMode: 256
        {{- end }}
        {{- if and (not .Values.kafka.enabled) .Values.externalKafka.tls.enabled .Values.externalKafka.tls.existingSecret }}
        - name: kafka-client-certs
          secret:
            secretName: {{ .Values.externalKafka.tls.existingSecret }}
            defaultMode: 256
        {{- end }}
        {{- if and (not .Values.minio.enabled) .Values.externalS3.tls.enabled .Values.externalS3.tls.existingSecret }}
        - name: minio-client-certs
          secret:
            secretName: {{ .Values.externalS3.tls.existingSecret }}
            defaultMode: 256
        {{- end }}
        {{- if .Values.coordinator.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.coordinator.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
