Skip to content

Commit a6b378e

Browse files
committed
fix: Fixing stateless CNI delete flow
1 parent 305303f commit a6b378e

File tree

6 files changed

+25
-29
lines changed

6 files changed

+25
-29
lines changed

cni/network/network.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,11 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
10851085
logger.Error("Get Endpoint State API returned error", zap.String("containerID", args.ContainerID), zap.Error(err))
10861086
return plugin.RetriableError(fmt.Errorf("failed to delete endpoint: %w", err))
10871087
}
1088+
} else {
1089+
for i, epInfo := range epInfos {
1090+
logger.Info("Found endpoint to delete", zap.String("IfName", epInfo.IfName), zap.String("EndpointID", epInfo.EndpointID), zap.Any("NICType", epInfo.NICType))
1091+
epInfos[i].NetNsPath = args.Netns // in case DelegatedNIC need to be moved to host namespace
1092+
}
10881093
}
10891094
} else {
10901095
epInfos = plugin.nm.GetEndpointInfosFromContainerID(args.ContainerID)

cns/restserver/ipam.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,16 +1313,12 @@ func updateIPInfoMap(iPInfo map[string]*IPInfo, interfaceInfo *IPInfo, ifName, e
13131313
iPInfo[ifName].MacAddress = interfaceInfo.MacAddress
13141314
logger.Printf("[updateEndpoint] update the endpoint %s with MacAddress %s", endpointID, interfaceInfo.MacAddress)
13151315
}
1316-
if interfaceInfo.NetworkNameSpace != "" {
1317-
iPInfo[ifName].NetworkNameSpace = interfaceInfo.NetworkNameSpace
1318-
logger.Printf("[updateEndpoint] update the endpoint %s with NetworkNameSpace %s", endpointID, interfaceInfo.NetworkNameSpace)
1319-
}
13201316
}
13211317

