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

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