
{{- $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 "," (uniq $additionalGroupByLabels) }}
{{- $grafanaAddr := include "vm-k8s-stack.grafana.addr" . }}
name: kube-apiserver-availability.rules
rules:
- expr: (avg_over_time(code_verb:apiserver_request_total:increase1h[30d]) * 24) * 30
  condition: true
  record: code_verb:apiserver_request_total:increase30d
- expr: {{ printf "sum(code_verb:apiserver_request_total:increase30d{verb=~\"LIST|GET\"}) by(%s,code)" $groupLabels }}
  condition: true
  labels:
    verb: read
  record: code:apiserver_request_total:increase30d
- expr: {{ printf "sum(code_verb:apiserver_request_total:increase30d{verb=~\"POST|PUT|PATCH|DELETE\"}) by(%s,code)" $groupLabels }}
  condition: true
  labels:
    verb: write
  record: code:apiserver_request_total:increase30d
- expr: {{ printf "sum(increase(apiserver_request_sli_duration_seconds_bucket[1h])) by(%s,verb,scope,le)" $groupLabels }}
  condition: true
  record: cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase1h
- expr: {{ printf "sum((avg_over_time(cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase1h[30d]) * 24) * 30) by(%s,verb,scope,le)" $groupLabels }}
  condition: true
  record: cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase30d
- expr: {{ printf "sum(cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase1h{le=\"+Inf\"}) by(%s,verb,scope)" $groupLabels }}
  condition: true
  record: cluster_verb_scope:apiserver_request_sli_duration_seconds_count:increase1h
- expr: {{ printf "sum(cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase30d{le=\"+Inf\"}) by(%s,verb,scope)" $groupLabels }}
  condition: true
  record: cluster_verb_scope:apiserver_request_sli_duration_seconds_count:increase30d
- expr: {{ printf "1 - ((((sum(cluster_verb_scope:apiserver_request_sli_duration_seconds_count:increase30d{verb=~\"POST|PUT|PATCH|DELETE\"}) by(%s) - sum(cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase30d{verb=~\"POST|PUT|PATCH|DELETE\",le=~\"1(\\\\.0)?\"} or vector(0)) by(%s)) + (sum(cluster_verb_scope:apiserver_request_sli_duration_seconds_count:increase30d{verb=~\"LIST|GET\"}) by(%s) - ((sum(cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase30d{verb=~\"LIST|GET\",scope=~\"resource|\",le=~\"1(\\\\.0)?\"} or vector(0)) by(%s) + sum(cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase30d{verb=~\"LIST|GET\",scope=\"namespace\",le=~\"5(\\\\.0)?\"} or vector(0)) by(%s)) + sum(cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase30d{verb=~\"LIST|GET\",scope=\"cluster\",le=~\"30(\\\\.0)?\"} or vector(0)) by(%s)))) + sum(code:apiserver_request_total:increase30d{code=~\"5..\"} or vector(0)) by(%s)) / sum(code:apiserver_request_total:increase30d) by(%s))" $groupLabels $groupLabels $groupLabels $groupLabels $groupLabels $groupLabels $groupLabels $groupLabels }}
  condition: true
  labels:
    verb: all
  record: apiserver_request:availability30d
- expr: {{ printf "1 - (((sum(cluster_verb_scope:apiserver_request_sli_duration_seconds_count:increase30d{verb=~\"LIST|GET\"}) by(%s) - ((sum(cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase30d{verb=~\"LIST|GET\",scope=~\"resource|\",le=~\"1(\\\\.0)?\"} or vector(0)) by(%s) + sum(cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase30d{verb=~\"LIST|GET\",scope=\"namespace\",le=~\"5(\\\\.0)?\"} or vector(0)) by(%s)) + sum(cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase30d{verb=~\"LIST|GET\",scope=\"cluster\",le=~\"30(\\\\.0)?\"} or vector(0)) by(%s))) + sum(code:apiserver_request_total:increase30d{verb=\"read\",code=~\"5..\"} or vector(0)) by(%s)) / sum(code:apiserver_request_total:increase30d{verb=\"read\"}) by(%s))" $groupLabels $groupLabels $groupLabels $groupLabels $groupLabels $groupLabels }}
  condition: true
  labels:
    verb: read
  record: apiserver_request:availability30d
- expr: {{ printf "1 - (((sum(cluster_verb_scope:apiserver_request_sli_duration_seconds_count:increase30d{verb=~\"POST|PUT|PATCH|DELETE\"}) by(%s) - sum(cluster_verb_scope_le:apiserver_request_sli_duration_seconds_bucket:increase30d{verb=~\"POST|PUT|PATCH|DELETE\",le=~\"1(\\\\.0)?\"} or vector(0)) by(%s)) + sum(code:apiserver_request_total:increase30d{verb=\"write\",code=~\"5..\"} or vector(0)) by(%s)) / sum(code:apiserver_request_total:increase30d{verb=\"write\"}) by(%s))" $groupLabels $groupLabels $groupLabels $groupLabels }}
  condition: true
  labels:
    verb: write
  record: apiserver_request:availability30d
- expr: {{ printf "sum(rate(apiserver_request_total{job=\"apiserver\",verb=~\"LIST|GET\"}[5m])) by(%s,code,resource)" $groupLabels }}
  condition: true
  labels:
    verb: read
  record: code_resource:apiserver_request_total:rate5m
- expr: {{ printf "sum(rate(apiserver_request_total{job=\"apiserver\",verb=~\"POST|PUT|PATCH|DELETE\"}[5m])) by(%s,code,resource)" $groupLabels }}
  condition: true
  labels:
    verb: write
  record: code_resource:apiserver_request_total:rate5m
- expr: {{ printf "sum(increase(apiserver_request_total{job=\"apiserver\",verb=~\"LIST|GET|POST|PUT|PATCH|DELETE\",code=~\"2..\"}[1h])) by(%s,code,verb)" $groupLabels }}
  condition: true
  record: code_verb:apiserver_request_total:increase1h
- expr: {{ printf "sum(increase(apiserver_request_total{job=\"apiserver\",verb=~\"LIST|GET|POST|PUT|PATCH|DELETE\",code=~\"3..\"}[1h])) by(%s,code,verb)" $groupLabels }}
  condition: true
  record: code_verb:apiserver_request_total:increase1h
- expr: {{ printf "sum(increase(apiserver_request_total{job=\"apiserver\",verb=~\"LIST|GET|POST|PUT|PATCH|DELETE\",code=~\"4..\"}[1h])) by(%s,code,verb)" $groupLabels }}
  condition: true
  record: code_verb:apiserver_request_total:increase1h
- expr: {{ printf "sum(increase(apiserver_request_total{job=\"apiserver\",verb=~\"LIST|GET|POST|PUT|PATCH|DELETE\",code=~\"5..\"}[1h])) by(%s,code,verb)" $groupLabels }}
  condition: true
  record: code_verb:apiserver_request_total:increase1h
condition: {{ ($Values.kubeApiServer).enabled }}
interval: 3m
