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

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