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

apiVersion: v1
kind: Service
metadata:
  name: {{ include "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.service.annotations .Values.commonAnnotations .Values.vminsert.metrics.enabled .Values.vminsert.metrics.annotations }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.vminsert.service.annotations .Values.commonAnnotations .Values.vminsert.metrics.annotations) "context" .) }}
  {{- if .Values.vminsert.metrics.enabled }}
  {{- $defaultMetricsAnnotations := dict "prometheus.io/scrape" "true" "prometheus.io/port" (.Values.vminsert.service.ports.http | toString) "prometheus.io/path" "/metrics" }}
  {{- $annotations = include "common.tplvalues.merge" (dict "values" (list $annotations $defaultMetricsAnnotations) "context" .) }}
  {{- end }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.vminsert.service.type }}
  {{- if and .Values.vminsert.service.clusterIP (eq .Values.vminsert.service.type "ClusterIP") }}
  clusterIP: {{ .Values.vminsert.service.clusterIP }}
  {{- end }}
  {{- if .Values.vminsert.service.sessionAffinity }}
  sessionAffinity: {{ .Values.vminsert.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.vminsert.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.vminsert.service.type "LoadBalancer") (eq .Values.vminsert.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.vminsert.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.vminsert.service.type "LoadBalancer") (not (empty .Values.vminsert.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.vminsert.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.vminsert.service.type "LoadBalancer") (not (empty .Values.vminsert.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.vminsert.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.vminsert.service.ports.http }}
      protocol: TCP
      {{- if and (or (eq .Values.vminsert.service.type "NodePort") (eq .Values.vminsert.service.type "LoadBalancer")) (not (empty .Values.vminsert.service.nodePorts.http)) }}
      nodePort: {{ .Values.vminsert.service.nodePorts.http }}
      {{- else if eq .Values.vminsert.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: http
    {{- if .Values.vminsert.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.vminsert.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.vminsert.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: vminsert
