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

{{- if .Values.kubelet.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "kube-prometheus.fullname" . }}-kubelet
  namespace: {{ include "common.names.namespace" . | quote }}
  {{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.kubelet.serviceMonitor.labels .Values.commonLabels ) "context" . ) }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: kubelet
  {{- if or .Values.commonAnnotations .Values.kubelet.serviceMonitor.annotations }}
  {{- $annotations := include "common.tplvalues.merge" ( dict "values" ( list .Values.kubelet.serviceMonitor.annotations .Values.commonAnnotations ) "context" . ) }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $annotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  jobLabel: {{ .Values.kubelet.serviceMonitor.jobLabel }}
  selector:
    matchLabels:
      k8s-app: kubelet
  namespaceSelector:
    matchNames:
      - {{ .Values.kubelet.namespace }}
  {{- with .Values.kubelet.serviceMonitor.sampleLimit }}
  sampleLimit: {{ . }}
  {{- end }}
  endpoints:
      {{- $scheme := ternary "https" "http" .Values.kubelet.serviceMonitor.https }}
    - port: {{ printf "%s-metrics" $scheme }}
      scheme: {{$scheme}}
      {{- if .Values.kubelet.serviceMonitor.https }}
      tlsConfig:
        caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        serverName: kubernetes
        insecureSkipVerify: true
      bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
      {{- else }}
      tlsConfig:
        insecureSkipVerify: false
      {{- end }}
      honorLabels: true
      {{- if .Values.kubelet.serviceMonitor.interval }}
      interval: {{ .Values.kubelet.serviceMonitor.interval }}
      {{- end }}
      {{- if .Values.kubelet.serviceMonitor.metricRelabelings }}
      metricRelabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.kubelet.serviceMonitor.metricRelabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.kubelet.serviceMonitor.relabelings }}
      relabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.kubelet.serviceMonitor.relabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.kubelet.serviceMonitor.cAdvisor }}
    - port: {{ printf "%s-metrics" $scheme }}
      scheme: {{$scheme}}
      path: /metrics/cadvisor
      {{- if .Values.kubelet.serviceMonitor.interval }}
      interval: {{ .Values.kubelet.serviceMonitor.interval }}
      {{- end }}
      honorLabels: true
      {{- if .Values.kubelet.serviceMonitor.https }}
      tlsConfig:
        caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        serverName: kubernetes
        insecureSkipVerify: true
      bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
      {{- else }}
      tlsConfig:
        insecureSkipVerify: false
      {{- end }}
      {{- end }}
      {{- if .Values.kubelet.serviceMonitor.cAdvisorMetricRelabelings }}
      metricRelabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.kubelet.serviceMonitor.cAdvisorMetricRelabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.kubelet.serviceMonitor.cAdvisorRelabelings }}
      relabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.kubelet.serviceMonitor.cAdvisorRelabelings "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.kubelet.serviceMonitor.resource }}
    - port: {{ printf "%s-metrics" $scheme }}
      scheme: {{$scheme}}
      path: {{ .Values.kubelet.serviceMonitor.resourcePath }}
      {{- if .Values.kubelet.serviceMonitor.https }}
      tlsConfig:
        caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        serverName: kubernetes
        insecureSkipVerify: true
      bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
      {{- else }}
      tlsConfig:
        insecureSkipVerify: false
      {{- end }}
      {{- if .Values.kubelet.serviceMonitor.resourceRelabelings }}
      relabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.kubelet.serviceMonitor.resourceRelabelings "context" .) | nindent 4 }}
      {{- end }}
      {{- if .Values.kubelet.serviceMonitor.resourceMetricRelabelings }}
      metricRelabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.kubelet.serviceMonitor.resourceMetricRelabelings "context" .) | nindent 4 }}
      {{- end }}
      {{- end }}
{{- end }}
