From 09ee53a23063a6be670afb2018868762bd36c865 Mon Sep 17 00:00:00 2001 From: Emmanuel Lodovice Date: Tue, 14 Nov 2023 10:03:37 -0800 Subject: [PATCH] Skip listing rule groups in CreateRuleGroup api if max rule groups limit is not set (#5653) Signed-off-by: Emmanuel Lodovice --- pkg/ruler/api.go | 22 ++++++++++++---------- pkg/ruler/ruler.go | 6 ++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/pkg/ruler/api.go b/pkg/ruler/api.go index 48debd328a..e76bc0830b 100644 --- a/pkg/ruler/api.go +++ b/pkg/ruler/api.go @@ -527,17 +527,19 @@ func (a *API) CreateRuleGroup(w http.ResponseWriter, req *http.Request) { return } - rgs, err := a.store.ListRuleGroupsForUserAndNamespace(req.Context(), userID, "") - if err != nil { - level.Error(logger).Log("msg", "unable to fetch current rule groups for validation", "err", err.Error(), "user", userID) - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } + if a.ruler.HasMaxRuleGroupsLimit(userID) { + rgs, err := a.store.ListRuleGroupsForUserAndNamespace(req.Context(), userID, "") + if err != nil { + level.Error(logger).Log("msg", "unable to fetch current rule groups for validation", "err", err.Error(), "user", userID) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } - if err := a.ruler.AssertMaxRuleGroups(userID, len(rgs)+1); err != nil { - level.Error(logger).Log("msg", "limit validation failure", "err", err.Error(), "user", userID) - http.Error(w, err.Error(), http.StatusBadRequest) - return + if err := a.ruler.AssertMaxRuleGroups(userID, len(rgs)+1); err != nil { + level.Error(logger).Log("msg", "limit validation failure", "err", err.Error(), "user", userID) + http.Error(w, err.Error(), http.StatusBadRequest) + return + } } rgProto := rulespb.ToProto(userID, namespace, rg) diff --git a/pkg/ruler/ruler.go b/pkg/ruler/ruler.go index 3cb56cb326..29799acc45 100644 --- a/pkg/ruler/ruler.go +++ b/pkg/ruler/ruler.go @@ -939,6 +939,12 @@ func (r *Ruler) Rules(ctx context.Context, in *RulesRequest) (*RulesResponse, er return &RulesResponse{Groups: groupDescs}, nil } +// HasMaxRuleGroupsLimit check if RulerMaxRuleGroupsPerTenant limit is set for the userID. +func (r *Ruler) HasMaxRuleGroupsLimit(userID string) bool { + limit := r.limits.RulerMaxRuleGroupsPerTenant(userID) + return limit > 0 +} + // AssertMaxRuleGroups limit has not been reached compared to the current // number of total rule groups in input and returns an error if so. func (r *Ruler) AssertMaxRuleGroups(userID string, rg int) error {