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

apiVersion: v1
kind: Service
metadata:
  name:  {{ include "jaeger.collector.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: jaeger
    app.kubernetes.io/component: collector
  {{- if or .Values.collector.service.annotations .Values.collector.service.metrics.annotations .Values.commonAnnotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.collector.service.annotations .Values.collector.service.metrics.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.collector.service.type }}
  {{- if and (eq .Values.collector.service.type "LoadBalancer") (not (empty .Values.collector.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.collector.service.loadBalancerIP }}
  {{- end }}
  {{- if and (eq .Values.collector.service.type "LoadBalancer") (not (empty .Values.collector.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.collector.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and .Values.collector.service.clusterIP (eq .Values.collector.service.type "ClusterIP") }}
  clusterIP: {{ .Values.collector.service.clusterIP }}
  {{- end }}
  {{- if .Values.collector.service.sessionAffinity }}
  sessionAffinity: {{ .Values.collector.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.collector.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.collector.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.collector.service.type "LoadBalancer") (eq .Values.collector.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.collector.service.externalTrafficPolicy | quote }}
  {{- end }}
  ports:
    {{- if has "otlp" .Values.collector.receivers }}
    - name: otlp-grpc
      port: {{ .Values.collector.service.ports.otlp.grpc }}
      targetPort: {{ .Values.collector.containerPorts.otlp.grpc }}
      {{- if and (or (eq .Values.collector.service.type "NodePort") (eq .Values.collector.service.type "LoadBalancer")) (not (empty .Values.collector.service.nodePorts.otlp.grpc)) }}
      nodePort: {{ .Values.collector.service.nodePorts.otlp.grpc }}
      {{- else if eq .Values.collector.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - name: otlp-http
      port: {{ .Values.collector.service.ports.otlp.http }}
      targetPort: {{ .Values.collector.containerPorts.otlp.http }}
      {{- if and (or (eq .Values.collector.service.type "NodePort") (eq .Values.collector.service.type "LoadBalancer")) (not (empty .Values.collector.service.nodePorts.otlp.http)) }}
      nodePort: {{ .Values.collector.service.nodePorts.otlp.http }}
      {{- else if eq .Values.collector.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- end }}
    {{- if has "jaeger" .Values.collector.receivers }}
    - name: jaeger-grpc
      port: {{ .Values.collector.service.ports.jaeger.grpc }}
      targetPort: {{ .Values.collector.containerPorts.jaeger.grpc }}
      {{- if and (or (eq .Values.collector.service.type "NodePort") (eq .Values.collector.service.type "LoadBalancer")) (not (empty .Values.collector.service.nodePorts.jaeger.grpc)) }}
      nodePort: {{ .Values.collector.service.nodePorts.jaeger.grpc }}
      {{- else if eq .Values.collector.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    - name: jaeger-http-thrift
      port: {{ .Values.collector.service.ports.jaeger.thrift_http }}
      targetPort: {{ .Values.collector.containerPorts.jaeger.thrift_http }}
      {{- if and (or (eq .Values.collector.service.type "NodePort") (eq .Values.collector.service.type "LoadBalancer")) (not (empty .Values.collector.service.nodePorts.jaeger.thrift_http)) }}
      nodePort: {{ .Values.collector.service.nodePorts.jaeger.thrift_http }}
      {{- else if eq .Values.collector.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- end }}
    {{- if has "zipkin" .Values.collector.receivers }}
    - name: zipkin
      port: {{ .Values.collector.service.ports.zipkin }}
      targetPort: {{ .Values.collector.containerPorts.zipkin }}
      {{- if and (or (eq .Values.collector.service.type "NodePort") (eq .Values.collector.service.type "LoadBalancer")) (not (empty .Values.collector.service.nodePorts.zipkin)) }}
      nodePort: {{ .Values.collector.service.nodePorts.zipkin }}
      {{- else if eq .Values.collector.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- end }}
    - name: metrics
      port: {{ .Values.collector.service.ports.metrics }}
      targetPort: {{ .Values.collector.containerPorts.metrics }}
      {{- if and (or (eq .Values.collector.service.type "NodePort") (eq .Values.collector.service.type "LoadBalancer")) (not (empty .Values.collector.service.nodePorts.metrics)) }}
      nodePort: {{ .Values.collector.service.nodePorts.metrics }}
      {{- else if eq .Values.collector.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.collector.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.collector.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.collector.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: jaeger
    app.kubernetes.io/component: collector
