Skip to content

kep: pod port allocation#176

Open
shangsmo wants to merge 6 commits intosgl-project:mainfrom
shangsmo:kep-171-pod-port-allocator
Open

kep: pod port allocation#176
shangsmo wants to merge 6 commits intosgl-project:mainfrom
shangsmo:kep-171-pod-port-allocator

Conversation

@shangsmo
Copy link

@shangsmo shangsmo commented Mar 3, 2026

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

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new Kubernetes Enhancement Proposal (KEP) aimed at solving port conflict issues for applications, particularly LLM services, that require hostNetwork: true in a Kubernetes environment. The proposal outlines a mechanism for dynamic port allocation, allowing RBG applications to automatically assign unique ports to Pod replicas. This enhancement is designed to improve deployment density and operational flexibility by preventing port clashes and enabling service discovery among replicas.

Highlights

  • New KEP Introduced: A new Kubernetes Enhancement Proposal (KEP-171) has been added, focusing on dynamic Pod Port Allocation within RBG (Role-Based Group) applications.
  • Addressing Port Conflicts: The KEP proposes a solution to automatically assign different port numbers to Pod replicas, specifically addressing port conflicts that arise when using hostNetwork: true for LLM services.
  • Configurable Port Allocation API: Users can configure dynamic port allocation via a new annotation (rolebasedgroup.workloads.x-k8s.io/port.allocator-config) in the Pod Template, allowing injection of ports as environment variables or Pod annotations.
  • Extensible Port Allocator Interface: An interface (PortAllocatorInterface) is defined to allow for different port allocation strategies, promoting future extensibility.
  • Lifecycle Integration: The proposed implementation integrates port allocation and release into the Pod lifecycle (creation, update, deletion) when managed by an Instance resource.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • keps/171-pod-port-allocation/README.md
    • Added a new KEP document detailing the motivation, goals, proposal, and design for Pod Port Allocation.
  • keps/171-pod-port-allocation/kep.yaml
    • Added KEP metadata, including title, number, authors, status, and milestones for the Pod Port Allocation proposal.
Activity
  • The pull request was opened by shangsmo, proposing a new feature for port allocation.
  • The description includes a standard template, indicating that the detailed motivation and changes are documented within the added KEP files.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@shangsmo shangsmo changed the title feature: add port allocation kep kep: add port allocation kep Mar 3, 2026
Copy link

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

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 Kubernetes Enhancement Proposal (KEP) for dynamic Pod port allocation. The proposal is well-structured and addresses a valid use case, particularly for hostNetwork scenarios. My review includes several suggestions to improve the clarity and design of the proposal. I've pointed out a couple of typos and inconsistencies in the API definition and examples. More importantly, I've raised concerns about the use of a global singleton for the port allocator, suggesting dependency injection as a better alternative for testability and maintainability. I also noted that the test plan is currently empty and needs to be filled out with details on unit, integration, and E2E tests. Overall, this is a good start for an important feature.

Comment on lines +196 to +202
// Singleton pattern, created at program startup based on the port allocation strategy
var portAllocator *PortAllocator

// GetPortAllocator
func GetPortAllocator() PortAllocatorInterface {
return portAllocator.pa
}

Choose a reason for hiding this comment

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

high

The proposed design uses a global singleton pattern for the portAllocator. This introduces global state, which can make testing difficult and the system harder to reason about. Consider using dependency injection instead. The PortAllocator could be instantiated in main.go and passed down to the reconcilers that need it. This would make the dependencies explicit and improve testability.

Comment on lines +219 to +229
### Test Plan

#### Unit Tests


#### Integration tests


#### End to End Tests


Choose a reason for hiding this comment

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

high

The Test Plan section is currently empty. For a feature of this nature, it's crucial to outline a comprehensive testing strategy. Please detail the planned unit, integration, and end-to-end tests. For example:

  • Unit Tests: Cover the port allocation logic, including edge cases like port range exhaustion.
  • Integration Tests: Verify that the controller correctly injects ports into Pods and releases them upon deletion.
  • End-to-End Tests: Test the full workflow in a cluster, including scenarios with hostNetwork: true and multiple replicas on the same node.

@shangsmo shangsmo changed the title kep: add port allocation kep kep: pod port allocation Mar 3, 2026
shangsmo and others added 4 commits March 3, 2026 20:45
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
```yaml
rolebasedgroup.workloads.x-k8s.io/port.allocator-config: '[
{
"shareName": "",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I propose restructuring the API to explicitly separate allocations from references. This approach improves clarity and reduces cognitive load:

rolebasedgroup.workloads.x-k8s.io/port-allocator: |
 {
  "allocations": [
    {
      "name": "http-port",         // Logical name
      "env": "SGLANG_PORT",        // Env var to inject
      "policy": "Dynamic"          // Dynamic (per-pod) or Static (per-role)
    }
  ],
  "references": [
    {
      "env": "LEADER_PORT",        // Env var to inject
      "from": "leader.http-port"   // Format: <role_name>.<port_name>
    }
  ]
}

"portRange": 10000,
"annotationKey": "sglang.ai/bootstrap-port",
"env": "BOOTSTRAP_PORT",
"scope": "pod",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rename scope to policy to better describe the allocation behavior:

  • Dynamic (Default): Every Pod gets a unique random port. (Solves the motivation scenario).
  • Static: The entire Role shares one port. (Useful only if using standard Overlay networking or ensuring strict anti-affinity).

{
"shareName": "",
"startPort": 30000,
"portRange": 10000,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Move startPort and portRange to the Controller's startup flags.

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.

2 participants