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

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