apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-kubernets-monitoring-rules
  namespace: infra
data:
  # THIS FILE WAS GENERATED FROM https://github.com/kubernetes-monitoring/kubernetes-mixin
  prometheus_rules.yml: |
    "groups":
      - "name": "k8s.rules"
        "rules":
        - "expr": |
            sum(rate(container_cpu_usage_seconds_total{job="cadvisor", image!="", container!="POD"}[5m])) by (namespace)
          "record": "namespace:container_cpu_usage_seconds_total:sum_rate"
        - "expr": |
            sum by (namespace, pod, container) (
              rate(container_cpu_usage_seconds_total{job="cadvisor", image!="", container!="POD"}[5m])
            )
          "record": "namespace_pod_container:container_cpu_usage_seconds_total:sum_rate"
        - "expr": |
            sum(container_memory_usage_bytes{job="cadvisor", image!="", container!="POD"}) by (namespace)
          "record": "namespace:container_memory_usage_bytes:sum"
        - "expr": |
            sum by (namespace, label_name) (
                sum(kube_pod_container_resource_requests_memory_bytes{job="kube-state-metrics"} * on (endpoint, instance, job, namespace, pod, service) group_left(phase) (kube_pod_status_phase{phase=~"^(Pending|Running)$"} == 1)) by (namespace, pod)
              * on (namespace, pod)
                group_left(label_name) kube_pod_labels{job="kube-state-metrics"}
            )
          "record": "namespace:kube_pod_container_resource_requests_memory_bytes:sum"
        - "expr": |
            sum by (namespace, label_name) (
                sum(kube_pod_container_resource_requests_cpu_cores{job="kube-state-metrics"} * on (endpoint, instance, job, namespace, pod, service) group_left(phase) (kube_pod_status_phase{phase=~"^(Pending|Running)$"} == 1)) by (namespace, pod)
              * on (namespace, pod)
                group_left(label_name) kube_pod_labels{job="kube-state-metrics"}
            )
          "record": "namespace:kube_pod_container_resource_requests_cpu_cores:sum"
        - "expr": |
            sum(
              label_replace(
                label_replace(
                  kube_pod_owner{job="kube-state-metrics", owner_kind="ReplicaSet"},
                  "replicaset", "$1", "owner_name", "(.*)"
                ) * on(replicaset, namespace) group_left(owner_name) kube_replicaset_owner{job="kube-state-metrics"},
                "workload", "$1", "owner_name", "(.*)"
              )
            ) by (namespace, workload, pod)
          "labels":
            "workload_type": "deployment"
          "record": "mixin_pod_workload"
        - "expr": |
            sum(
              label_replace(
                kube_pod_owner{job="kube-state-metrics", owner_kind="DaemonSet"},
                "workload", "$1", "owner_name", "(.*)"
              )
            ) by (namespace, workload, pod)
          "labels":
            "workload_type": "daemonset"
          "record": "mixin_pod_workload"
        - "expr": |
            sum(
              label_replace(
                kube_pod_owner{job="kube-state-metrics", owner_kind="StatefulSet"},
                "workload", "$1", "owner_name", "(.*)"
              )
            ) by (namespace, workload, pod)
          "labels":
            "workload_type": "statefulset"
          "record": "mixin_pod_workload"
      - "name": "kube-scheduler.rules"
        "rules":
        - "expr": |
            histogram_quantile(0.99, sum(rate(scheduler_e2e_scheduling_duration_seconds_bucket{job="kube-scheduler"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.99"
          "record": "cluster_quantile:scheduler_e2e_scheduling_duration_seconds:histogram_quantile"
        - "expr": |
            histogram_quantile(0.99, sum(rate(scheduler_scheduling_algorithm_duration_seconds_bucket{job="kube-scheduler"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.99"
          "record": "cluster_quantile:scheduler_scheduling_algorithm_duration_seconds:histogram_quantile"
        - "expr": |
            histogram_quantile(0.99, sum(rate(scheduler_binding_duration_seconds_bucket{job="kube-scheduler"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.99"
          "record": "cluster_quantile:scheduler_binding_duration_seconds:histogram_quantile"
        - "expr": |
            histogram_quantile(0.9, sum(rate(scheduler_e2e_scheduling_duration_seconds_bucket{job="kube-scheduler"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.9"
          "record": "cluster_quantile:scheduler_e2e_scheduling_duration_seconds:histogram_quantile"
        - "expr": |
            histogram_quantile(0.9, sum(rate(scheduler_scheduling_algorithm_duration_seconds_bucket{job="kube-scheduler"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.9"
          "record": "cluster_quantile:scheduler_scheduling_algorithm_duration_seconds:histogram_quantile"
        - "expr": |
            histogram_quantile(0.9, sum(rate(scheduler_binding_duration_seconds_bucket{job="kube-scheduler"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.9"
          "record": "cluster_quantile:scheduler_binding_duration_seconds:histogram_quantile"
        - "expr": |
            histogram_quantile(0.5, sum(rate(scheduler_e2e_scheduling_duration_seconds_bucket{job="kube-scheduler"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.5"
          "record": "cluster_quantile:scheduler_e2e_scheduling_duration_seconds:histogram_quantile"
        - "expr": |
            histogram_quantile(0.5, sum(rate(scheduler_scheduling_algorithm_duration_seconds_bucket{job="kube-scheduler"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.5"
          "record": "cluster_quantile:scheduler_scheduling_algorithm_duration_seconds:histogram_quantile"
        - "expr": |
            histogram_quantile(0.5, sum(rate(scheduler_binding_duration_seconds_bucket{job="kube-scheduler"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.5"
          "record": "cluster_quantile:scheduler_binding_duration_seconds:histogram_quantile"
      - "name": "kube-apiserver.rules"
        "rules":
        - "expr": |
            histogram_quantile(0.99, sum(rate(apiserver_request_duration_seconds_bucket{job="kube-apiserver"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.99"
          "record": "cluster_quantile:apiserver_request_duration_seconds:histogram_quantile"
        - "expr": |
            histogram_quantile(0.9, sum(rate(apiserver_request_duration_seconds_bucket{job="kube-apiserver"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.9"
          "record": "cluster_quantile:apiserver_request_duration_seconds:histogram_quantile"
        - "expr": |
            histogram_quantile(0.5, sum(rate(apiserver_request_duration_seconds_bucket{job="kube-apiserver"}[5m])) without(instance, pod))
          "labels":
            "quantile": "0.5"
          "record": "cluster_quantile:apiserver_request_duration_seconds:histogram_quantile"
      - "name": "node.rules"
        "rules":
        - "expr": "sum(min(kube_pod_info) by (node))"
          "record": ":kube_pod_info_node_count:"
        - "expr": |
            max(label_replace(kube_pod_info{job="kube-state-metrics"}, "pod", "$1", "pod", "(.*)")) by (node, namespace, pod)
          "record": "node_namespace_pod:kube_pod_info:"
        - "expr": |
            count by (node) (sum by (node, cpu) (
              node_cpu_seconds_total{job="node-exporter"}
            * on (namespace, pod) group_left(node)
              node_namespace_pod:kube_pod_info:
            ))
          "record": "node:node_num_cpu:sum"
        - "expr": |
            sum(node_memory_MemFree_bytes{job="node-exporter"} + node_memory_Cached_bytes{job="node-exporter"} + node_memory_Buffers_bytes{job="node-exporter"})
          "record": ":node_memory_MemFreeCachedBuffers_bytes:sum"
      