Skip to content

feat: auto-generate TenantSite entries for privileged tenants and service accounts#3534

Open
RajMandaliya wants to merge 6 commits into
NVIDIA:mainfrom
RajMandaliya:feat/auto-tenant-site-privileged
Open

feat: auto-generate TenantSite entries for privileged tenants and service accounts#3534
RajMandaliya wants to merge 6 commits into
NVIDIA:mainfrom
RajMandaliya:feat/auto-tenant-site-privileged

Conversation

@RajMandaliya

Copy link
Copy Markdown
Contributor

Addresses #3370.

Problem

TenantSite records (which grant a tenant visibility and action rights on a site) were only ever created as a side effect of that tenant's first allocation on the site, and deleted when their last allocation there was removed.

This no longer matches real access patterns: privileged tenants (TargetedInstanceCreation enabled) have long been able to create instances on sites without any compute allocation there, and zero-DPU instances mean they may not need a network allocation either. Service accounts have the same need but currently have no path to a TenantSite record at all unless they happen to also hold an allocation.

Fix

  • Adds EnsureTenantSitesForInfrastructureProvider, a shared helper that auto-creates TenantSite records for every site under a tenant's infrastructure provider (mirrors the existing get-or-create pattern already used in allocation.go).
  • Wired into GetCurrentServiceAccountHandler (service accounts always get this) and GetCurrentTenantHandler (only when the tenant is privileged).
  • Updates the allocation-deletion cleanup path to skip removing a TenantSite record for privileged tenants/service accounts, since per the issue, these tenants shouldn't lose site access just because their last allocation there was deleted.

Testing

  • New direct test for the helper (creation across multiple sites, plus idempotency on a second call).
  • Full existing handler package test suite passes. Note: two pre-existing tests showed flakiness under default parallel execution
    against the shared test DB (relation "tenant" does not exist, duplicate pg_type key) — confirmed unrelated to this change by running serially (-p 1 -parallel 1), where everything passes cleanly.

RajMandaliya and others added 5 commits July 8, 2026 12:58
Keeping Pace with original repo
…vice accounts

Historically, TenantSite records (which grant a tenant visibility and
action rights on a site) were only created as a side effect of that
tenant's first allocation on the site, and deleted when their last
allocation there was removed.

This no longer covers real access patterns: privileged tenants
(TargetedInstanceCreation enabled) have long been able to create
instances on sites without any compute allocation there, and zero-DPU
instances mean they may not need a network allocation either. Service
accounts have the same need.

Adds EnsureTenantSitesForInfrastructureProvider, a shared helper that
auto-creates TenantSite records for every site under a tenant's
infrastructure provider. Wired into:
- GetCurrentServiceAccountHandler (service accounts always get this)
- GetCurrentTenantHandler (only when the tenant is privileged, i.e.
  TargetedInstanceCreation is enabled)

Also updates the allocation-deletion cleanup path in allocation.go to
skip removing a TenantSite record when the tenant is privileged, since
these tenants shouldn't lose site access just because their last
allocation there was deleted.

Tested directly (creation + idempotency on a second call), plus full
existing handler test suite passes (serially -- a couple of tests
showed pre-existing flakiness under parallel execution against a
shared test DB, unrelated to this change, and passed cleanly with
-p 1 -parallel 1).

Addresses NVIDIA#3370.

Signed-off-by: RajMandaliya <rajmandaliya2249@gmail.com>
@RajMandaliya RajMandaliya requested a review from a team as a code owner July 15, 2026 05:12
@copy-pr-bot

copy-pr-bot Bot commented Jul 15, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: c9496fd0-3aa6-423d-aa32-55dcd0184a83

📥 Commits

Reviewing files that changed from the base of the PR and between 3439896 and 4621deb.

📒 Files selected for processing (3)
  • rest-api/api/pkg/api/handler/allocation.go
  • rest-api/api/pkg/api/handler/allocation_test.go
  • rest-api/api/pkg/api/handler/tenantsite_helpers.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • rest-api/api/pkg/api/handler/tenantsite_helpers.go

Summary by CodeRabbit

  • New Features
    • Automatically ensures tenant site access is created for every site covered by an infrastructure provider.
    • Service account and current tenant flows now keep tenant site associations synchronized.
  • Bug Fixes
    • Privileged tenants now retain their tenant-site association when their last allocation is removed.
    • Prevents duplicate tenant-site records during repeated setup.
  • Tests
    • Added coverage for idempotent tenant-site provisioning and privileged allocation deletion behavior.

Walkthrough

The change adds idempotent TenantSite synchronization for infrastructure-provider sites, invokes it during service-account and privileged tenant initialization, and preserves TenantSite associations for privileged tenants when their last allocation is deleted.

Changes

TenantSite lifecycle synchronization

