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

{{- if .Values.kubeProxy.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "kube-prometheus.fullname" . }}-kube-proxy
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.kubeProxy.serviceMonitor.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: kube-proxy
  {{- if or .Values.commonAnnotations .Values.kubeProxy.serviceMonitor.annotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.kubeProxy.serviceMonitor.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.kubeProxy.serviceMonitor.jobLabel }}
  selector:
    matchLabels:
      app.kubernetes.io/component: kube-proxy
  namespaceSelector:
    matchNames:
      - {{ .Values.kubeProxy.namespace }}
  {{- with .Values.kubeProxy.serviceMonitor.sampleLimit }}
  sampleLimit: {{ . }}
  {{- end }}
  endpoints:
    - port: http-metrics
      bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
      {{- if .Values.kubeProxy.serviceMonitor.https }}
      scheme: https
      tlsConfig:
        caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      {{- end }}
      {{- if .Values.kubeProxy.serviceMonitor.interval }}
      interval: {{ .Values.kubeProxy.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.kubeProxy.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{- toYaml .Values.kubeProxy.serviceMonitor.metricRelabelings | nindent 8 }}
      {{- end }}
      {{- if .Values.kubeProxy.serviceMonitor.relabelings }}
      relabelings: {{- toYaml .Values.kubeProxy.serviceMonitor.relabelings | nindent 8 }}
      {{- end }}
{{- end }}
