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 {