-
Notifications
You must be signed in to change notification settings - Fork 257
fix: fixing Stateless CNI delete in SwiftV2 scenario #3967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
behzad-mir
wants to merge
2
commits into
master
Choose a base branch
from
swiftv2-stateless
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+344
−58
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package network | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"sync" | ||
"time" | ||
|
@@ -19,6 +20,7 @@ import ( | |
"github.com/Azure/azure-container-networking/netlink" | ||
"github.com/Azure/azure-container-networking/platform" | ||
"github.com/Azure/azure-container-networking/store" | ||
cniSkel "github.com/containernetworking/cni/pkg/skel" | ||
"github.com/pkg/errors" | ||
"go.uber.org/zap" | ||
) | ||
|
@@ -116,11 +118,13 @@ type NetworkManager interface { | |
UpdateEndpoint(networkID string, existingEpInfo *EndpointInfo, targetEpInfo *EndpointInfo) error | ||
GetNumberOfEndpoints(ifName string, networkID string) int | ||
GetEndpointID(containerID, ifName string) string | ||
GetEndpointIDByNicType(containerID, ifName string, nicType cns.NICType) string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: GetEndpointIDByNICType as nic is an acronym |
||
IsStatelessCNIMode() bool | ||
SaveState(eps []*endpoint) error | ||
DeleteState(epInfos []*EndpointInfo) error | ||
GetEndpoint(networkID string, args *cniSkel.CmdArgs) ([]*EndpointInfo, error) | ||
GetEndpointInfosFromContainerID(containerID string) []*EndpointInfo | ||
GetEndpointState(networkID, containerID string) ([]*EndpointInfo, error) | ||
GetEndpointState(networkID, containerID, netns string) ([]*EndpointInfo, error) | ||
} | ||
|
||
// Creates a new network manager. | ||
|
@@ -455,7 +459,7 @@ func validateUpdateEndpointState(endpointID string, ifNameToIPInfoMap map[string | |
// GetEndpointState will make a call to CNS GetEndpointState API in the stateless CNI mode to fetch the endpointInfo | ||
// TODO unit tests need to be added, WorkItem: 26606939 | ||
// In stateless cni, container id is the endpoint id, so you can pass in either | ||
func (nm *networkManager) GetEndpointState(networkID, containerID string) ([]*EndpointInfo, error) { | ||
func (nm *networkManager) GetEndpointState(networkID, containerID, netns string) ([]*EndpointInfo, error) { | ||
endpointResponse, err := nm.CnsClient.GetEndpoint(context.TODO(), containerID) | ||
if err != nil { | ||
if endpointResponse.Response.ReturnCode == types.NotFound { | ||
|
@@ -466,7 +470,7 @@ func (nm *networkManager) GetEndpointState(networkID, containerID string) ([]*En | |
} | ||
return nil, ErrGetEndpointStateFailure | ||
} | ||
epInfos := cnsEndpointInfotoCNIEpInfos(endpointResponse.EndpointInfo, containerID) | ||
epInfos := cnsEndpointInfotoCNIEpInfos(endpointResponse.EndpointInfo, containerID, netns) | ||
|
||
for i := 0; i < len(epInfos); i++ { | ||
if epInfos[i].NICType == cns.InfraNIC { | ||
|
@@ -514,7 +518,7 @@ func (nm *networkManager) DeleteEndpointState(networkID string, epInfo *Endpoint | |
nw := &network{ | ||
Id: networkID, // currently unused in stateless cni | ||
HnsId: epInfo.HNSNetworkID, | ||
behzad-mir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Mode: opModeTransparentVlan, | ||
Mode: opModeTransparent, | ||
behzad-mir marked this conversation as resolved.
Show resolved
Hide resolved
behzad-mir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SnatBridgeIP: "", | ||
NetNs: dummyGUID, // to trigger hns v2, windows | ||
extIf: &externalInterface{ | ||
|
@@ -529,6 +533,7 @@ func (nm *networkManager) DeleteEndpointState(networkID string, epInfo *Endpoint | |
HNSNetworkID: epInfo.HNSNetworkID, // unused (we use nw.HnsId for deleting the network) | ||
HostIfName: epInfo.HostIfName, | ||
LocalIP: "", | ||
behzad-mir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
IPAddresses: epInfo.IPAddresses, | ||
VlanID: 0, | ||
AllowInboundFromHostToNC: false, // stateless currently does not support apipa | ||
AllowInboundFromNCToHost: false, | ||
|
@@ -537,11 +542,12 @@ func (nm *networkManager) DeleteEndpointState(networkID string, epInfo *Endpoint | |
NetworkContainerID: epInfo.NetworkContainerID, // we don't use this as long as AllowInboundFromHostToNC and AllowInboundFromNCToHost are false | ||
NetNs: dummyGUID, // to trigger hnsv2, windows | ||
NICType: epInfo.NICType, | ||
NetworkNameSpace: epInfo.NetNsPath, | ||
IfName: epInfo.IfName, // TODO: For stateless cni linux populate IfName here to use in deletion in secondary endpoint client | ||
behzad-mir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
logger.Info("Deleting endpoint with", zap.String("Endpoint Info: ", epInfo.PrettyString()), zap.String("HNISID : ", ep.HnsId)) | ||
|
||
err := nw.deleteEndpointImpl(netlink.NewNetlink(), platform.NewExecClient(logger), nil, nil, nil, nil, nil, ep) | ||
err := nw.deleteEndpointImpl(nm.netlink, nm.plClient, nil, nm.netio, nm.nsClient, nm.iptablesClient, nm.dhcpClient, ep) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -562,7 +568,7 @@ func (nm *networkManager) GetEndpointInfo(networkID, endpointID string) (*Endpoi | |
|
||
if nm.IsStatelessCNIMode() { | ||
logger.Info("calling cns getEndpoint API") | ||
epInfos, err := nm.GetEndpointState(networkID, endpointID) | ||
epInfos, err := nm.GetEndpointState(networkID, endpointID, "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -745,6 +751,16 @@ func (nm *networkManager) GetEndpointID(containerID, ifName string) string { | |
return containerID + "-" + ifName | ||
} | ||
|
||
// GetEndpointIDByNicType returns a unique endpoint ID based on the CNI mode and NIC type. | ||
func (nm *networkManager) GetEndpointIDByNicType(containerID, ifName string, nicType cns.NICType) string { | ||
// For stateless CNI, secondary NICs use containerID-ifName as endpointID. | ||
if nm.IsStatelessCNIMode() && nicType != cns.InfraNIC { | ||
return containerID + "-" + ifName | ||
} | ||
// For InfraNIC, use GetEndpointID() logic. | ||
return nm.GetEndpointID(containerID, ifName) | ||
} | ||
|
||
// saves the map of network ids to endpoints to the state file | ||
func (nm *networkManager) SaveState(eps []*endpoint) error { | ||
nm.Lock() | ||
|
@@ -779,7 +795,7 @@ func (nm *networkManager) DeleteState(_ []*EndpointInfo) error { | |
} | ||
|
||
// called to convert a cns restserver EndpointInfo into a network EndpointInfo | ||
func cnsEndpointInfotoCNIEpInfos(endpointInfo restserver.EndpointInfo, endpointID string) []*EndpointInfo { | ||
func cnsEndpointInfotoCNIEpInfos(endpointInfo restserver.EndpointInfo, endpointID, netns string) []*EndpointInfo { | ||
ret := []*EndpointInfo{} | ||
|
||
for ifName, ipInfo := range endpointInfo.IfnameToIPMap { | ||
|
@@ -809,6 +825,10 @@ func cnsEndpointInfotoCNIEpInfos(endpointInfo restserver.EndpointInfo, endpointI | |
epInfo.NICType = ipInfo.NICType | ||
epInfo.HNSNetworkID = ipInfo.HnsNetworkID | ||
epInfo.MacAddress = net.HardwareAddr(ipInfo.MacAddress) | ||
// fill out the netns if it is empty via args passed by container runtime | ||
if epInfo.NetNsPath == "" { | ||
epInfo.NetNsPath = netns | ||
} | ||
ret = append(ret, epInfo) | ||
} | ||
return ret | ||
|
@@ -847,3 +867,54 @@ func generateCNSIPInfoMap(eps []*endpoint) map[string]*restserver.IPInfo { | |
|
||
return ifNametoIPInfoMap | ||
} | ||
|
||
func (nm *networkManager) GetEndpoint(networkID string, args *cniSkel.CmdArgs) ([]*EndpointInfo, error) { | ||
if nm.IsStatelessCNIMode() { | ||
logger.Info("calling cns getEndpoint API") | ||
epInfos, err := nm.GetEndpointState(networkID, args.ContainerID, args.Netns) | ||
if err != nil { | ||
switch { | ||
case errors.Is(err, ErrConnectionFailure): | ||
logger.Error("Failed to connect to CNS", zap.Error(err)) | ||
logger.Info("Endpoint will be deleted from state file asynchronously", zap.String("containerID", args.ContainerID)) | ||
// In SwiftV2 Linux stateless CNI mode, if the plugin cannot connect to CNS, | ||
// we still have to remove the secondary (delegated) interface from the pod’s network namespace in the absence of the endpoint state. | ||
// This is necessary because leaving the delegated NIC in the pod netns can cause the kernel to block rtnetlink operations. | ||
// When that happens, kubelet and containerd hang during sandbox creation or teardown. | ||
// The delegated NIC (SR-IOV VF) used by SwiftV2 for multitenant pods remains tied to the pod namespace, | ||
// triggering hot-unplug/re-register events and leaving the node in an unhealthy state. | ||
// This workaround mitigates the issue by generating a minimal endpointInfo via containerd args and netlink APIs that can be then passed to DeleteEndpoint API. | ||
epInfos, err = nm.generateEndpointLocally(args) | ||
if err != nil { | ||
logger.Error("Failed to fetch secondary endpoint from pod netns", zap.String("netns", args.Netns), zap.Error(err)) | ||
return nil, fmt.Errorf("failed to fetch secondary interfaces: %w", err) | ||
} | ||
case errors.Is(err, ErrEndpointStateNotFound): | ||
logger.Info("Endpoint Not found", zap.String("containerID", args.ContainerID), zap.Error(err)) | ||
return nil, nil | ||
default: | ||
logger.Error("Get Endpoint State API returned error", zap.String("containerID", args.ContainerID), zap.Error(err)) | ||
return nil, ErrEndpointRetrievalFailure | ||
} | ||
} | ||
for _, epInfo := range epInfos { | ||
logger.Info("Found endpoint to delete", zap.String("IfName", epInfo.IfName), zap.String("EndpointID", epInfo.EndpointID), zap.Any("NICType", epInfo.NICType)) | ||
} | ||
return epInfos, nil | ||
} | ||
return nm.GetEndpointInfosFromContainerID(args.ContainerID), nil | ||
} | ||
|
||
// generateEndpointLocally fetches the endpoint information using containerd args and netlink APIs | ||
func (nm *networkManager) generateEndpointLocally(args *cniSkel.CmdArgs) ([]*EndpointInfo, error) { | ||
ep := &endpoint{ | ||
Id: args.ContainerID, | ||
NetworkNameSpace: args.Netns, | ||
IfName: args.IfName, // TODO: For stateless cni linux populate IfName here to use in deletion in secondary endpoint client | ||
} | ||
epInfo, err := nm.getEndpointInfoByIfNameImpl(ep) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to fetch secondary interfaces: %w", err) | ||
} | ||
return epInfo, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.