limit rule count per group (#5350)

Signed-off-by: junot <junotxiang@kubesphere.io>

Signed-off-by: junot <junotxiang@kubesphere.io>
This commit is contained in:
junot
2022-11-04 18:34:00 +08:00
committed by GitHub
parent ff59b80c28
commit 846bfd9459

View File

@@ -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 {