Skip to content

Commit

Permalink
Skip listing rule groups in CreateRuleGroup api if max rule groups li…
Browse files Browse the repository at this point in the history
…mit is not set

Signed-off-by: Emmanuel Lodovice <[email protected]>
  • Loading branch information
emanlodovice committed Nov 14, 2023
1 parent dc63e23 commit a6b5801
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
22 changes: 12 additions & 10 deletions pkg/ruler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pkg/ruler/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a6b5801

Please sign in to comment.