
{{- $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: k8s.rules.pod_owner
rules:
- expr: {{ printf "max(label_replace(label_replace(kube_pod_owner{job=\"kube-state-metrics\",owner_kind=\"ReplicaSet\"}, \"replicaset\", \"$1\", \"owner_name\", \"(.*)\") * on(%s,replicaset,namespace) group_left(owner_name) topk(1, max(kube_replicaset_owner{job=\"kube-state-metrics\",owner_kind=\"\"}) by(%s,replicaset,namespace,owner_name)) by(%s,replicaset,namespace), \"workload\", \"$1\", \"replicaset\", \"(.*)\")) by(%s,namespace,workload,pod)" $groupLabels $groupLabels $groupLabels $groupLabels }}
  condition: true
  labels:
    workload_type: replicaset
  record: namespace_workload_pod:kube_pod_owner:relabel
- expr: {{ printf "max(label_replace(label_replace(kube_pod_owner{job=\"kube-state-metrics\",owner_kind=\"ReplicaSet\"}, \"replicaset\", \"$1\", \"owner_name\", \"(.*)\") * on(replicaset,namespace,%s) group_left(owner_name) topk(1, max(kube_replicaset_owner{job=\"kube-state-metrics\",owner_kind=\"Deployment\"}) by(%s,replicaset,namespace,owner_name)) by(%s,replicaset,namespace), \"workload\", \"$1\", \"owner_name\", \"(.*)\")) by(%s,namespace,workload,pod)" $groupLabels $groupLabels $groupLabels $groupLabels }}
  condition: true
  labels:
    workload_type: deployment
  record: namespace_workload_pod:kube_pod_owner:relabel
- expr: {{ printf "max(label_replace(kube_pod_owner{job=\"kube-state-metrics\",owner_kind=\"DaemonSet\"}, \"workload\", \"$1\", \"owner_name\", \"(.*)\")) by(%s,namespace,workload,pod)" $groupLabels }}
  condition: true
  labels:
    workload_type: daemonset
  record: namespace_workload_pod:kube_pod_owner:relabel
- expr: {{ printf "max(label_replace(kube_pod_owner{job=\"kube-state-metrics\",owner_kind=\"StatefulSet\"}, \"workload\", \"$1\", \"owner_name\", \"(.*)\")) by(%s,namespace,workload,pod)" $groupLabels }}
  condition: true
  labels:
    workload_type: statefulset
  record: namespace_workload_pod:kube_pod_owner:relabel
- expr: {{ printf "group(label_join(group(label_join(kube_pod_owner{job=\"kube-state-metrics\",owner_kind=\"Job\"}, \"job_name\", \"\", \"owner_name\")) by(%s,namespace,job_name,pod,owner_name) * on(%s,namespace,job_name) group_left() group(kube_job_owner{job=\"kube-state-metrics\",owner_kind=~\"Pod|\"}) by(%s,namespace,job_name), \"workload\", \"\", \"owner_name\")) by(%s,namespace,workload,pod)" $groupLabels $groupLabels $groupLabels $groupLabels }}
  condition: true
  labels:
    workload_type: job
  record: namespace_workload_pod:kube_pod_owner:relabel
- expr: {{ printf "max(label_replace(kube_pod_owner{job=\"kube-state-metrics\",owner_kind=\"\",owner_name=\"\"}, \"workload\", \"$1\", \"pod\", \"(.+)\")) by(%s,namespace,workload,pod)" $groupLabels }}
  condition: true
  labels:
    workload_type: barepod
  record: namespace_workload_pod:kube_pod_owner:relabel
- expr: {{ printf "max(label_replace(kube_pod_owner{job=\"kube-state-metrics\",owner_kind=\"Node\"}, \"workload\", \"$1\", \"pod\", \"(.+)\")) by(%s,namespace,workload,pod)" $groupLabels }}
  condition: true
  labels:
    workload_type: staticpod
  record: namespace_workload_pod:kube_pod_owner:relabel
- expr: {{ printf "group(label_join(label_join(group(label_join(kube_pod_owner{job=\"kube-state-metrics\",owner_kind=\"Job\"}, \"job_name\", \"\", \"owner_name\")) by(%s,namespace,job_name,pod) * on(%s,namespace,job_name) group_left(owner_kind,owner_name) group(kube_job_owner{job=\"kube-state-metrics\",owner_kind!=\"Pod\",owner_kind!=\"\"}) by(%s,namespace,job_name,owner_kind,owner_name), \"workload\", \"\", \"owner_name\"), \"workload_type\", \"\", \"owner_kind\") or label_replace(label_replace(label_replace(kube_pod_owner{job=\"kube-state-metrics\",owner_kind=\"ReplicaSet\"}, \"replicaset\", \"$1\", \"owner_name\", \"(.+)\") * on(%s,namespace,replicaset) group_left(owner_kind,owner_name) group(kube_replicaset_owner{job=\"kube-state-metrics\",owner_kind!=\"Deployment\",owner_kind!=\"\"}) by(%s,namespace,replicaset,owner_kind,owner_name), \"workload\", \"$1\", \"owner_name\", \"(.+)\") or label_replace(group(kube_pod_owner{job=\"kube-state-metrics\",owner_kind!=\"ReplicaSet\",owner_kind!=\"DaemonSet\",owner_kind!=\"StatefulSet\",owner_kind!=\"Job\",owner_kind!=\"Node\",owner_kind!=\"\"}) by(%s,namespace,pod,owner_name,owner_kind), \"workload\", \"$1\", \"owner_name\", \"(.+)\"), \"workload_type\", \"$1\", \"owner_kind\", \"(.+)\")) by(%s,namespace,workload,workload_type,pod)" $groupLabels $groupLabels $groupLabels $groupLabels $groupLabels $groupLabels $groupLabels }}
  condition: true
  record: namespace_workload_pod:kube_pod_owner:relabel
condition: true
