Refactor managed cluster cache helper#771
Conversation
📝 WalkthroughWalkthroughManaged cluster listing now encapsulates RBAC cache handling and returns only cluster data and errors. Admin tenant authorization, listing, and pool organization resolution use the updated function, with tests verifying cache reuse. ChangesManaged cluster cache consolidation
Estimated code review effort: 2 (Simple) | ~15 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Refactors the server’s managed-cluster listing helper to always route through the TiDB Cloud RBAC cache, removing redundant wrapper/bypass plumbing and simplifying call sites in the admin tenant and tenant-pool flows.
Changes:
- Inlined the RBAC cache lookup path into
listAllManagedClustersand removed the redundant cached helper, cache toggle, and cache-origin return value. - Updated admin tenant and tenant-pool call sites to use the simplified
listAllManagedClusterssignature. - Added a regression test verifying repeated managed-cluster list calls reuse the RBAC cache (avoiding extra TiDB Cloud list calls).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pkg/server/admin_tenants.go | Simplifies managed-cluster listing helper to directly include RBAC cache behavior; updates admin tenant endpoints to match new return signature. |
| pkg/server/admin_tenant_pool.go | Updates org resolution path to use the refactored managed-cluster listing helper. |
| pkg/server/admin_tenant_pool_cache_test.go | Adds regression coverage ensuring repeated managed-cluster lists hit the RBAC cache (single upstream list call). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
🧹 Nitpick comments (1)
pkg/server/admin_tenants.go (1)
531-534: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueUse
errors.Newfor static error messages.Since there are no formatting arguments provided, prefer using
errors.Newoverfmt.Errorf.♻️ Proposed refactor
lister, ok := s.provisioner.(tenant.ManagedClusterLister) if !ok { - return nil, fmt.Errorf("managed cluster list not enabled") + return nil, errors.New("managed cluster list not enabled") }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@pkg/server/admin_tenants.go` around lines 531 - 534, Update the error construction in the managed cluster lister type-assertion branch of the server handler to use errors.New for the static “managed cluster list not enabled” message instead of fmt.Errorf, and add or reuse the required errors import.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@pkg/server/admin_tenants.go`:
- Around line 531-534: Update the error construction in the managed cluster
lister type-assertion branch of the server handler to use errors.New for the
static “managed cluster list not enabled” message instead of fmt.Errorf, and add
or reuse the required errors import.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 0c65ce99-6d03-466d-aaa1-a17cf3b570b7
📒 Files selected for processing (3)
pkg/server/admin_tenant_pool.gopkg/server/admin_tenant_pool_cache_test.gopkg/server/admin_tenants.go
mornyx
left a comment
There was a problem hiding this comment.
Reviewed locally (checked out the branch, built, ran the targeted tests).
Verdict: approve — this is a behavior-preserving simplification. Verified the two claims behind it:
- The bypass path was unreachable. On main, the only direct
listAllManagedClustersCachedcaller passedallowCache=true(authorizedAdminTenant), and thelistAllManagedClusterswrapper hard-codedtrue. So removing theallowCacheparameter and thebypassmetric label loses nothing —bypasscould never be emitted at runtime, and no dashboard under docs/grafana references it. - The cache-origin
boolreturn was discarded at every call site (clusters, _, err× 3). Dropping it from the signature is safe.
Also confirmed:
- All 4 call sites updated; no dangling references to
listAllManagedClustersCached;go build ./pkg/serverclean. TestListAllManagedClustersUsesRBACCache+TestFirstManagedOrganizationUsesRBACCachepass locally; CI green.- Cache semantics unchanged: scoped lookup still only serves a hit when the cached cluster has a non-empty OrganizationID; unscoped miss still populates via
rememberClusterList; the deleting-missing-cluster fallback inauthorizedAdminTenantbehaves exactly as before.
Two non-blocking notes:
- The branch history contains the accidental
add managed-cluster-cachecommit and its revert (net-zero). Harmless under squash merge — just flagging so nobody is confused by the commit list. - Process FYI: that accidental commit and its revert were pushed directly to main (bypassing PR/CI). The revert did restore main's tree byte-for-byte to the post-#765 state (verified:
git diff cd8e004 origin/mainis empty), so no harm done — but it may be worth tightening branch protection on main to require PRs.
|
Reviewed (read-only diff). Clean behavior-preserving refactor and a natural follow-up to #764. LGTM. This folds
Already carries an approval; no concerns from me. LGTM. |
Summary
listAllManagedClustersTest plan
go test ./pkg/server -run "^(TestListAllManagedClustersUsesRBACCache|TestFirstManagedOrganizationUsesRBACCache)$" -count=1make lintgit diff --checkSummary by CodeRabbit
Bug Fixes
Tests