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

apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "victoriametrics.vminsert.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" (dict "customLabels" .Values.commonLabels "context" $) | nindent 4 }}
    app.kubernetes.io/part-of: victoriametrics
    app.kubernetes.io/component: vminsert
  {{- if or .Values.vminsert.deploymentAnnotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.vminsert.deploymentAnnotations .Values.commonAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if not .Values.vminsert.autoscaling.hpa.enabled }}
  replicas: {{ .Values.vminsert.replicaCount }}
  {{- end }}
  {{- if .Values.vminsert.updateStrategy }}
  strategy: {{- toYaml .Values.vminsert.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" (dict "values" (list .Values.vminsert.podLabels .Values.commonLabels) "context" .) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: vminsert
  template:
    metadata:
      {{- if .Values.vminsert.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: victoriametrics
        app.kubernetes.io/component: vminsert
    spec:
      serviceAccountName: {{ template "victoriametrics.vminsert.serviceAccountName" . }}
      {{- include "victoriametrics.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.vminsert.automountServiceAccountToken }}
      {{- if .Values.vminsert.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.vminsert.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.vminsert.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.vminsert.podAffinityPreset "component" "vminsert" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.vminsert.podAntiAffinityPreset "component" "vminsert" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.vminsert.nodeAffinityPreset.type "key" .Values.vminsert.nodeAffinityPreset.key "values" .Values.vminsert.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.vminsert.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.vminsert.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.vminsert.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.vminsert.priorityClassName }}
      priorityClassName: {{ .Values.vminsert.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.vminsert.schedulerName }}
      schedulerName: {{ .Values.vminsert.schedulerName | quote }}
      {{- end }}
      {{- if .Values.vminsert.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.vminsert.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.vminsert.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.vminsert.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.vminsert.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.vminsert.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: vminsert
          image: {{ template "victoriametrics.vminsert.image" . }}
          imagePullPolicy: {{ .Values.vminsert.image.pullPolicy }}
          {{- if .Values.vminsert.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.vminsert.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.vminsert.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.command "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.vminsert.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            {{- if .Values.envflagEnable }}
            - --envflag.enable
            - --envflag.prefix={{ .Values.envflagPrefix }}
            {{- end }}
            - --loggerFormat={{ .Values.loggerFormat }}
            - --httpListenAddr=:{{ .Values.vminsert.containerPorts.http }}
            {{- range $e, $i := until (int .Values.vmstorage.replicaCount) }}
            - --storageNode={{ include "victoriametrics.vmstorage.fullname" $ }}-{{ int $i }}.{{ printf "%s-headless" (include "victoriametrics.vmstorage.fullname" $) | trunc 63 | trimSuffix "-" }}:{{ $.Values.vmstorage.containerPorts.vminsert }}
            {{- end }}
            {{- if .Values.vminsert.extraArgs }}
            {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.extraArgs "context" $) | nindent 12 }}
            {{- end }}
          {{- end }}
          env:
            {{- if .Values.vminsert.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.vminsert.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.vminsert.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.vminsert.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.vminsert.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.vminsert.resources }}
          resources: {{- toYaml .Values.vminsert.resources | nindent 12 }}
          {{- else if ne .Values.vminsert.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.vminsert.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.vminsert.containerPorts.http }}
          {{- if .Values.vminsert.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.vminsert.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.vminsert.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- if .Values.vminsert.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.vminsert.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.vminsert.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /health
              port: http
          {{- end }}
          {{- if .Values.vminsert.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.vminsert.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.vminsert.startupProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /health
              port: http
          {{- end }}
          {{- if .Values.vminsert.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.vminsert.extraVolumeMounts }}
          volumeMounts: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.vminsert.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.vminsert.sidecars "context" $) | nindent 8 }}
        {{- end }}
      {{- if .Values.vminsert.extraVolumes }}
      volumes: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.extraVolumes "context" $) | nindent 8 }}
      {{- end }}
