Skip to content

feat: clean up stale HNS resources on NC create for delegated NICs in SwiftV2#4290

Open
jackieluc wants to merge 2 commits intoAzure:masterfrom
jackieluc:jackieluc/swiftv2-hnscleanup
Open

feat: clean up stale HNS resources on NC create for delegated NICs in SwiftV2#4290
jackieluc wants to merge 2 commits intoAzure:masterfrom
jackieluc:jackieluc/swiftv2-hnscleanup

Conversation

@jackieluc
Copy link
Copy Markdown
Member

Reason for Change:

  • Add a EnableStaleHNSCleanupOnNCCreate config flag (also gated behind several conditions: NetworkingContainerType == ACI, ManageEndpointState, and NC has delegated NIC type)
  • Adds stale HNS endpoint/network cleanup during CreateNetworkContainer when a delegated NIC MAC is reused by a new NC. This handles cases where a previous NC was not properly cleaned up (e.g., CNI DEL was never called because of containerd issues), which kept stale HNS resources and blocked new SwiftV2 NC creations due to control plane thinking the NIC was available when in reality it was not.
  • Add a HNSClient for CNS and for testing
  • Emits a telemetry metric on each cleanup attempt with success/failure status.

Testing:

  • Unit tests added for cleanupStaleHNSResources and findEndpointStateByMAC
  • Validated on a SwiftV2 SF cluster with delegated NICs by simulating stale HNS resources and stale endpoint state in CNS

Issue Fixed:

Requirements:

Notes:

@jackieluc jackieluc requested a review from a team as a code owner March 24, 2026 21:58
Copilot AI review requested due to automatic review settings March 24, 2026 21:58
@jackieluc jackieluc requested a review from a team as a code owner March 24, 2026 21:58
@jackieluc jackieluc requested a review from thatmattlong March 24, 2026 21:58
@jackieluc jackieluc added cns Related to CNS. swift-v2 labels Mar 24, 2026
@jackieluc jackieluc requested a review from behzad-mir March 24, 2026 22:01
@jackieluc jackieluc force-pushed the jackieluc/swiftv2-hnscleanup branch from b0f802a to 3a934e6 Compare March 24, 2026 22:04
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

Adds an opt-in mechanism in CNS to proactively clean up stale Windows HNS endpoint/network resources (and corresponding CNS endpoint-state entries) during CreateNetworkContainer for SwiftV2 delegated NIC scenarios, helping unblock NC creation when a delegated NIC/MAC is reused.

Changes:

  • Introduces EnableStaleHNSCleanupOnNCCreate config/option and wires it into CNS service options.
  • Adds stale-resource detection/cleanup helpers (normalizeMAC, findEndpointStateByMAC, cleanupStaleHNSResources) invoked on ACI NC create when delegated NIC info is present and endpoint-state management is enabled.
  • Adds an HNSClient abstraction for deletions (with tests) and emits a cleanup metric.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
common/config.go Adds new option key for enabling stale HNS cleanup on NC create.
cns/configuration/configuration.go Adds config field to toggle stale HNS cleanup behavior.
cns/service/main.go Wires new config flag into CNS service options.
cns/restserver/restserver.go Injects an hnsClient into HTTPRestService for runtime + testability.
cns/restserver/api.go Triggers stale HNS cleanup during ACI CreateNetworkContainer under gating conditions.
cns/restserver/ipam.go Implements MAC normalization, endpoint-state lookup by MAC, cleanup logic, and metric emission.
cns/restserver/ipam_test.go Adds unit tests for MAC lookup and stale HNS cleanup (with a mock HNS client).
cns/logger/constants.go Adds metric name constant for stale HNS cleanup telemetry.
cns/hnsclient/hnsclient.go Introduces HNSClient interface + default implementation.
cns/hnsclient/hnsclient_windows.go Exposes DeleteNetworkByIDHnsV2 wrapper used by the client interface.
cns/hnsclient/hnsclient_linux.go Adds non-Windows stubs for the new delete-by-ID functions.

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

@jackieluc
Copy link
Copy Markdown
Member Author

/azp run Azure Container Networking PR

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@jackieluc jackieluc force-pushed the jackieluc/swiftv2-hnscleanup branch from a7fde2f to f137028 Compare March 24, 2026 22:54
@jackieluc
Copy link
Copy Markdown
Member Author

/azp run Azure Container Networking PR

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 8, 2026

This pull request is stale because it has been open for 2 weeks with no activity. Remove stale label or comment or this will be closed in 7 days

@github-actions github-actions bot added the stale Stale due to inactivity. label Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cns Related to CNS. stale Stale due to inactivity. swift-v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants