prometheus-operator / kube-prometheus

Use Prometheus to monitor Kubernetes and applications running on Kubernetes
https://prometheus-operator.dev/
Apache License 2.0
6.76k stars 1.93k forks source link

Remove one default rule using jsonnet #697

Open KR411-prog opened 4 years ago

KR411-prog commented 4 years ago

I have below code in jsonnet which does update default rule,create a new rule..This works fine. But I want to delete the rule "AggregateAPIDown".I am not sure how to do this in jsonnet.

I tried this,

          else if rule.alert == "AggregatedAPIDown" then
                    rule {

                    }

But its not removing the rule. Please let me know how to achieve this.

Here is the full code,


 local kp = (import 'kube-prometheus/kube-prometheus.libsonnet') +{
  _config+:: {
      namespace: 'monitoring',
    },
  prometheusAlerts+:: {
      groups: 
        std.map(
          function(group)
            if group.name == 'kubernetes-apps' then
              group {
                rules: std.map(function(rule)
                  if rule.alert == "KubeStatefulSetReplicasMismatch" then
                    rule {
                      expr: "kube_statefulset_status_replicas_ready{job=\"kube-state-metrics\",statefulset!=\"vault\"} != kube_statefulset_status_replicas{job=\"kube-state-metrics\",statefulset!=\"vault\"}",
                      labels: {
                          priority: 'P1',
                          severity: 'info',
                        },
                    }
                  else if rule.alert == "KubePodNotReady" then
                    rule {
                      expr: |||
              (sum by (namespace, pod) (
                max by(namespace, pod) (
                  kube_pod_status_phase{%(prefixedNamespaceSelector)s%(kubeStateMetricsSelector)s, phase=~"Pending|Unknown"}
                ) * on(namespace, pod) group_left(owner_kind) topk by(namespace, pod) (
                  1, max by(namespace, pod, owner_kind) (kube_pod_owner{owner_kind!="Job"})
                )
              ) > 0)* on(namespace, pod) group_left(label_teamname) kube_pod_labels 
            ||| % $._config,
                      labels: {
                          priority: 'P2',
                          severity: 'critical',
                        },
                    }
            else if rule.alert == "AggregatedAPIDown" then
                    rule {

                    }
                  else                  
                    rule
                  ,
                  group.rules

                )
              }
            else
              group,
          super.groups
        ),
    },
 }+
 {
    prometheusRules+:: {
    groups+: [
      {
        name: 'cluster-autoscaler',
        rules: [
          {
            alert: 'ClusterReachedMaximumCapacity',
            expr: 'sum(cluster_autoscaler_unschedulable_pods_count)>0 and sum(cluster_autoscaler_unneeded_nodes_count)==0',
            annotations:
            {
              message: 'Cluster has reached its maximum capacity for more than 15 minutes.',

            },
   //         for: '15m',
            labels:
            {
              severity: 'critical',
            }, 
          },
        ],
      },
    ],
  },
  };
// local kp = (import 'kube-prometheus/kube-prometheus.libsonnet') + filter + update;

{ ['00namespace-' + name]: kp.kubePrometheus[name] for name in std.objectFields(kp.kubePrometheus) } +
{ ['0prometheus-operator-' + name]: kp.prometheusOperator[name] for name in std.objectFields(kp.prometheusOperator) } +
{ ['node-exporter-' + name]: kp.nodeExporter[name] for name in std.objectFields(kp.nodeExporter) } +
{ ['kube-state-metrics-' + name]: kp.kubeStateMetrics[name] for name in std.objectFields(kp.kubeStateMetrics) } +
{ ['alertmanager-' + name]: kp.alertmanager[name] for name in std.objectFields(kp.alertmanager) } +
{ ['prometheus-' + name]: kp.prometheus[name] for name in std.objectFields(kp.prometheus) } +
{ ['prometheus-adapter-' + name]: kp.prometheusAdapter[name] for name in std.objectFields(kp.prometheusAdapter) } +
{ ['grafana-' + name]: kp.grafana[name] for name in std.objectFields(kp.grafana) }
paulfantom commented 4 years ago

Here is an example how to do this: https://github.com/openshift/cluster-monitoring-operator/blob/release-4.4/jsonnet/main.jsonnet#L37-L61

lilic commented 4 years ago

Mind be good to document this in our README, if we do not have it already. Do you mind @KR411-prog creating a PR to add an example if the above works for you? Thanks! 🎉 :)

yeya24 commented 3 years ago

We are currently using https://github.com/openshift/cluster-monitoring-operator/blob/master/jsonnet/patch-rules.libsonnet to patch or disable rules. It is super handy and easy to use for our usecase. Might be worth mentioning somewhere in the document.

paulfantom commented 3 years ago

It might be good to port it into https://github.com/prometheus-operator/kube-prometheus/tree/main/jsonnet/kube-prometheus/lib, use in https://github.com/prometheus-operator/kube-prometheus/blob/main/examples/changing-default-rules.libsonnet and amend our docs in https://github.com/prometheus-operator/kube-prometheus/blob/main/docs/developing-prometheus-rules-and-grafana-dashboards.md#changing-default-rules#735

any takers? :)


@dgrisonnet wdyt about moving that patch functions into kube-prometheus and amending CMO codebase to use ported ones?

dgrisonnet commented 3 years ago

Happy to move the patch functions to kube-prometheus if they can be useful to the community :)

dgrisonnet commented 3 years ago

/cc @raptorsun

raptorsun commented 3 years ago

I am going to work on this issue 🙋‍♂️