Skip to content

feat(rbg): support enabling portAllocator on demand - #250

Merged
cheyang merged 4 commits into
sgl-project:mainfrom
Syspretor:feat/support-enable-on-demand-for-portallocator
Mar 31, 2026
Merged

cheyang merged 4 commits into
sgl-project:mainfrom
Syspretor:feat/support-enable-on-demand-for-portallocator

Conversation

@Syspretor

Copy link
Copy Markdown
Collaborator

Ⅰ. Motivation

Ⅱ. Modifications

Ⅲ. Does this pull request fix one issue?

fixes #XXXX

Ⅳ. List the added test cases (unit test/integration test) if any, please explain if no tests are needed.

Ⅴ. Describe how to verify it

VI. Special notes for reviews

Checklist

  • Format your code make fmt.
  • Add unit tests or integration tests.
  • Update the documentation related to the change.

@Syspretor
Syspretor requested a review from cheyang March 27, 2026 09:10

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a toggle for the port allocator feature, allowing it to be enabled or disabled via a command-line flag and Helm configuration. The changes include conditional initialization in the main entry point and safety checks in the port allocator package to handle cases where it is not initialized. A review comment suggests improving error handling by using a sentinel error instead of ad-hoc error strings, which would allow callers to programmatically detect the disabled state.

Comment thread pkg/port-allocator/port_allocator.go

Copilot AI 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.

Pull request overview

Adds an opt-in switch for the port allocator feature so clusters can run RBGS without port allocation unless explicitly enabled (via CLI flag / Helm value), and wires that toggle through controller startup and Helm deployment.

Changes:

  • Introduces --enable-port-allocator and plumbs it into SetupPortAllocator(...), plus a global IsEnabled() check.
  • Gates RoleInstanceSet role-scoped port annotation handling behind the allocator-enabled switch.
  • Updates Helm values/template and e2e workflow to optionally enable the feature.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
pkg/reconciler/roleinstanceset_reconciler.go Gates role-scoped port annotation allocation/copying behind portallocator.IsEnabled() and refactors logic into a helper.
pkg/port-allocator/port_allocator.go Adds enabled/disabled semantics (IsEnabled, ErrPortAllocatorDisabled) and updates initialization to support being “off” by default.
cmd/rbgs/main.go Adds --enable-port-allocator flag and passes it into port allocator setup.
deploy/helm/rbgs/values.yaml Adds portAllocator.enabled value and documents port allocator settings.
deploy/helm/rbgs/templates/manager.yaml Conditionally injects port allocator flags only when enabled.
.github/workflows/e2e-test.yml Enables port allocator in e2e Helm install.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +189 to 193
if portallocator.IsEnabled() {
if err := allocateRoleScopedPortAnnotations(roleInstanceTemplateConfig.Components, old, roleInstanceSetAnnotation); err != nil {
return nil, err
}
}

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

Gating RoleScoped port annotation handling on portallocator.IsEnabled() means that when the allocator is disabled the reconciler will stop applying the previously-managed port allocation annotations. With server-side apply, this can result in existing role-scoped port annotations being removed from RoleInstanceSet on update. Consider always preserving/copying existing role-scoped port annotation keys from the current RoleInstanceSet, and only performing new allocations when the allocator is enabled.

Copilot uses AI. Check for mistakes.
Comment on lines +490 to +494
needCreate := old.Name == ""
if needCreate {
// Allocate RoleScoped ports for this component.
portAnnotations, err := portallocator.AllocateRoleScopedPorts(config, *component.Name)
if err != nil {

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

RoleScoped ports are only allocated when old.Name == "" (create). If the controller starts with port allocator disabled (so no annotations are written) and later runs with it enabled, existing RoleInstanceSets will never get role-scoped ports because they are treated as updates and only the (missing) annotations are copied. To support “enable on demand”, consider allocating any missing role-scoped ports on update when enabled (while still preserving existing values to avoid reallocation).

Copilot uses AI. Check for mistakes.
Comment on lines +121 to +124
}
alloc.pa = pa
alloc.enabled = true
return nil

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

configure() sets alloc.enabled=true before SetupPortAllocator calls start(). If start() fails, SetupPortAllocator returns an error but leaves the global allocator in an “enabled” state with a partially initialized allocator, which can cause confusing behavior if any code continues running (tests, embedding, or future refactors). Consider setting enabled=true only after start succeeds, or resetting enabled=false on start error.

Copilot uses AI. Check for mistakes.
Comment on lines 76 to 80
func AllocateBatch(num int32) ([]int32, error) {
if portAllocator == nil || !IsEnabled() {
return nil, ErrPortAllocatorDisabled
}
return portAllocator.pa.AllocateBatch(num)

Copilot AI Mar 31, 2026

Copy link

Choose a reason for hiding this comment

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

AllocateBatch now returns ErrPortAllocatorDisabled unless portAllocator.enabled is true. In-package tests (e.g. pkg/port-allocator/port_allocator_test.go) and any internal callers that set the global portAllocator directly (without setting enabled) will start failing with ErrPortAllocatorDisabled. Either update those tests/helpers to set enabled=true, or provide a helper initializer to avoid brittle direct global mutation.

Copilot uses AI. Check for mistakes.

@cheyang cheyang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

/lgtm
/approve

@cheyang
cheyang merged commit 18d454d into sgl-project:main Mar 31, 2026
9 checks passed
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.

3 participants