
{{- 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 }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: proxy
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  annotations:
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if .Values.proxy.service.metrics.annotations }}
    {{- include "common.tplvalues.render" (dict "value" .Values.proxy.service.metrics.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: {{ coalesce .Values.proxy.service.metrics.ports.http .Values.proxy.service.metrics.port }}
      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 }}
  selector: {{- include "common.labels.matchLabels" . | nindent 4 }}
    app.kubernetes.io/component: proxy
{{- end }}
