Skip to content

Support attaching and detaching containers to networks after creation#40549

Open
beena352 wants to merge 2 commits into
microsoft:masterfrom
beena352:user/beenachauhan/wslc-attach-detach-network
Open

Support attaching and detaching containers to networks after creation#40549
beena352 wants to merge 2 commits into
microsoft:masterfrom
beena352:user/beenachauhan/wslc-attach-detach-network

Conversation

@beena352
Copy link
Copy Markdown
Contributor

Summary of the Pull Request

Implements AttachContainerToNetwork and DetachContainerFromNetwork methods on IWSLCSession, enabling containers to join/leave networks after they are created and running.

PR Checklist

  • Closes: Link to issue #xxx
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • [X ] Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

  • Validation order: IpAddress → NetworkName → ValidateName → container lookup → network lookup → mode check
  • Uses existing lock pattern: m_lock.lock_shared() then scoped_lock(m_containersLock, m_networksLock)
  • Cleans up deleted containers via erase_if before lookup
  • Error handling follows DeleteNetwork pattern: local validation with exact errors, Docker errors surfaced via catch-all

Validation Steps Performed

  • AttachDetachContainerNetworkRoundTripTest: Create bridged container → create network → attach → verify via inspect (network appears with IP) → detach → verify gone
  • AttachContainerEmptyNetworkNameTest: Empty NetworkName → E_INVALIDARG
  • AttachContainerNonexistentNetworkTest: Nonexistent network → WSLC_E_NETWORK_NOT_FOUND
  • AttachHostOrNoneModeContainerRejectedTest: Host and None mode containers → E_INVALIDARG
  • AttachContainerWithIpAddressRejectedTest: ContainerIpAddress set → E_NOTIMPL
  • AttachNonexistentContainerToNetworkTest: Nonexistent container → WSLC_E_CONTAINER_NOT_FOUND
  • DetachContainerNonexistentNetworkTest: Nonexistent network in detach → WSLC_E_NETWORK_NOT_FOUND
  • DetachNonexistentContainerFromNetworkTest: Nonexistent container in detach → WSLC_E_CONTAINER_NOT_FOUND

@beena352 beena352 marked this pull request as ready for review May 15, 2026 16:10
@beena352 beena352 requested a review from a team as a code owner May 15, 2026 16:10
HRESULT DeleteNetwork([in] LPCSTR Name);
HRESULT ListNetworks([out, size_is(, *Count)] WSLCNetworkInformation** Networks, [out] ULONG* Count);
HRESULT InspectNetwork([in] LPCSTR Name, [out] LPSTR* Output);
HRESULT AttachContainerToNetwork([in] LPCSTR ContainerId, [in] const WSLCNetworkAttachment* Attachment);
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.

I would recommend moving those methods to WSLCContainer instead. That way we don't need to perform the container lookup in the method's implementation. That will also take care of container names and id prefixes for us

THROW_HR_WITH_USER_ERROR_IF(
WSLC_E_CONTAINER_NOT_FOUND, Localization::MessageWslcContainerNotFound(containerId), containerIt == m_containers.end());

auto networkIt = m_networks.find(networkName);
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.

Could we just let docker handle the network not found error for us ? That would allow us to easily support the container:<id> case

…eenachauhan/wslc-attach-detach-network

# Conflicts:
#	test/windows/WSLCTests.cpp
Copilot AI review requested due to automatic review settings May 16, 2026 00:10
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds WSLC APIs intended to attach and detach already-created containers from WSLC-managed Docker networks, plus tests for the new attach/detach behavior.

Changes:

  • Adds session-level AttachContainerToNetwork and DetachContainerFromNetwork declarations and implementations.
  • Adds Docker HTTP client helpers and request schema types for network connect/disconnect endpoints.
  • Adds WSLC tests covering round-trip attach/detach and several validation paths.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/windows/service/inc/wslc.idl Adds new network attach/detach methods to IWSLCSession.
src/windows/wslcsession/WSLCSession.h Declares the new session methods.
src/windows/wslcsession/WSLCSession.cpp Implements validation, lookup, Docker connect/disconnect calls, and logging.
src/windows/wslcsession/WSLCContainer.h Exposes container network mode to session logic.
src/windows/wslcsession/DockerHTTPClient.h Declares Docker network connect/disconnect helpers.
src/windows/wslcsession/DockerHTTPClient.cpp Implements Docker network connect/disconnect endpoint calls.
src/windows/inc/docker_schema.h Adds request payload schemas for Docker network connect/disconnect.
test/windows/WSLCTests.cpp Adds tests for attach/detach network behavior and validation.
Comments suppressed due to low confidence (4)

test/windows/WSLCTests.cpp:6293

  • This call targets IWSLCContainer::AttachToNetwork, but that method is not declared on IWSLCContainer; the added API is IWSLCSession::AttachContainerToNetwork. Update the test to call through the session with the launched container's ID.
        VERIFY_ARE_EQUAL(E_INVALIDARG, container.Get().AttachToNetwork(&attachment));

test/windows/WSLCTests.cpp:6321

  • This call will not compile because AttachToNetwork is not part of IWSLCContainer; the new attach method is on IWSLCSession. Use the session-level API with this container's ID.
            VERIFY_ARE_EQUAL(E_INVALIDARG, container.Get().AttachToNetwork(&attachment));

test/windows/WSLCTests.cpp:6332

  • This call will not compile because AttachToNetwork is not declared on IWSLCContainer; call the session-level attach API with the container ID instead.
            VERIFY_ARE_EQUAL(E_INVALIDARG, container.Get().AttachToNetwork(&attachment));

test/windows/WSLCTests.cpp:6347

  • This call targets a non-existent IWSLCContainer::AttachToNetwork method. The attach API added in this PR is session-level, so the test should use AttachContainerToNetwork on the session with the container ID.
        VERIFY_ARE_EQUAL(E_NOTIMPL, container.Get().AttachToNetwork(&attachment));

Comment on lines +6269 to +6272

WSLCNetworkAttachment attachment{};
attachment.NetworkName = networkName.c_str();
VERIFY_SUCCEEDED(container.Get().AttachToNetwork(&attachment));
VERIFY_IS_TRUE(inspect.NetworkSettings.Networks.contains(networkName));
VERIFY_IS_FALSE(inspect.NetworkSettings.Networks.at(networkName).IPAddress.empty());

VERIFY_SUCCEEDED(container.Get().DetachFromNetwork(networkName.c_str()));
@@ -790,6 +790,8 @@ interface IWSLCSession : IUnknown
HRESULT DeleteNetwork([in] LPCSTR Name);
HRESULT ListNetworks([out, size_is(, *Count)] WSLCNetworkInformation** Networks, [out] ULONG* Count);
HRESULT InspectNetwork([in] LPCSTR Name, [out] LPSTR* Output);
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