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

{{- if .Values.receive.enabled -}}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "thanos.receive.fullname" . }}
  namespace: {{ include "common.names.namespace" . }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.receive.service.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    {{- if eq .Values.receive.mode "dual-mode" }}
    app.kubernetes.io/component: receive-distributor
    {{ else }}
    app.kubernetes.io/component: receive
    {{ end }}
    {{- include "thanos.servicemonitor.matchLabels" . | nindent 4 -}}
  {{- if or .Values.receive.service.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.receive.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.receive.service.type }}
  {{- if and .Values.receive.service.clusterIP (eq .Values.receive.service.type "ClusterIP") }}
  clusterIP: {{ .Values.receive.service.clusterIP }}
  {{- end }}
  {{- if ne .Values.receive.service.type "ClusterIP" }}
  externalTrafficPolicy: {{ .Values.receive.service.externalTrafficPolicy }}
  {{- end }}
  {{- if and .Values.receive.service.loadBalancerIP (eq .Values.receive.service.type "LoadBalancer") }}
  loadBalancerIP: {{ .Values.receive.service.loadBalancerIP }}
  {{- end }}
  {{- if and (eq .Values.receive.service.type "LoadBalancer") .Values.receive.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges: {{- toYaml .Values.receive.service.loadBalancerSourceRanges | nindent 4 }}
  {{- end }}
  {{- if .Values.receive.service.ipFamilyPolicy }}
  ipFamilyPolicy: {{ .Values.receive.service.ipFamilyPolicy }}
  {{- end }}
  {{- if .Values.receive.service.ipFamilies }}
  ipFamilies: {{ toYaml .Values.receive.service.ipFamilies | nindent 4 }}
  {{- end }}
  ports:
    - port: {{ .Values.receive.service.ports.http }}
      targetPort: http
      protocol: TCP
      name: http
      {{- if and (or (eq .Values.receive.service.type "NodePort") (eq .Values.receive.service.type "LoadBalancer")) .Values.receive.service.nodePorts.http }}
      nodePort: {{ .Values.receive.service.nodePorts.http }}
      {{- else if eq .Values.receive.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - port: {{ .Values.receive.service.ports.grpc }}
      targetPort: grpc
      protocol: TCP
      name: grpc
      {{- if and (or (eq .Values.receive.service.type "NodePort") (eq .Values.receive.service.type "LoadBalancer")) .Values.receive.service.nodePorts.grpc }}
      nodePort: {{ .Values.receive.service.nodePorts.grpc }}
      {{- else if eq .Values.receive.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - port: {{ .Values.receive.service.ports.remote }}
      targetPort: remote-write
      protocol: TCP
      name: remote
      {{- if and (or (eq .Values.receive.service.type "NodePort") (eq .Values.receive.service.type "LoadBalancer")) .Values.receive.service.nodePorts.remote }}
      nodePort: {{ .Values.receive.service.nodePorts.remote }}
      {{- else if eq .Values.receive.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - port: {{ .Values.receive.service.ports.capnproto }}
      targetPort: capnproto
      protocol: TCP
      name: capnproto
      {{- if and (or (eq .Values.receive.service.type "NodePort") (eq .Values.receive.service.type "LoadBalancer")) .Values.receive.service.nodePorts.capnproto }}
      nodePort: {{ .Values.receive.service.nodePorts.capnproto }}
      {{- else if eq .Values.receive.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.receive.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.receive.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector:
    {{- if .Values.receive.service.labelSelectorsOverride }}
    {{- include "common.tplvalues.render" (dict "value" .Values.receive.service.labelSelectorsOverride "context" $) | nindent 4 }}
    {{- else }}
    {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.receive.podLabels .Values.commonLabels ) "context" . ) }}
    {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    {{- if eq .Values.receive.mode "dual-mode" }}
    app.kubernetes.io/component: receive-distributor
    {{ else }}
    app.kubernetes.io/component: receive
    {{ end }}
    {{- end }}
{{- end }}
