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


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