Layer / File(s) Summary
Ensure TenantSite records
rest-api/api/pkg/api/handler/tenantsite_helpers.go, rest-api/api/pkg/api/handler/tenantsite_helpers_test.go
Adds a helper that creates missing TenantSite records for all provider sites and tests repeated-call idempotency.
Tenant initialization integration
rest-api/api/pkg/api/handler/serviceaccount.go, rest-api/api/pkg/api/handler/tenant.go
Runs TenantSite synchronization transactionally for service-account tenants and privileged current tenants, with transaction error handling before response construction.
Allocation deletion behavior
rest-api/api/pkg/api/handler/allocation.go, rest-api/api/pkg/api/handler/allocation_test.go
Skips TenantSite removal for privileged tenants after their final allocation is deleted and verifies the association remains.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant TenantHandler
  participant EnsureTenantSitesForInfrastructureProvider
  participant SiteDAO
  participant TenantSiteDAO
  TenantHandler->>EnsureTenantSitesForInfrastructureProvider: synchronize privileged tenant sites
  EnsureTenantSitesForInfrastructureProvider->>SiteDAO: retrieve infrastructure-provider sites
  EnsureTenantSitesForInfrastructureProvider->>TenantSiteDAO: check and create missing TenantSite records
  TenantSiteDAO-->>TenantHandler: transaction completes
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: auto-generating TenantSite entries for privileged tenants and service accounts.
Description check ✅ Passed The description is directly related to the implemented TenantSite automation, retention, and testing changes.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🤖 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.

Inline comments:
In `@rest-api/api/pkg/api/handler/allocation.go`:
- Around line 1478-1484: Remove the redundant tenant lookup via tnDAO.GetByID in
the cleanup check and derive isPrivileged directly from the already-loaded
a.Tenant and its Config.TargetedInstanceCreation, preserving the existing
nil-safe behavior and error flow where applicable.
- Around line 1473-1509: Extend allocation_test.go with a case covering the
TargetedInstanceCreation privileged-tenant path in the allocation deletion flow.
Configure the tenant/service account as privileged, remove its last allocation,
and assert that its TenantSite association remains instead of being deleted,
while preserving existing regular-tenant coverage.

In `@rest-api/api/pkg/api/handler/tenantsite_helpers.go`:
- Around line 43-73: Optimize EnsureTenantSitesForInfrastructureProvider in
rest-api/api/pkg/api/handler/tenantsite_helpers.go:43-73 by fetching all
existing TenantSite rows for the tenant and supplied site IDs in one query, then
creating only missing entries instead of issuing per-site GetAll/Create calls.
In rest-api/api/pkg/api/handler/serviceaccount.go:180-188 and
rest-api/api/pkg/api/handler/tenant.go:209-230, retain these calls only if the
helper’s cheap no-op behavior makes them safe; otherwise gate them to avoid
repeated execution on every GetCurrent request.
- Around line 43-73: Batch the TenantSite lookup in the surrounding helper: call
tsDAO.GetAll once for tenant.ID without filtering by individual site, build a
set of existing SiteIDs from the returned rows, then iterate sites and invoke
tsDAO.Create only for missing IDs. Preserve the existing error context for
lookup and creation failures, and avoid per-site GetAll calls.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: cee7183f-8743-44f5-8204-9e9c25b4aaa5

📥 Commits

Reviewing files that changed from the base of the PR and between eb4ea22 and 3439896.

📒 Files selected for processing (5)
  • rest-api/api/pkg/api/handler/allocation.go
  • rest-api/api/pkg/api/handler/serviceaccount.go
  • rest-api/api/pkg/api/handler/tenant.go
  • rest-api/api/pkg/api/handler/tenantsite_helpers.go
  • rest-api/api/pkg/api/handler/tenantsite_helpers_test.go

Comment thread rest-api/api/pkg/api/handler/allocation.go
Comment thread rest-api/api/pkg/api/handler/allocation.go Outdated
Comment thread rest-api/api/pkg/api/handler/tenantsite_helpers.go
- Remove the redundant Tenant lookup in the allocation-deletion cleanup
  check; a.Tenant is already loaded via includeRelations on the
  earlier GetByID for this allocation, so derive isPrivileged directly
  from it instead of an extra DB round-trip.
- Batch EnsureTenantSitesForInfrastructureProvider's TenantSite lookup
  into a single query across all sites (previously issued one GetAll
  per site), then only Create the entries actually missing. This also
  makes the helper cheap enough to safely call on every GetCurrent
  request, as it was before.
- Add TestAllocationHandler_Delete_PrivilegedTenantKeepsTenantSite,
  covering the actual gap CodeRabbit flagged: a privileged tenant's
  last allocation on a site is deleted, and its TenantSite association
  is verified to persist through the real delete handler, rather than
  only being covered indirectly via the unit-level helper test.

Signed-off-by: RajMandaliya <rajmandaliya2249@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant