{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and .Values.prometheus.enabled .Values.prometheus.defaultRules.create .Values.prometheus.defaultRules.rules.k8sPodOwner }}
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: {{ printf "%s-k8s.rules.pod-owner" (include "kube-prometheus.prometheus.fullname" .) }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{ include "kube-prometheus.prometheus.labels" . | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" . ) | nindent 4 }}
  {{- end }}
spec:
  groups:
    - name: k8s.rules.pod_owner
      rules:
        - expr: |-
            max by (cluster, namespace, workload, pod) (
              label_replace(
                label_replace(
                  kube_pod_owner{job="kube-state-metrics", owner_kind="ReplicaSet"},
                  "replicaset", "$1", "owner_name", "(.*)"
                ) * on (cluster, replicaset, namespace) group_left(owner_name) topk by (cluster, replicaset, namespace) (
                  1, max by (cluster, replicaset, namespace, owner_name) (
                    kube_replicaset_owner{job="kube-state-metrics", owner_kind=""}
                  )
                ),
                "workload", "$1", "replicaset", "(.*)"
              )
            )
          labels:
            workload_type: replicaset
          record: namespace_workload_pod:kube_pod_owner:relabel
        - expr: |-
            max by (cluster, namespace, workload, pod) (
              label_replace(
                label_replace(
                  kube_pod_owner{job="kube-state-metrics", owner_kind="ReplicaSet"},
                  "replicaset", "$1", "owner_name", "(.*)"
                ) * on (replicaset, namespace, cluster) group_left(owner_name) topk by (cluster, replicaset, namespace) (
                  1, max by (cluster, replicaset, namespace, owner_name) (
                    kube_replicaset_owner{job="kube-state-metrics", owner_kind="Deployment"}
                  )
                ),
                "workload", "$1", "owner_name", "(.*)"
              )
            )
          labels:
            workload_type: deployment
          record: namespace_workload_pod:kube_pod_owner:relabel
        - expr: |-
            max by (cluster, namespace, workload, pod) (
              label_replace(
                kube_pod_owner{job="kube-state-metrics", owner_kind="DaemonSet"},
                "workload", "$1", "owner_name", "(.*)"
              )
            )
          labels:
            workload_type: daemonset
          record: namespace_workload_pod:kube_pod_owner:relabel
        - expr: |-
            max by (cluster, namespace, workload, pod) (
              label_replace(
                kube_pod_owner{job="kube-state-metrics", owner_kind="StatefulSet"},
              "workload", "$1", "owner_name", "(.*)")
            )
          labels:
            workload_type: statefulset
          record: namespace_workload_pod:kube_pod_owner:relabel
        - expr: |-
            group by (cluster, namespace, workload, pod) (
              label_join(
                group by (cluster, namespace, job_name, pod, owner_name) (
                  label_join(
                    kube_pod_owner{job="kube-state-metrics", owner_kind="Job"}
                  , "job_name", "", "owner_name")
                )
                * on (cluster, namespace, job_name) group_left()
                group by (cluster, namespace, job_name) (
                  kube_job_owner{job="kube-state-metrics", owner_kind=~"Pod|"}
                )
              , "workload", "", "owner_name")
            )
          labels:
            workload_type: job
          record: namespace_workload_pod:kube_pod_owner:relabel
        - expr: |-
            max by (cluster, namespace, workload, pod) (
              label_replace(
                kube_pod_owner{job="kube-state-metrics", owner_kind="", owner_name=""},
              "workload", "$1", "pod", "(.+)")
            )
          labels:
            workload_type: barepod
          record: namespace_workload_pod:kube_pod_owner:relabel
        - expr: |-
            max by (cluster, namespace, workload, pod) (
              label_replace(
                kube_pod_owner{job="kube-state-metrics", owner_kind="Node"},
              "workload", "$1", "pod", "(.+)")
            )
          labels:
            workload_type: staticpod
          record: namespace_workload_pod:kube_pod_owner:relabel
        - expr: |-
            group by (cluster, namespace, workload, workload_type, pod) (
              label_join(
                label_join(
                  group by (cluster, namespace, job_name, pod) (
                    label_join(
                      kube_pod_owner{job="kube-state-metrics", owner_kind="Job"}
                    , "job_name", "", "owner_name")
                  )
                  * on (cluster, namespace, job_name) group_left(owner_kind, owner_name)
                  group by (cluster, namespace, job_name, owner_kind, owner_name) (
                    kube_job_owner{job="kube-state-metrics", owner_kind!="Pod", owner_kind!=""}
                  )
                , "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 (cluster, namespace, replicaset) group_left(owner_kind, owner_name)
                  group by (cluster, namespace, replicaset, owner_kind, owner_name) (
                    kube_replicaset_owner{job="kube-state-metrics", owner_kind!="Deployment", owner_kind!=""}
                  )
                , "workload", "$1", "owner_name", "(.+)")
                OR
                label_replace(
                  group by (cluster, namespace, pod, owner_name, owner_kind) (
                    kube_pod_owner{job="kube-state-metrics", owner_kind!="ReplicaSet", owner_kind!="DaemonSet", owner_kind!="StatefulSet", owner_kind!="Job", owner_kind!="Node", owner_kind!=""}
                  )
                  , "workload", "$1", "owner_name", "(.+)"
                )
              , "workload_type", "$1", "owner_kind", "(.+)")
            )
          record: namespace_workload_pod:kube_pod_owner:relabel
{{- end }}
