Skip to content

Commit 052b6e3

Browse files
authored
LBAC rules: Change to query all teams before looping for better performance (grafana#111689)
performance nit for querying all teams existance before looping through lbac rules
1 parent 004f30f commit 052b6e3

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

pkg/services/cleanup/cleanup.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -483,40 +483,46 @@ func (srv *CleanUpService) getLBACRulesForTeamsStillExisting(ctx context.Context
483483
cleanedHeaders := &datasources.TeamHTTPHeaders{Headers: make(map[string][]datasources.TeamHTTPHeader)}
484484
removedCount := 0
485485

486+
allTeams, err := srv.teamService.SearchTeams(ctx, &team.SearchTeamsQuery{
487+
OrgID: orgID,
488+
})
489+
if err != nil {
490+
logger.Error("Failed to get teams for LBAC cleanup", "error", err)
491+
return nil, removedCount
492+
}
493+
494+
teamUIDs := make(map[string]bool)
495+
for _, team := range allTeams.Teams {
496+
teamUIDs[team.UID] = true
497+
}
498+
teamIDs := make(map[int64]bool)
499+
for _, team := range allTeams.Teams {
500+
teamIDs[team.ID] = true
501+
}
502+
486503
for teamIdentifier, headers := range teamHeaders.Headers {
487504
// Determine if this is a UID or ID
488-
var teamUID string
489505
teamID, err := strconv.ParseInt(teamIdentifier, 10, 64)
490506

491507
if err != nil {
492508
// It's a UID
493-
teamUID = teamIdentifier
509+
if _, ok := teamUIDs[teamIdentifier]; !ok {
510+
logger.Debug("Team UID no longer exists, removing LBAC rules",
511+
"teamUID", teamIdentifier, "orgID", orgID)
512+
removedCount++
513+
continue
514+
}
494515
} else {
495-
// It's an ID, need to resolve to UID
496-
teamByID, err := srv.teamService.GetTeamByID(ctx, &team.GetTeamByIDQuery{
497-
OrgID: orgID,
498-
ID: teamID,
499-
})
500-
if err != nil {
516+
if _, ok := teamIDs[teamID]; !ok {
501517
logger.Debug("Team ID no longer exists, removing LBAC rules",
502518
"teamID", teamIdentifier, "orgID", orgID)
503519
removedCount++
504520
continue
505521
}
506-
teamUID = teamByID.UID
507-
}
508-
509-
// Check if team still exists by UID
510-
_, err = srv.teamService.GetTeamByID(ctx, &team.GetTeamByIDQuery{
511-
OrgID: orgID,
512-
UID: teamUID,
513-
})
514-
515-
if err != nil {
516-
logger.Debug("Team UID no longer exists, removing LBAC rules",
517-
"teamUID", teamUID, "orgID", orgID)
518-
removedCount++
519-
continue
522+
// team exists in lbac and exists in teams
523+
// lbac rule has team.ID and team exists
524+
// update the rule with the UID instead
525+
// TODO: we could replace the ID for the UID here we want
520526
}
521527

522528
// Team exists, keep the rules

0 commit comments

Comments
 (0)