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

{{- if .Values.operator.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ template "kube-prometheus.operator.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "kube-prometheus.operator.labels" . | nindent 4 }}
    {{- if .Values.operator.service.labels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.operator.service.labels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if or .Values.commonAnnotations .Values.operator.service.annotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.operator.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.operator.service.type }}
  {{- if and (eq .Values.operator.service.type "LoadBalancer") (not (empty .Values.operator.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.operator.service.loadBalancerIP }}
  {{- end }}
  {{- if and (eq .Values.operator.service.type "LoadBalancer") (not (empty .Values.operator.service.loadBalancerClass)) }}
  loadBalancerClass: {{ .Values.operator.service.loadBalancerClass }}
  {{- end }}
  {{- if and (eq .Values.operator.service.type "LoadBalancer") (not (empty .Values.operator.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.operator.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if or (eq .Values.operator.service.type "LoadBalancer") (eq .Values.operator.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.operator.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if .Values.operator.service.sessionAffinity }}
  sessionAffinity: {{ .Values.operator.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.operator.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.operator.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if and (eq .Values.operator.service.type "LoadBalancer") (eq .Values.operator.service.externalTrafficPolicy "Local") .Values.operator.service.healthCheckNodePort }}
  healthCheckNodePort: {{ .Values.operator.service.healthCheckNodePort }}
  {{- end }}
  {{- if and .Values.operator.service.clusterIP (eq .Values.operator.service.type "ClusterIP") }}
  clusterIP: {{ .Values.operator.service.clusterIP }}
  {{- end }}
  ports:
    - name: http
      port: {{ .Values.operator.service.ports.http }}
      targetPort: http
      {{- if and .Values.operator.service.nodePorts.http (or (eq .Values.operator.service.type "NodePort") (eq .Values.operator.service.type "LoadBalancer")) }}
      nodePort: {{ .Values.operator.service.nodePorts.http }}
      {{- end }}
    {{- if .Values.operator.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.operator.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector: {{- include "kube-prometheus.operator.matchLabels" . | nindent 4 }}
{{- end }}
