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

{{- if .Values.operator.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ include "kuberay.operator.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.operator.image "chart" .Chart ) ) }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: kuberay
    app.kubernetes.io/component: operator
  {{- if or .Values.operator.service.annotations .Values.commonAnnotations (and .Values.operator.metrics.enabled .Values.operator.metrics.annotations) }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.operator.service.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: 
    {{- if or .Values.operator.service.annotations .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
    {{- end }}
    {{- if and .Values.operator.metrics.enabled .Values.operator.metrics.annotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.operator.metrics.annotations "context" $) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ .Values.operator.service.type }}
  {{- if and .Values.operator.service.clusterIP (eq .Values.operator.service.type "ClusterIP") }}
  clusterIP: {{ .Values.operator.service.clusterIP }}
  {{- 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 or (eq .Values.operator.service.type "LoadBalancer") (eq .Values.operator.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.operator.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.operator.service.type "LoadBalancer") (not (empty .Values.operator.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.operator.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.operator.service.type "LoadBalancer") (not (empty .Values.operator.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.operator.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http-metrics
      port: {{ .Values.operator.service.ports.metrics }}
      protocol: TCP
      {{- if and (or (eq .Values.operator.service.type "NodePort") (eq .Values.operator.service.type "LoadBalancer")) (not (empty .Values.operator.service.nodePorts.metrics)) }}
      nodePort: {{ .Values.operator.service.nodePorts.metrics }}
      {{- else if eq .Values.operator.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
      targetPort: http-metrics
    {{- if .Values.operator.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.operator.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.operator.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: kuberay
    app.kubernetes.io/component: operator
{{- end }}
