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

{{- if .Values.agent.enabled }}
{{- if .Values.agent.metrics.enabled }}
apiVersion: v1
kind: Service
metadata:
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: agent
  name: {{ template "common.names.fullname" . }}-agent-metrics
  namespace: {{ .Release.Namespace | quote }}
  {{- if or .Values.agent.service.annotations .Values.commonAnnotations (and .Values.agent.metrics.enabled .Values.agent.metrics.annotations) }}
  {{- $annotations := dict -}}
  {{- if or .Values.agent.service.annotations .Values.commonAnnotations }}
  {{- $annotations = merge $annotations .Values.agent.service.annotations .Values.commonAnnotations -}}
  {{- end }}
  {{- if and .Values.agent.metrics.enabled .Values.agent.metrics.annotations }}
  {{- $annotations = merge $annotations .Values.agent.metrics.annotations }}
  {{- end }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $) | nindent 4 }}
  {{- end }}
spec:
  type: {{ .Values.agent.service.type }}
  {{- if and .Values.agent.service.clusterIP (eq .Values.agent.service.type "ClusterIP") }}
  clusterIP: {{ .Values.agent.service.clusterIP }}
  {{- end }}
  {{- if (or (eq .Values.agent.service.type "LoadBalancer") (eq .Values.agent.service.type "NodePort")) }}
  externalTrafficPolicy: {{ .Values.agent.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if eq .Values.agent.service.type "LoadBalancer" }}
  loadBalancerSourceRanges: {{ .Values.agent.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if (and (eq .Values.agent.service.type "LoadBalancer") (not (empty .Values.agent.service.loadBalancerIP))) }}
  loadBalancerIP: {{ .Values.agent.service.loadBalancerIP }}
  {{- end }}
  {{- if .Values.agent.service.sessionAffinity }}
  sessionAffinity: {{ .Values.agent.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.agent.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.agent.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  ports:
    - name: metrics
      port: {{ .Values.agent.metrics.port }}
      targetPort: metrics
      protocol: TCP
      {{- if (and (or (eq .Values.agent.service.type "NodePort") (eq .Values.agent.service.type "LoadBalancer")) (not (empty .Values.agent.service.nodePorts.metrics))) }}
      nodePort: {{ .Values.agent.service.nodePorts.metrics }}
      {{- else if eq .Values.agent.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.agent.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.agent.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.agent.podLabels .Values.commonLabels ) "context" . ) }}
  selector: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: agent
{{- end }}
{{- end }}
