Add port mappings to container list output#14438
Add port mappings to container list output#14438beena352 wants to merge 15 commits intomicrosoft:feature/wsl-for-appsfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds port mapping information to the container list output. When containers are created with -p/--publish, the mapped ports now appear in a new PORTS column in the table view and in JSON output.
Changes:
- Extended the IDL with
WSLAPortProtocolenum and addedPorts/PortsCountfields toWSLAContainerEntry, plusProtocoltoWSLAPortMapping - Added CLI-side port parsing from
--publishargs,FormatPorts()formatting, andPortInformation/PortOptionmodel structs - Populated port data in
ListContainerson the service side and added cleanup handling for the nested COM-allocated ports array
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/windows/wslaservice/inc/wslaservice.idl | Added WSLAPortProtocol enum, Protocol to WSLAPortMapping, Ports/PortsCount to WSLAContainerEntry |
| src/windows/wslasession/WSLASession.cpp | Populate port data in ListContainers with error cleanup scope guard |
| src/windows/wslasession/WSLAContainer.h | Declared GetPorts() accessor |
| src/windows/wslasession/WSLAContainer.cpp | Implemented GetPorts() returning m_mappedPorts |
| src/windows/common/WSLAContainerLauncher.cpp | Set Protocol field in AddPort() |
| src/windows/WslcSDK/wslcsdk.cpp | Pass Protocol field through SDK |
| src/windows/wslc/services/ContainerModel.h | Added PortOption, PortInformation structs with JSON serialization |
| src/windows/wslc/services/ContainerService.h | Declared FormatPorts() |
| src/windows/wslc/services/ContainerService.cpp | Implemented FormatPorts(), extract ports in List(), pass ports in CreateInternal() |
| src/windows/wslc/tasks/ContainerTasks.cpp | Parse --publish args, add PORTS column to table output |
| test/windows/WSLATests.cpp | Added port mapping assertions to PortMappingsNat test and zero-port checks to existing tests |
You can also share your feedback on Copilot code review. Take the survey.
There was a problem hiding this comment.
Pull request overview
This PR adds port mapping information to the container list output. When containers are created with -p, the PORTS column now displays mapped ports in the format host:port->container_port/protocol. The changes span the IDL, service, CLI, SDK, and test layers.
Changes:
- Extended
WSLAContainerEntryandWSLAPortMappingIDL structs withPorts/PortsCountandProtocolfields; addedWSLAPortProtocolenum. - Wired port data through the service (
ListContainers), CLI (FormatPorts,-pargument parsing viaAddPort), and SDK layers. - Added test verification that
ListContainersreturns correct port data for containers created with port mappings.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/windows/wslaservice/inc/wslaservice.idl | Added WSLAPortProtocol enum, Protocol to WSLAPortMapping, and Ports/PortsCount to WSLAContainerEntry. |
| src/windows/wslasession/WSLASession.cpp | Populates port data in ListContainers with error-cleanup scope guard. |
| src/windows/wslasession/WSLAContainer.h | Declares GetPorts() accessor. |
| src/windows/wslasession/WSLAContainer.cpp | Implements GetPorts() returning m_mappedPorts. |
| src/windows/common/WSLAContainerLauncher.cpp | Sets Protocol = WSLAPortProtocolTCP in AddPort. |
| src/windows/WslcSDK/wslcsdk.cpp | Passes Protocol field when converting SDK port mappings to IDL. |
| src/windows/wslc/services/ContainerModel.h | Adds PortOption, PortInformation structs and Ports to ContainerOptions/ContainerInformation. |
| src/windows/wslc/services/ContainerService.h | Declares FormatPorts(). |
| src/windows/wslc/services/ContainerService.cpp | Implements FormatPorts(), extracts/frees port data in List(), wires ports through CreateInternal. |
| src/windows/wslc/tasks/ContainerTasks.cpp | Parses -p/--publish, adds PORTS column to table output. |
| test/windows/WSLATests.cpp | Adds PortsCount assertions and new ListContainers port verification block in PortMappingsNat. |
You can also share your feedback on Copilot code review. Take the survey.
… into user/beenachauhan/container-port-mappings
There was a problem hiding this comment.
Pull request overview
Adds port mapping visibility end-to-end so wsl --container list can display published ports in a new PORTS column, backed by service-side ListContainers data.
Changes:
- Extends
WSLAContainerEntry(IDL) to includePorts/PortsCountand populates that data inWSLASession::ListContainers. - Propagates ports through the CLI model and adds formatting + a
PORTScolumn to table output. - Updates/extends WSLA tests to validate port mappings are returned by
ListContainers.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| test/windows/WSLATests.cpp | Adds assertions for PortsCount and validates port mapping data returned by ListContainers. |
| src/windows/wslc/tasks/ContainerTasks.cpp | Adds PORTS column to container list table output. |
| src/windows/wslc/services/ContainerService.h | Declares FormatPorts() helper for CLI display. |
| src/windows/wslc/services/ContainerService.cpp | Implements FormatPorts() and extracts/frees port mappings returned from ListContainers. |
| src/windows/wslc/services/ContainerModel.h | Adds PortInformation to the CLI model and includes it in JSON serialization. |
| src/windows/wslasession/WSLASession.cpp | Allocates and returns port mapping arrays per container in ListContainers, with cleanup on error. |
| src/windows/wslasession/WSLAContainer.h | Exposes GetPorts() accessor on container implementation. |
| src/windows/wslasession/WSLAContainer.cpp | Implements GetPorts() accessor. |
| src/windows/service/inc/wslaservice.idl | Extends WSLAContainerEntry with Ports and PortsCount. |
… into user/beenachauhan/container-port-mappings
| VERIFY_ARE_EQUAL(2, containerPorts.size()); | ||
| VERIFY_ARE_EQUAL(1234, containerPorts[0].HostPort); | ||
| VERIFY_ARE_EQUAL(8000, containerPorts[0].ContainerPort); | ||
| VERIFY_ARE_EQUAL(AF_INET, containerPorts[0].Family); | ||
| VERIFY_ARE_EQUAL(1234, containerPorts[1].HostPort); | ||
| VERIFY_ARE_EQUAL(8000, containerPorts[1].ContainerPort); | ||
| VERIFY_ARE_EQUAL(AF_INET6, containerPorts[1].Family); |
There was a problem hiding this comment.
This new assertion block assumes the port mappings are returned in a stable order (containerPorts[0] is AF_INET and [1] is AF_INET6). Since ListContainers builds allPorts by iterating internal vectors, the ordering may not be guaranteed and could lead to flaky tests. Please match port mappings by key (e.g., find entries by Family/HostPort/ContainerPort/Protocol) or sort the results before indexing.
| // TODO: Consider using standard protocol numbers instead of our own enum. | ||
| convertedPort.Protocol = internalPort.protocol == WSLC_PORT_PROTOCOL_TCP ? IPPROTO_TCP : IPPROTO_UDP; | ||
| convertedPort.BindingAddress = "127.0.0.1"; | ||
| strcpy_s(convertedPort.BindingAddress, "127.0.0.1"); |
There was a problem hiding this comment.
strcpy_s returns an error code that is ignored here. Even though the source literal is currently small enough, it’s safer/consistent with other updates in this PR to check the return value (or use a helper that throws on failure) so unexpected future changes (e.g., different default address) don’t silently truncate or fail.
| strcpy_s(convertedPort.BindingAddress, "127.0.0.1"); | |
| errno_t copyResult = strcpy_s(convertedPort.BindingAddress, "127.0.0.1"); | |
| if (copyResult != 0) | |
| { | |
| return HRESULT_FROM_WIN32(copyResult); | |
| } |
| typedef struct _WSLAContainerEntry | ||
| { | ||
| char Name[WSLA_MAX_CONTAINER_NAME_LENGTH + 1]; | ||
| char Image[WSLA_MAX_IMAGE_NAME_LENGTH + 1]; | ||
| WSLAContainerId Id; | ||
| ULONGLONG StateChangedAt; | ||
| ULONGLONG CreatedAt; | ||
| WSLAContainerState State; | ||
| } WSLAContainerEntry; | ||
|
|
||
| typedef struct _WSLAContainerPortMapping | ||
| { | ||
| WSLAContainerId Id; | ||
| WSLAPortMapping PortMapping; | ||
| } WSLAContainerPortMapping; |
There was a problem hiding this comment.
PR description says ports were added as fields on WSLAContainerEntry and that a WSLAPortProtocol enum was added in the IDL. In the actual IDL change, WSLAContainerEntry is unchanged and a separate WSLAContainerPortMapping array is introduced, and the protocol is still an int. Please update the PR description to match the implemented contract (or adjust the IDL to match the stated design) to avoid confusion for SDK/service consumers.
|
|
||
| *Count = 0; | ||
| *Containers = nullptr; | ||
| *Ports = nullptr; | ||
| *PortsCount = 0; |
There was a problem hiding this comment.
WSLASession::ListContainers dereferences all four out-parameters unconditionally (*Count = 0, etc.). Other methods in this file use THROW_HR_IF(E_POINTER, ...) / THROW_HR_IF_NULL to validate pointers before dereferencing. Please add E_POINTER validation for Containers, Count, Ports, and PortsCount up front so a bad caller doesn’t AV the COM server process.
Summary of the Pull Request
Add port mappings to container list output. When containers are created with
-p, the PORTS column incontainer listnow displays the mapped ports.PR Checklist
Detailed Description of the Pull Request / Additional comments
PortsandPortsCountfields toWSLAContainerEntryin the IDLWSLAPortProtocolenum (TCP/UDP) andProtocolfield toWSLAPortMappingin the IDLListContainerson the service sidePortInformationstruct andFormatPorts()on the CLI sidecontainer listtable output-p/--publishargument to pass ports during container creation viaAddPort()Validation Steps Performed
Ran
PortMappingsNattest — verifiedListContainersreturns correct HostPort, ContainerPort, and Family for both AF_INET and AF_INET6 mappings, across Bridged and Host network modes