{{- if and .Values.indexNode.enabled .Values.cluster.enabled }}
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "milvus.indexnode.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
{{ include "milvus.labels" . | indent 4 }}
    component: "indexnode"
{{ include "milvus.ud.labels" . | indent 4 }}
  annotations:
{{ include "milvus.ud.annotations" . | indent 4 }}

spec:
  {{- if ge (int .Values.indexNode.replicas) 0 }}
  replicas: {{ .Values.indexNode.replicas }}
  {{- end }}
  {{- with .Values.indexNode.strategy }}
  strategy:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  selector:
    matchLabels:
{{ include "milvus.matchLabels" . | indent 6 }}
      component: "indexnode"
  template:
    metadata:
      labels:
{{ include "milvus.matchLabels" . | indent 8 }}
        component: "indexnode"
{{ include "milvus.ud.labels" . | indent 8 }}
      annotations:
      {{- if .Values.indexNode.profiling.enabled }}
        pyroscope.io/scrape: "true"
        pyroscope.io/application-name: {{ template "milvus.indexnode.fullname" . }}
        pyroscope.io/port: "9091"
      {{- end }}
      {{- if .Values.indexNode.annotations }}
        {{- toYaml .Values.indexNode.annotations | nindent 8 }}
      {{- end }}
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
{{ include "milvus.ud.annotations" . | indent 8 }}
    spec:
      serviceAccountName: {{ include "milvus.serviceAccount" . }}
      {{- if .Values.indexNode.runtimeClassName }}
      runtimeClassName: {{ .Values.indexNode.runtimeClassName }}
      {{- end }}
      {{- if .Values.image.all.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.all.pullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      initContainers:
      {{- if .Values.indexNode.heaptrack.enabled }}
      - name: heaptrack
        command:
        - /bin/bash
        - -c
        - "cp -r /opt/heaptrack /milvus/tools"
        image: "{{ .Values.heaptrack.image.repository }}:{{ .Values.heaptrack.image.tag }}"
        imagePullPolicy: {{ .Values.heaptrack.image.pullPolicy }}
        {{ if and (.Values.containerSecurityContext) (not .Values.indexNode.containerSecurityContext) }}
        securityContext:
          {{- toYaml .Values.containerSecurityContext | nindent 12 }}
        {{ end }}
        {{ if .Values.indexNode.containerSecurityContext }}
        securityContext:
          {{- toYaml .Values.indexNode.containerSecurityContext | nindent 12 }}
        {{ end }}
        volumeMounts:
        - mountPath: /milvus/tools
          name: tools
      {{- end }}
      containers:
      - name: indexnode
        image: "{{ .Values.image.all.repository }}:{{ .Values.image.all.tag }}"
        imagePullPolicy: {{ .Values.image.all.pullPolicy }}
        {{ if and (.Values.containerSecurityContext) (not .Values.indexNode.containerSecurityContext) }}
        securityContext:
          {{- toYaml .Values.containerSecurityContext | nindent 12 }}
        {{ end }}
        {{ if .Values.indexNode.containerSecurityContext }}
        securityContext:
          {{- toYaml .Values.indexNode.containerSecurityContext | nindent 12 }}
        {{ end }}
        {{- if .Values.indexNode.heaptrack.enabled }}
        args: [ "/milvus/tools/heaptrack/bin/heaptrack", "milvus", "run", "indexnode" ]
        {{- else }}
        args: [ "milvus", "run", "indexnode" ]
        {{- end }}
        env:
        {{- if and .Values.streaming .Values.streaming.enabled }}
        - name: MILVUS_STREAMING_SERVICE_ENABLED
          value: "1"
        {{- end }}
        {{- if .Values.indexNode.heaptrack.enabled }}
        - name: LD_LIBRARY_PATH
          value: /milvus/tools/heaptrack/lib:/milvus/lib:/usr/lib
        {{- end }}
        {{- if .Values.indexNode.disk.size.enabled }}
        - name: LOCAL_STORAGE_SIZE
          valueFrom:
            resourceFieldRef:
              divisor: 1Gi
              resource: limits.ephemeral-storage
        {{- end }}
        {{- if .Values.indexNode.extraEnv }}
          {{- toYaml .Values.indexNode.extraEnv | nindent 8 }}
        {{- end }}
        ports:
          - name: indexnode
            containerPort: 21121
            protocol: TCP
          - name: metrics
            containerPort: 9091
            protocol: TCP
        {{- if .Values.livenessProbe.enabled }}
        livenessProbe:
          tcpSocket:
            port: metrics
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.livenessProbe.successThreshold }}
          failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
        {{- end }}
        {{- if .Values.readinessProbe.enabled }}
        readinessProbe:
          httpGet:
            path: /healthz
            port: metrics
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
          successThreshold: {{ .Values.readinessProbe.successThreshold }}
          failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
        {{- end }}
        resources:
          {{- toYaml .Values.indexNode.resources | nindent 10 }}
        volumeMounts:
        {{- if .Values.customConfigMap }}
        - name: milvus-config
          mountPath: /milvus/configs/user.yaml
          subPath: milvus.yaml
          readOnly: true
        {{- else }}
        - name: milvus-config
          mountPath: /milvus/configs/default.yaml
          subPath: default.yaml
          readOnly: true
        - name: milvus-config
          mountPath: /milvus/configs/user.yaml
          subPath: user.yaml
          readOnly: true
        {{- end }}
        {{- if .Values.log.persistence.enabled }}
        - name: milvus-logs-disk
          mountPath: {{ .Values.log.persistence.mountPath | quote }}
          subPath: {{ .Values.log.persistence.persistentVolumeClaim.subPath | default "" }}
        {{- end }}
        - mountPath: /milvus/tools
          name: tools
        {{- if .Values.indexNode.disk.enabled }}
        - mountPath: /var/lib/milvus/data
          name: disk
        {{- end }}
        {{- if .Values.volumeMounts }}
          {{- toYaml .Values.volumeMounts | nindent 8 }}
        {{- end}}

    {{- if and (.Values.nodeSelector) (not .Values.indexNode.nodeSelector) }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.indexNode.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.indexNode.nodeSelector | indent 8 }}
    {{- end }}
    {{- if and (.Values.affinity) (not .Values.indexNode.affinity) }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
    {{- end }}
    {{- if .Values.indexNode.affinity }}
      affinity:
{{ toYaml .Values.indexNode.affinity | indent 8 }}
    {{- end }}
    {{- if and (.Values.tolerations) (not .Values.indexNode.tolerations) }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
    {{- end }}
    {{- if .Values.indexNode.tolerations }}
      tolerations:
{{ toYaml .Values.indexNode.tolerations | indent 8 }}
    {{- end }}

    {{- if and (.Values.topologySpreadConstraints) (not .Values.indexNode.topologySpreadConstraints) }}
      topologySpreadConstraints:
{{ toYaml .Values.topologySpreadConstraints | indent 8 }}
    {{- end }}
    {{- if .Values.indexNode.topologySpreadConstraints }}
      topologySpreadConstraints:
{{ toYaml .Values.indexNode.topologySpreadConstraints | indent 8 }}
    {{- end }}
    {{- if and (.Values.securityContext) (not .Values.indexNode.securityContext) }}
      securityContext:
{{ toYaml .Values.securityContext | indent 8 }}
    {{- end }}
    {{- if .Values.indexNode.securityContext }}
      securityContext:
{{ toYaml .Values.indexNode.securityContext | indent 8 }}
    {{- end }}

      volumes:
      - name: milvus-config
        configMap:
          {{- if .Values.customConfigMap }}
          name: {{ .Values.customConfigMap }}
          {{- else }}
          name: {{ template "milvus.fullname" . }}
          {{- end }}
      {{- if .Values.log.persistence.enabled }}
      - name: milvus-logs-disk
        persistentVolumeClaim:
          claimName: {{ .Values.log.persistence.persistentVolumeClaim.existingClaim | default (printf "%s-logs" (include "milvus.fullname" . | trunc 58)) }}
      {{- end }}
      - name: tools
        emptyDir: {}
      {{- if .Values.indexNode.disk.enabled }}
      - name: disk
        emptyDir: {}
      {{- end }}
      {{- if .Values.volumes }}
        {{- toYaml .Values.volumes | nindent 6 }}
      {{- end}}
{{- end }}
