{{- if .Values.metrics.enabled }}
apiVersion: v1
kind: Service
metadata:
  name: {{ printf "%s-metrics" (include "dialCore.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{ include "dialCore.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if .Values.labels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.labels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if or .Values.metrics.service.annotations .Values.commonAnnotations }}
  annotations:
    {{- if .Values.metrics.service.annotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.metrics.service.annotations "context" $) | nindent 4 }}
    {{- end }}
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
  {{- end }}
spec:
  type: {{ .Values.metrics.service.type }}
  {{- if and .Values.metrics.service.clusterIP (eq .Values.metrics.service.type "ClusterIP") }}
  clusterIP: {{ .Values.metrics.service.clusterIP }}
  {{- end }}
  {{- if .Values.metrics.service.sessionAffinity }}
  sessionAffinity: {{ .Values.metrics.service.sessionAffinity }}
  {{- end }}
  {{- if .Values.metrics.service.sessionAffinityConfig }}
  sessionAffinityConfig: {{- include "common.tplvalues.render" (dict "value" .Values.metrics.service.sessionAffinityConfig "context" $) | nindent 4 }}
  {{- end }}
  {{- if or (eq .Values.metrics.service.type "LoadBalancer") (eq .Values.metrics.service.type "NodePort") }}
  externalTrafficPolicy: {{ .Values.metrics.service.externalTrafficPolicy | quote }}
  {{- end }}
  {{- if and (eq .Values.metrics.service.type "LoadBalancer") (not (empty .Values.metrics.service.loadBalancerSourceRanges)) }}
  loadBalancerSourceRanges: {{ .Values.metrics.service.loadBalancerSourceRanges }}
  {{- end }}
  {{- if and (eq .Values.metrics.service.type "LoadBalancer") (not (empty .Values.metrics.service.loadBalancerIP)) }}
  loadBalancerIP: {{ .Values.metrics.service.loadBalancerIP }}
  {{- end }}
  ports:
    - name: http-metrics
      targetPort: http-metrics
      port: {{ .Values.metrics.service.ports.http }}
      protocol: TCP
      {{- if (and (or (eq .Values.metrics.service.type "NodePort") (eq .Values.metrics.service.type "LoadBalancer")) (not (empty .Values.metrics.service.nodePorts.http))) }}
      nodePort: {{ .Values.metrics.service.nodePorts.http }}
      {{- else if eq .Values.metrics.service.type "ClusterIP" }}
      nodePort: null
      {{- end }}
    {{- if .Values.metrics.service.extraPorts }}
    {{- include "common.tplvalues.render" (dict "value" .Values.metrics.service.extraPorts "context" $) | nindent 4 }}
    {{- end }}
  selector: {{ include "dialCore.labels.matchLabels" . | nindent 4 }}
{{- end }}
