{{- 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.labels.standard" . | nindent 4 }}
    app.kubernetes.io/component: kubelet
    {{- if .Values.kubelet.serviceMonitor.labels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.kubelet.serviceMonitor.labels "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  annotations:
    {{- if .Values.commonAnnotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.annotations }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.kubelet.serviceMonitor.annotations "context" $ ) | nindent 4 }}
    {{- end }}
spec:
  jobLabel: k8s-app
  selector:
    matchLabels:
      k8s-app: kubelet
  namespaceSelector:
    matchNames:
      - {{ .Values.kubelet.namespace }}
  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 }}
