{{- if and .Values.kubelet.enabled .Values.kubernetesServiceMonitors.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: {{ template "kube-prometheus-stack.fullname" . }}-kubelet
  {{- if .Values.prometheus.prometheusSpec.ignoreNamespaceSelectors }}
  namespace: {{ .Values.kubelet.namespace }}
  {{- else }}
  namespace: {{ template "kube-prometheus-stack.namespace" . }}
  {{- end }}
  labels:
    app: {{ template "kube-prometheus-stack.name" . }}-kubelet
  {{- with .Values.kubelet.serviceMonitor.additionalLabels }}
    {{- toYaml . | nindent 4 }}
  {{- end }}
{{- include "kube-prometheus-stack.labels" . | indent 4 }}
spec:
  {{- include "servicemonitor.scrapeLimits" .Values.kubelet.serviceMonitor | nindent 2 }}
  {{- with .Values.kubelet.serviceMonitor.attachMetadata }}
  attachMetadata:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  endpoints:
  {{- if .Values.kubelet.serviceMonitor.https }}
  - port: https-metrics
    scheme: https
    {{- if .Values.kubelet.serviceMonitor.interval }}
    interval: {{ .Values.kubelet.serviceMonitor.interval }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.proxyUrl }}
    proxyUrl: {{ .Values.kubelet.serviceMonitor.proxyUrl }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ .Values.kubelet.serviceMonitor.scrapeTimeout }}
    {{- end }}
    tlsConfig:
      caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      insecureSkipVerify: true
    bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
    honorLabels: {{ .Values.kubelet.serviceMonitor.honorLabels }}
    honorTimestamps: {{ .Values.kubelet.serviceMonitor.honorTimestamps }}
{{- if .Values.kubelet.serviceMonitor.metricRelabelings }}
    metricRelabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.metricRelabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.relabelings }}
    relabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.relabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.cAdvisor }}
  - port: https-metrics
    scheme: https
    path: /metrics/cadvisor
    {{- if .Values.kubelet.serviceMonitor.interval }}
    interval: {{ .Values.kubelet.serviceMonitor.interval }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.proxyUrl }}
    proxyUrl: {{ .Values.kubelet.serviceMonitor.proxyUrl }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ .Values.kubelet.serviceMonitor.scrapeTimeout }}
    {{- end }}
    honorLabels: {{ .Values.kubelet.serviceMonitor.honorLabels }}
    honorTimestamps: {{ .Values.kubelet.serviceMonitor.honorTimestamps }}
    tlsConfig:
      caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      insecureSkipVerify: true
    bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
{{- if .Values.kubelet.serviceMonitor.cAdvisorMetricRelabelings }}
    metricRelabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.cAdvisorMetricRelabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.cAdvisorRelabelings }}
    relabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.cAdvisorRelabelings | indent 4) . }}
{{- end }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.probes }}
  - port: https-metrics
    scheme: https
    path: /metrics/probes
    {{- if .Values.kubelet.serviceMonitor.interval }}
    interval: {{ .Values.kubelet.serviceMonitor.interval }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.proxyUrl }}
    proxyUrl: {{ .Values.kubelet.serviceMonitor.proxyUrl }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ .Values.kubelet.serviceMonitor.scrapeTimeout }}
    {{- end }}
    honorLabels: {{ .Values.kubelet.serviceMonitor.honorLabels }}
    honorTimestamps: {{ .Values.kubelet.serviceMonitor.honorTimestamps }}
    tlsConfig:
      caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      insecureSkipVerify: true
    bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
{{- if .Values.kubelet.serviceMonitor.probesMetricRelabelings }}
    metricRelabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.probesMetricRelabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.probesRelabelings }}
    relabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.probesRelabelings | indent 4) . }}
{{- end }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.resource }}
  - port: https-metrics
    scheme: https
    path: {{ .Values.kubelet.serviceMonitor.resourcePath }}
    {{- if .Values.kubelet.serviceMonitor.interval }}
    interval: {{ .Values.kubelet.serviceMonitor.interval }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.proxyUrl }}
    proxyUrl: {{ .Values.kubelet.serviceMonitor.proxyUrl }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ .Values.kubelet.serviceMonitor.scrapeTimeout }}
    {{- end }}
    honorLabels: {{ .Values.kubelet.serviceMonitor.honorLabels }}
    honorTimestamps: {{ .Values.kubelet.serviceMonitor.honorTimestamps }}
    tlsConfig:
      caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
      insecureSkipVerify: true
    bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
{{- if .Values.kubelet.serviceMonitor.resourceMetricRelabelings }}
    metricRelabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.resourceMetricRelabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.resourceRelabelings }}
    relabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.resourceRelabelings | indent 4) . }}
{{- end }}
{{- end }}
  {{- else }}
  - port: http-metrics
    {{- if .Values.kubelet.serviceMonitor.interval }}
    interval: {{ .Values.kubelet.serviceMonitor.interval }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.proxyUrl }}
    proxyUrl: {{ .Values.kubelet.serviceMonitor.proxyUrl }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ .Values.kubelet.serviceMonitor.scrapeTimeout }}
    {{- end }}
    honorLabels: {{ .Values.kubelet.serviceMonitor.honorLabels }}
    honorTimestamps: {{ .Values.kubelet.serviceMonitor.honorTimestamps }}
{{- if .Values.kubelet.serviceMonitor.metricRelabelings }}
    metricRelabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.metricRelabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.relabelings }}
    relabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.relabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.cAdvisor }}
  - port: http-metrics
    path: /metrics/cadvisor
    {{- if .Values.kubelet.serviceMonitor.interval }}
    interval: {{ .Values.kubelet.serviceMonitor.interval }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.proxyUrl }}
    proxyUrl: {{ .Values.kubelet.serviceMonitor.proxyUrl }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ .Values.kubelet.serviceMonitor.scrapeTimeout }}
    {{- end }}
    honorLabels: {{ .Values.kubelet.serviceMonitor.honorLabels }}
    honorTimestamps: {{ .Values.kubelet.serviceMonitor.honorTimestamps }}
{{- if .Values.kubelet.serviceMonitor.cAdvisorMetricRelabelings }}
    metricRelabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.cAdvisorMetricRelabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.cAdvisorRelabelings }}
    relabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.cAdvisorRelabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.probes }}
  - port: http-metrics
    path: /metrics/probes
    {{- if .Values.kubelet.serviceMonitor.interval }}
    interval: {{ .Values.kubelet.serviceMonitor.interval }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.proxyUrl }}
    proxyUrl: {{ .Values.kubelet.serviceMonitor.proxyUrl }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ .Values.kubelet.serviceMonitor.scrapeTimeout }}
    {{- end }}
    honorLabels: {{ .Values.kubelet.serviceMonitor.honorLabels }}
    honorTimestamps: {{ .Values.kubelet.serviceMonitor.honorTimestamps }}
{{- if .Values.kubelet.serviceMonitor.probesMetricRelabelings }}
    metricRelabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.probesMetricRelabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.probesRelabelings }}
    relabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.probesRelabelings | indent 4) . }}
{{- end }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.resource }}
  - port: http-metrics
    path: {{ .Values.kubelet.serviceMonitor.resourcePath }}
    {{- if .Values.kubelet.serviceMonitor.interval }}
    interval: {{ .Values.kubelet.serviceMonitor.interval }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.proxyUrl }}
    proxyUrl: {{ .Values.kubelet.serviceMonitor.proxyUrl }}
    {{- end }}
    {{- if .Values.kubelet.serviceMonitor.scrapeTimeout }}
    scrapeTimeout: {{ .Values.kubelet.serviceMonitor.scrapeTimeout }}
    {{- end }}
    honorLabels: {{ .Values.kubelet.serviceMonitor.honorLabels }}
    honorTimestamps: {{ .Values.kubelet.serviceMonitor.honorTimestamps }}
{{- if .Values.kubelet.serviceMonitor.resourceMetricRelabelings }}
    metricRelabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.resourceMetricRelabelings | indent 4) . }}
{{- end }}
{{- if .Values.kubelet.serviceMonitor.resourceRelabelings }}
    relabelings:
{{ tpl (toYaml .Values.kubelet.serviceMonitor.resourceRelabelings | indent 4) . }}
{{- end }}
{{- end }}
{{- end }}
  {{- end }}
  jobLabel: k8s-app
  namespaceSelector:
    matchNames:
    - {{ .Values.kubelet.namespace }}
  selector:
    matchLabels:
      app.kubernetes.io/name: kubelet
      k8s-app: kubelet
{{- end}}
