-
Notifications
You must be signed in to change notification settings - Fork 150
feat: auto-generate TenantSite entries for privileged tenants and service accounts #3534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RajMandaliya
wants to merge
6
commits into
NVIDIA:main
Choose a base branch
from
RajMandaliya:feat/auto-tenant-site-privileged
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fd0035a
Merge pull request #1 from NVIDIA/main
RajMandaliya 1ba0b8a
Merge branch 'main' of https://github.com/RajMandaliya/infra-controller
RajMandaliya e3117a7
merge upstream/main
RajMandaliya 54f9c55
merge upstream/main
RajMandaliya 3439896
feat: auto-generate TenantSite entries for privileged tenants and ser…
RajMandaliya 4621deb
fix: address CodeRabbit review feedback
RajMandaliya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package handler | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| cdb "github.com/NVIDIA/infra-controller/rest-api/db/pkg/db" | ||
| cdbm "github.com/NVIDIA/infra-controller/rest-api/db/pkg/db/model" | ||
| cdbp "github.com/NVIDIA/infra-controller/rest-api/db/pkg/db/paginator" | ||
| cutil "github.com/NVIDIA/infra-controller/rest-api/common/pkg/util" | ||
| "github.com/google/uuid" | ||
| ) | ||
|
|
||
| // EnsureTenantSitesForInfrastructureProvider auto-creates TenantSite records | ||
| // linking the given tenant to every registered Site under the given | ||
| // InfrastructureProvider. This is used for tenants that should be able to | ||
| // operate on any site belonging to their infrastructure provider without | ||
| // first needing a compute or network allocation there -- privileged tenants | ||
| // (TargetedInstanceCreation enabled) and service accounts. | ||
| func EnsureTenantSitesForInfrastructureProvider( | ||
| ctx context.Context, | ||
| tx *cdb.Tx, | ||
| dbSession *cdb.Session, | ||
| tenant *cdbm.Tenant, | ||
| infrastructureProviderID uuid.UUID, | ||
| createdBy uuid.UUID, | ||
| ) error { | ||
| siteDAO := cdbm.NewSiteDAO(dbSession) | ||
| sites, _, err := siteDAO.GetAll( | ||
| ctx, | ||
| tx, | ||
| cdbm.SiteFilterInput{InfrastructureProviderIDs: []uuid.UUID{infrastructureProviderID}}, | ||
| cdbp.PageInput{Limit: cutil.GetPtr(cdbp.TotalLimit)}, | ||
| nil, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("error retrieving sites for infrastructure provider: %w", err) | ||
| } | ||
| if len(sites) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| siteIDs := make([]uuid.UUID, len(sites)) | ||
| for i, site := range sites { | ||
| siteIDs[i] = site.ID | ||
| } | ||
|
|
||
| tsDAO := cdbm.NewTenantSiteDAO(dbSession) | ||
| existing, _, err := tsDAO.GetAll( | ||
| ctx, | ||
| tx, | ||
| cdbm.TenantSiteFilterInput{ | ||
| TenantIDs: []uuid.UUID{tenant.ID}, | ||
| SiteIDs: siteIDs, | ||
| }, | ||
| cdbp.PageInput{Limit: cutil.GetPtr(cdbp.TotalLimit)}, | ||
| nil, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("error retrieving existing TenantSite entries: %w", err) | ||
| } | ||
|
|
||
| existingSiteIDs := make(map[uuid.UUID]bool, len(existing)) | ||
| for _, ts := range existing { | ||
| existingSiteIDs[ts.SiteID] = true | ||
| } | ||
|
|
||
| for _, site := range sites { | ||
| if existingSiteIDs[site.ID] { | ||
| continue | ||
| } | ||
| _, err := tsDAO.Create( | ||
| ctx, | ||
| tx, | ||
| cdbm.TenantSiteCreateInput{ | ||
| TenantID: tenant.ID, | ||
| TenantOrg: tenant.Org, | ||
| SiteID: site.ID, | ||
| CreatedBy: createdBy, | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("error creating TenantSite entry for site %s: %w", site.ID, err) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package handler | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/NVIDIA/infra-controller/rest-api/api/pkg/api/handler/util/common" | ||
| authz "github.com/NVIDIA/infra-controller/rest-api/auth/pkg/authorization" | ||
| cdb "github.com/NVIDIA/infra-controller/rest-api/db/pkg/db" | ||
| cdbm "github.com/NVIDIA/infra-controller/rest-api/db/pkg/db/model" | ||
| cdbp "github.com/NVIDIA/infra-controller/rest-api/db/pkg/db/paginator" | ||
| "github.com/google/uuid" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestEnsureTenantSitesForInfrastructureProvider(t *testing.T) { | ||
| ctx := context.Background() | ||
|
|
||
| dbSession := common.TestInitDB(t) | ||
| defer dbSession.Close() | ||
|
|
||
| common.TestSetupSchema(t, dbSession) | ||
|
|
||
| org := "test-org-ensure-sites" | ||
| user := common.TestBuildUser(t, dbSession, uuid.NewString(), org, []string{authz.ProviderAdminRole, authz.TenantAdminRole}) | ||
| ip := common.TestBuildInfrastructureProvider(t, dbSession, "test-provider-ensure-sites", org, user) | ||
| tn := common.TestBuildTenant(t, dbSession, "test-tenant-ensure-sites", org, user) | ||
|
|
||
| site1 := common.TestBuildSite(t, dbSession, ip, "site-1", user) | ||
| site2 := common.TestBuildSite(t, dbSession, ip, "site-2", user) | ||
|
|
||
| tsDAO := cdbm.NewTenantSiteDAO(dbSession) | ||
|
|
||
| // Sanity check: no TenantSite records exist yet. | ||
| _, count, err := tsDAO.GetAll(ctx, nil, cdbm.TenantSiteFilterInput{TenantIDs: []uuid.UUID{tn.ID}}, cdbp.PageInput{}, nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 0, count) | ||
|
|
||
| // First call should create one TenantSite record per site. | ||
| err = cdb.WithTx(ctx, dbSession, func(tx *cdb.Tx) error { | ||
| return EnsureTenantSitesForInfrastructureProvider(ctx, tx, dbSession, tn, ip.ID, user.ID) | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| sites, count, err := tsDAO.GetAll(ctx, nil, cdbm.TenantSiteFilterInput{TenantIDs: []uuid.UUID{tn.ID}}, cdbp.PageInput{}, nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 2, count) | ||
|
|
||
| siteIDs := map[uuid.UUID]bool{} | ||
| for _, s := range sites { | ||
| siteIDs[s.SiteID] = true | ||
| } | ||
| require.True(t, siteIDs[site1.ID]) | ||
| require.True(t, siteIDs[site2.ID]) | ||
|
|
||
| // Calling it again should be idempotent -- no duplicate TenantSite rows. | ||
| err = cdb.WithTx(ctx, dbSession, func(tx *cdb.Tx) error { | ||
| return EnsureTenantSitesForInfrastructureProvider(ctx, tx, dbSession, tn, ip.ID, user.ID) | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| _, count, err = tsDAO.GetAll(ctx, nil, cdbm.TenantSiteFilterInput{TenantIDs: []uuid.UUID{tn.ID}}, cdbp.PageInput{}, nil) | ||
| require.NoError(t, err) | ||
| require.Equal(t, 2, count, "second call should not create duplicate TenantSite records") | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.