From 846bfd9459176fa4bb726d6dd8fa5bc517f341e7 Mon Sep 17 00:00:00 2001 From: junot <49136171+junotx@users.noreply.github.com> Date: Fri, 4 Nov 2022 18:34:00 +0800 Subject: [PATCH] limit rule count per group (#5350) Signed-off-by: junot Signed-off-by: junot --- .../api/alerting/v2beta1/rulegroup_webhook.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/staging/src/kubesphere.io/api/alerting/v2beta1/rulegroup_webhook.go b/staging/src/kubesphere.io/api/alerting/v2beta1/rulegroup_webhook.go index 659c1d523..1e5902013 100644 --- a/staging/src/kubesphere.io/api/alerting/v2beta1/rulegroup_webhook.go +++ b/staging/src/kubesphere.io/api/alerting/v2beta1/rulegroup_webhook.go @@ -33,7 +33,10 @@ import ( var rulegrouplog = logf.Log.WithName("rulegroup") -const RuleLabelKeyRuleId = "rule_id" +const ( + RuleLabelKeyRuleId = "rule_id" + MaxRuleCountPerGroup = 40 +) func (r *RuleGroup) SetupWebhookWithManager(mgr ctrl.Manager) error { return ctrl.NewWebhookManagedBy(mgr). @@ -88,6 +91,10 @@ func (r *RuleGroup) Validate() error { log := rulegrouplog.WithValues("name", r.Namespace+"/"+r.Name) log.Info("validate") + if len(r.Spec.Rules) > MaxRuleCountPerGroup { + return fmt.Errorf("the rule group has %d rules, exceeding the max count (%d)", len(r.Spec.Rules), MaxRuleCountPerGroup) + } + var rules []Rule for _, r := range r.Spec.Rules { rules = append(rules, r.Rule) @@ -210,6 +217,10 @@ func (r *ClusterRuleGroup) Validate() error { log := clusterrulegrouplog.WithValues("name", r.Name) log.Info("validate") + if len(r.Spec.Rules) > MaxRuleCountPerGroup { + return fmt.Errorf("the rule group has %d rules, exceeding the max count (%d)", len(r.Spec.Rules), MaxRuleCountPerGroup) + } + var rules []Rule for _, r := range r.Spec.Rules { rules = append(rules, r.Rule) @@ -307,6 +318,10 @@ func (r *GlobalRuleGroup) Validate() error { log := globalrulegrouplog.WithValues("name", r.Name) log.Info("validate") + if len(r.Spec.Rules) > MaxRuleCountPerGroup { + return fmt.Errorf("the rule group has %d rules, exceeding the max count (%d)", len(r.Spec.Rules), MaxRuleCountPerGroup) + } + var rules []Rule for _, r := range r.Spec.Rules { if r.ClusterSelector != nil {