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

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