Initial support for port mapping in CLI#14225
Initial support for port mapping in CLI#14225AmelBawa-msft merged 35 commits intofeature/wsl-for-appsfrom
Conversation
|
I have a meta comment before we go too far down the argument validation path. We have two types of arguments in the CLI:
@OneBlue recommended that our general approach for argument validation for arguments to the service is that we should pass the argument along to the service with as little modification as possible, and if it's bad input the service will return error messages that we can surface to the user. I completely agree with this approach and believe it should apply to everything not just the service. I would prefer not to have rigid validation of arguments in the CLI unless it's a type 1 argument specific to the CLI or some minimum parsing needs to be done in order to put the data into a form the service can consume (converting a string to two integers, for example). The port validation is something I would expect the service to handle and I am generally concerned about the CLI parsing the port data argument differently from the service. I am strongly in favor of only validating the bare minimum needed to make it valid input to the service and let the service tell us if its inputs were invalid. So the general guidance for argument validation should be: "Is something else already validating or going to validate this?" If the answer is yes, then the CLI should only validate it enough to get it to that validator and then rely on that validator to do its job correctly. |
|
@dkbennett great observation and feedback, thank you! I have a couple of questions before marking this PR as ready, one of which you touched on, and another related to what we currently support. This PR contains the necessary pieces to light up the feature, but I’ve held off on marking it ready to avoid duplicating service logic and to clarify how certain parts of the syntax should be handled. Some aspects may require follow-up work with the runtime team. I will sync up today with the team for suggestions on the next step. Excited to see this feature finally available 🚀🙌 |
There was a problem hiding this comment.
Pull request overview
This PR adds initial support for port mapping in the WSLC CLI, implementing the -p/--publish flag for the wslc container run and wslc container create commands. The implementation includes a comprehensive port mapping parser that supports various formats including IPv4/IPv6 addresses, port ranges, and protocol specifications (TCP/UDP).
Changes:
- Adds
PublishPortparser with support for formats like[host-ip:][host-port:]container-port[/protocol], including IPv6 addresses in brackets and port ranges - Integrates port mapping into CLI commands with
-p/--publishargument - Implements ephemeral port allocation using socket binding when host port is not specified
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| src/windows/wslc/ContainerModel.h | Adds PublishPort class with nested PortRange and IPAddress structs for parsing port mapping specifications |
| src/windows/wslc/ContainerModel.cpp | Implements parsing logic for port mappings, IP addresses, and port ranges with validation |
| src/windows/wslc/ContainerService.cpp | Adds ResolveOrAllocatePort function for ephemeral port allocation and integrates port mapping into container creation |
| src/windows/wslc/ContainerCommand.h | Adds -p/--publish flag to container run and create commands with help text |
| src/windows/wslc/CMakeLists.txt | Adds ContainerModel.cpp to build configuration |
… into user/amelbawa/port
… into user/amelbawa/port
… into user/amelbawa/port
| { | ||
| auto address = hostIpPart.substr(1, hostIpPart.size() - 2); | ||
| IN6_ADDR v6{}; | ||
| if (inet_pton(AF_INET6, address.c_str(), &(v6)) == 1) |
There was a problem hiding this comment.
The CLI doesn't need to parse this since this will be done in the service (same for IPV4)
There was a problem hiding this comment.
We use this information to determine if the IP is v4 or v6. But if the service will handle this, we can possibly reduce this to checking for just [ and ]?
There was a problem hiding this comment.
Yeah I think just relying on the bracket syntax is the right move for now
There was a problem hiding this comment.
One interesting use case I came across is:
::1:8080:80
After the refactor PR is merged, the CLI will pass:
AddPort(8080, 80, AF_INET, "::1")
Notice that AF_INET is used instead of AF_INET6 because square brackets were not provided. I’m wondering whether this scenario will be handled correctly on the service side.
Summary of the Pull Request
🦞 Port mapping parser
Parser support examples:
✅ Valid (parsing)
❌ Invalid (parsing)
🦐 Full example
PR Checklist
Detailed Description of the Pull Request / Additional comments
Validation Steps Performed