13221318
// verifyUpdateEndpointStateRequest verify the CNI request body for the UpdateENdpointState API
13231319
func verifyUpdateEndpointStateRequest(req map[string]*IPInfo) error {
13241320
for ifName, InterfaceInfo := range req {
1325-
if InterfaceInfo.HostVethName == "" && InterfaceInfo.HnsEndpointID == "" && InterfaceInfo.NICType == "" && InterfaceInfo.MacAddress == "" && InterfaceInfo.NetworkNameSpace == "" {
1321+
if InterfaceInfo.HostVethName == "" && InterfaceInfo.HnsEndpointID == "" && InterfaceInfo.NICType == "" && InterfaceInfo.MacAddress == "" {
13261322
return errors.New("[updateEndpoint] No NicType, MacAddress, HnsEndpointID or HostVethName has been provided")
13271323
}
13281324
if ifName == "" {

cns/restserver/restserver.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,13 @@ type EndpointInfo struct {
126126
}
127127

128128
type IPInfo struct {
129-
IPv4 []net.IPNet
130-
IPv6 []net.IPNet `json:",omitempty"`
131-
HnsEndpointID string `json:",omitempty"`
132-
HnsNetworkID string `json:",omitempty"`
133-
HostVethName string `json:",omitempty"`
134-
MacAddress string `json:",omitempty"`
135-
NICType cns.NICType `json:",omitempty"`
136-
NetworkNameSpace string `json:",omitempty"`
129+
IPv4 []net.IPNet
130+
IPv6 []net.IPNet `json:",omitempty"`
131+
HnsEndpointID string `json:",omitempty"`
132+
HnsNetworkID string `json:",omitempty"`
133+
HostVethName string `json:",omitempty"`
134+
MacAddress string `json:",omitempty"`
135+
NICType cns.NICType `json:",omitempty"`
137136
}
138137

139138
type GetHTTPServiceDataResponse struct {

network/manager.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,6 @@ func cnsEndpointInfotoCNIEpInfos(endpointInfo restserver.EndpointInfo, endpointI
822822
epInfo.NICType = ipInfo.NICType
823823
epInfo.HNSNetworkID = ipInfo.HnsNetworkID
824824
epInfo.MacAddress = net.HardwareAddr(ipInfo.MacAddress)
825-
epInfo.NetNsPath = ipInfo.NetworkNameSpace
826825
ret = append(ret, epInfo)
827826
}
828827
return ret
@@ -851,12 +850,11 @@ func generateCNSIPInfoMap(eps []*endpoint) map[string]*restserver.IPInfo {
851850

852851
for _, ep := range eps {
853852
ifNametoIPInfoMap[ep.IfName] = &restserver.IPInfo{ // in windows, the nicname is args ifname, in linux, it's ethX
854-
NICType: ep.NICType,
855-
HnsEndpointID: ep.HnsId,
856-
HnsNetworkID: ep.HNSNetworkID,
857-
HostVethName: ep.HostIfName,
858-
MacAddress: ep.MacAddress.String(),
859-
NetworkNameSpace: ep.NetworkNameSpace,
853+
NICType: ep.NICType,
854+
HnsEndpointID: ep.HnsId,
855+
HnsNetworkID: ep.HNSNetworkID,
856+
HostVethName: ep.HostIfName,
857+
MacAddress: ep.MacAddress.String(),
860858
}
861859
}
862860

network/manager_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -548,22 +548,20 @@ func TestCnsEndpointInfotoCNIEpInfos_Cases(t *testing.T) {
548548
expectedNICType cns.NICType
549549
}{
550550
{
551-
name: "With NetworkNameSpace and DelegatedVMNIC",
551+
name: "DelegatedVMNIC",
552552
ifName: "eth1",
553553
ipInfo: restserver.IPInfo{
554-
NetworkNameSpace: "/var/run/netns/testns",
555-
NICType: cns.DelegatedVMNIC,
554+
NICType: cns.DelegatedVMNIC,
556555
},
557556
expectedNetNs: "/var/run/netns/testns",
558557
expectedIfName: "eth1",
559558
expectedNICType: cns.DelegatedVMNIC,
560559
},
561560
{
562-
name: "Empty NetworkNameSpace and InfraNIC",
561+
name: "InfraNIC",
563562
ifName: "eth0",
564563
ipInfo: restserver.IPInfo{
565-
NetworkNameSpace: "",
566-
NICType: cns.InfraNIC,
564+
NICType: cns.InfraNIC,
567565
},
568566
expectedNetNs: "",
569567
expectedIfName: "eth0",

network/secondary_endpoint_client_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ func (client *SecondaryEndpointClient) DeleteEndpoints(ep *endpoint) error {
191191
}()
192192
// For stateless cni linux, check if delegated vmnic type, and if so, delete using this *endpoint* struct's ifname
193193
if ep.NICType == cns.DelegatedVMNIC {
194-
if err := client.moveInterfaceToHostNetns(ep, ep.IfName, vmns); err != nil {
194+
if err := client.moveInterfaceToHostNetns(ep.IfName, vmns); err != nil {
195195
logger.Error("Failed to move interface", zap.String("IfName", ep.IfName), zap.Error(newErrorSecondaryEndpointClient(err)))
196196
}
197197
}
198198
// For Statefull cni linux, Use SecondaryInterfaces map to move all interfaces to host netns
199199
// TODO: SecondaryInterfaces map should be retired and only IfName field and NICType should be used to determine the delgated NIC
200200
for iface := range ep.SecondaryInterfaces {
201-
if err := client.moveInterfaceToHostNetns(ep, iface, vmns); err != nil {
201+
if err := client.moveInterfaceToHostNetns(iface, vmns); err != nil {
202202
logger.Error("Failed to move interface", zap.String("IfName", iface), zap.Error(newErrorSecondaryEndpointClient(err)))
203203
continue
204204
}
@@ -209,7 +209,7 @@ func (client *SecondaryEndpointClient) DeleteEndpoints(ep *endpoint) error {
209209
}
210210

211211
// moveInterfaceToHostNetns moves the given interface to the host netns.
212-
func (client *SecondaryEndpointClient) moveInterfaceToHostNetns(ep *endpoint, ifName string, vmns int) error {
212+
func (client *SecondaryEndpointClient) moveInterfaceToHostNetns(ifName string, vmns int) error {
213213
logger.Info("Moving interface to host netns", zap.String("IfName", ifName))
214214
if err := client.netlink.SetLinkNetNs(ifName, uintptr(vmns)); err != nil {
215215
return newErrorSecondaryEndpointClient(err)

0 commit comments

Comments
 (0)