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

{{- if .Values.dataNode.enabled }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "milvus.data-node.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: data-node
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.dataNode.autoscaling.hpa.enabled }}
  replicas: {{ .Values.dataNode.replicaCount }}
  {{- end }}
  {{- if .Values.dataNode.updateStrategy }}
  strategy: {{- toYaml .Values.dataNode.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.dataNode.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: data-node
  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 "/data-node/extra-configmap.yaml") . | sha256sum }}
        {{- if .Values.dataNode.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.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: data-node
    spec:
      enableServiceLinks: {{ .Values.enableServiceLinks }}
      serviceAccountName: {{ template "milvus.data-node.serviceAccountName" . }}
      {{- include "milvus.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.dataNode.automountServiceAccountToken }}
      {{- if .Values.dataNode.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.dataNode.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.dataNode.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.dataNode.podAffinityPreset "component" "data-node" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.dataNode.podAntiAffinityPreset "component" "data-node" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.dataNode.nodeAffinityPreset.type "key" .Values.dataNode.nodeAffinityPreset.key "values" .Values.dataNode.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.dataNode.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.dataNode.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.dataNode.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.dataNode.runtimeClassName }}
      runtimeClassName: {{ .Values.dataNode.runtimeClassName | quote }}
      {{- end }}
      {{- if .Values.dataNode.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.dataNode.priorityClassName }}
      priorityClassName: {{ .Values.dataNode.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.dataNode.schedulerName }}
      schedulerName: {{ .Values.dataNode.schedulerName }}
      {{- end }}
      {{- if .Values.dataNode.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.dataNode.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.dataNode.enableDefaultInitContainers }}
        {{- include "milvus.waitForETCDInitContainer" . | nindent 8 }}
        {{- include "milvus.waitForKafkaInitContainer" . | nindent 8 }}
        {{- include "milvus.waitForS3InitContainer" . | nindent 8 }}
        {{- include "milvus.prepareMilvusInitContainer" (dict "component" "dataNode" "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.dataNode.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: milvus
          image: {{ template "milvus.image" . }}
          imagePullPolicy: {{ .Values.milvus.image.pullPolicy }}
          {{- if .Values.dataNode.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.dataNode.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.dataNode.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.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.dataNode.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - run
            - datanode
          {{- end }}
          env:
            - name: METRICS_PORT
              value: {{ .Values.dataNode.containerPorts.metrics | quote }}
            {{- if .Values.dataNode.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.dataNode.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.dataNode.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.dataNode.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.dataNode.extraEnvVarsSecret "context" $) }}
            {{- end }}
          ports:
            - containerPort: {{ .Values.dataNode.containerPorts.grpc }}
              name: grpc
            - containerPort: {{ .Values.dataNode.containerPorts.metrics }}
              name: http-metrics
          {{- if .Values.dataNode.resources }}
          resources: {{- toYaml .Values.dataNode.resources | nindent 12 }}
          {{- else if ne .Values.dataNode.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.dataNode.resourcesPreset) | nindent 12 }}
          {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.dataNode.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.dataNode.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.dataNode.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http-metrics
          {{- end }}
          {{- if .Values.dataNode.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.dataNode.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.dataNode.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /healthz
              port: http-metrics
          {{- end }}
          {{- if .Values.dataNode.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.dataNode.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.dataNode.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http-metrics
          {{- end }}
          {{- end }}
          {{- if .Values.dataNode.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.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.dataNode.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.dataNode.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.dataNode.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.data-node.configmapName" . }}
        {{- if or .Values.dataNode.extraConfig .Values.dataNode.extraConfigExistingConfigMap }}
        - name: component-extra-config
          configMap:
            name: {{ template "milvus.data-node.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.dataNode.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.dataNode.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
