{{- if .Values.controller.metrics.enabled }}
apiVersion: v1
kind: Service
metadata:
{{- if .Values.controller.metrics.service.annotations }}
  annotations:
  {{- range $key, $value := .Values.controller.metrics.service.annotations }}
    {{ $key }}: {{ $value | quote }}
  {{- end }}
{{- end }}
  labels:
{{- if .Values.controller.metrics.service.labels }}
{{ toYaml .Values.controller.metrics.service.labels | indent 4 }}
{{- end }}
    app: {{ template "nginx-ingress.name" . }}
    chart: {{ template "nginx-ingress.chart" . }}
    component: "{{ .Values.controller.name }}"
    heritage: {{ .Release.Service }}
    release: {{ template "nginx-ingress.releaseLabel" . }}
  name: {{ template "nginx-ingress.controller.fullname" . }}-metrics
spec:
{{- if not .Values.controller.metrics.service.omitClusterIP }}
  {{- with .Values.controller.metrics.service.clusterIP }}
  clusterIP: {{ if eq "-" . }}""{{ else }}{{ . | quote }}{{ end }}
  {{- end }}
{{- end }}
{{- if .Values.controller.metrics.service.externalIPs }}
  externalIPs:
{{ toYaml .Values.controller.metrics.service.externalIPs | indent 4 }}
{{- end }}
{{- if .Values.controller.metrics.service.loadBalancerIP }}
  loadBalancerIP: "{{ .Values.controller.metrics.service.loadBalancerIP }}"
{{- end }}
{{- if .Values.controller.metrics.service.loadBalancerSourceRanges }}
  loadBalancerSourceRanges:
{{ toYaml .Values.controller.metrics.service.loadBalancerSourceRanges | indent 4 }}
{{- end }}
  ports:
    - name: metrics
      port: {{ .Values.controller.metrics.service.servicePort }}
      targetPort: metrics
  selector:
    app: {{ template "nginx-ingress.name" . }}
    release: {{ template "nginx-ingress.releaseLabel" . }}
    {{ .Values.controller.componentLabelKeyOverride | default "app.kubernetes.io/component" }}: controller
  type: "{{ .Values.controller.metrics.service.type }}"
{{- end }}
