{{- $Values := (.helm).Values | default .Values }}
{{- $runbookUrl := ($Values.defaultRules).runbookUrl | default "https://runbooks.prometheus-operator.dev/runbooks" }}
{{- $clusterLabel := ($Values.global).clusterLabel | default "cluster" }}
{{- $additionalGroupByLabels := append $Values.defaultRules.additionalGroupByLabels $clusterLabel }}
{{- $groupLabels := join "," $additionalGroupByLabels }}
{{- $grafanaHost := ternary (index (($Values.grafana).ingress).hosts 0) (($Values.external).grafana).host ($Values.grafana).enabled }}
condition: '{{ true }}'
name: kubernetes-system
rules:
- alert: KubeVersionMismatch
  annotations:
    description: 'There are {{`{{`}} $value {{`}}`}} different semantic versions of Kubernetes components running on cluster {{`{{`}} $labels.{{ $clusterLabel }} {{`}}`}}.'
    runbook_url: '{{ $runbookUrl }}/kubernetes/kubeversionmismatch'
    summary: 'Different semantic versions of Kubernetes components running.'
  condition: '{{ true }}'
  expr: count by ({{ $groupLabels }}) (count by (git_version,{{ $groupLabels }}) (label_replace(kubernetes_build_info{job!~"kube-dns|coredns"},"git_version","$1","git_version","(v[0-9]*.[0-9]*).*"))) > 1
  for: 15m
  labels:
    severity: warning
- alert: KubeClientErrors
  annotations:
    description: 'Kubernetes API server client ''{{`{{`}} $labels.job {{`}}`}}/{{`{{`}} $labels.instance {{`}}`}}'' is experiencing {{`{{`}} $value | humanizePercentage {{`}}`}} errors on cluster {{`{{`}} $labels.{{ $clusterLabel }} {{`}}`}}.'
    runbook_url: '{{ $runbookUrl }}/kubernetes/kubeclienterrors'
    summary: 'Kubernetes API server client is experiencing errors.'
  condition: '{{ true }}'
  expr: |-
    (sum(rate(rest_client_requests_total{job="apiserver",code=~"5.."}[5m])) by (instance,job,namespace,{{ $groupLabels }})
      /
    sum(rate(rest_client_requests_total{job="apiserver"}[5m])) by (instance,job,namespace,{{ $groupLabels }}))
    > 0.01
  for: 15m
  labels:
    severity: warning
