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

apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-management" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: nessie
    app.kubernetes.io/part-of: nessie
  {{- if and .Values.metrics.enabled .Values.metrics.serviceMonitor.enabled }}
    prometheus.io/scrape: "true"
  {{- end}}
  {{- if or .Values.service.management.annotations .Values.commonAnnotations .Values.metrics.enabled }}
  {{- $metricsDefaultAnnotations := dict
    "prometheus.io/scrape" "true"
    "prometheus.io/port" (toString .Values.service.management.ports.http)
    "prometheus.io/path" "/q/metrics"
  }}
  {{- $annotations := include "common.tplvalues.merge" (dict "values" (list .Values.service.management.annotations .Values.commonAnnotations .Values.metrics.annotations $metricsDefaultAnnotations) "context" .) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.service.management.type }}
  {{- if and .Values.service.management.clusterIP (eq .Values.service.management.type "ClusterIP") }}
  clusterIP: {{ .Values.service.management.clusterIP }}
  {{- end }}
  {{- if .Values.service.management.sessionAffinity }}
  sessionAffinity: {{ .Values.service.management.sessionAffinity }}
  {{- end }}
  {{- if .Values.service.management.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.service.management.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.service.management.type "LoadBalancer") (eq .Values.service.management.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.service.management.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.service.management.type "LoadBalancer") (not (empty .Values.service.management.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.service.management.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.service.management.type "LoadBalancer") (not (empty .Values.service.management.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.service.management.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http-management
      port: {{ .Values.service.management.ports.http }}
      targetPort: http-management
      protocol: TCP
      {{- if and (or (eq .Values.service.management.type "NodePort") (eq .Values.service.management.type "LoadBalancer")) (not (empty .Values.service.management.nodePorts.http)) }}
      nodePort: {{ .Values.service.management.nodePorts.http }}
      {{- else if eq .Values.service.management.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.service.management.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.service.management.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" (dict "values" (list .Values.podLabels .Values.commonLabels) "context" .) | fromYaml }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: nessie
    app.kubernetes.io/part-of: nessie
