Skip to content

Commit 5804dc7

Browse files
committed
fix: fixing Stateless CNI delete in SwiftV2 scenario
1 parent d1a7d21 commit 5804dc7

File tree

5 files changed

+34
-21
lines changed

5 files changed

+34
-21
lines changed

cni/network/network.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,11 +712,11 @@ func (plugin *NetPlugin) createEpInfo(opt *createEpInfoOpt) (*network.EndpointIn
712712
if opt.ifInfo.NICType == cns.InfraNIC && !*opt.infraSeen {
713713
// so we do not break existing scenarios, only the first infra gets the original endpoint id generation
714714
ifName = opt.args.IfName
715-
endpointID = plugin.nm.GetEndpointID(opt.args.ContainerID, ifName)
715+
endpointID = plugin.nm.GetEndpointID(opt.args.ContainerID, ifName, opt.ifInfo.NICType)
716716
*opt.infraSeen = true
717717
} else {
718718
ifName = "eth" + strconv.Itoa(opt.endpointIndex)
719-
endpointID = plugin.nm.GetEndpointID(opt.args.ContainerID, ifName)
719+
endpointID = plugin.nm.GetEndpointID(opt.args.ContainerID, ifName, opt.ifInfo.NICType)
720720
}
721721

722722
endpointInfo := network.EndpointInfo{
@@ -1096,7 +1096,7 @@ func (plugin *NetPlugin) Delete(args *cniSkel.CmdArgs) error {
10961096
// for when the endpoint is not created, but the ips are already allocated (only works if single network, single infra)
10971097
// this block is not applied to stateless CNI
10981098
if len(epInfos) == 0 {
1099-
endpointID := plugin.nm.GetEndpointID(args.ContainerID, args.IfName)
1099+
endpointID := plugin.nm.GetEndpointID(args.ContainerID, args.IfName, cns.InfraNIC)
11001100
if !nwCfg.MultiTenancy {
11011101
logger.Warn("Could not query endpoint",
11021102
zap.String("endpoint", endpointID),

cns/restserver/ipam.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1313,12 +1313,16 @@ 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+
}
13161320
}
13171321

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

cns/restserver/restserver.go

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

121121
type IPInfo struct {
122-
IPv4 []net.IPNet
123-
IPv6 []net.IPNet `json:",omitempty"`
124-
HnsEndpointID string `json:",omitempty"`
125-
HnsNetworkID string `json:",omitempty"`
126-
HostVethName string `json:",omitempty"`
127-
MacAddress string `json:",omitempty"`
128-
NICType cns.NICType
122+
IPv4 []net.IPNet
123+
IPv6 []net.IPNet `json:",omitempty"`
124+
HnsEndpointID string `json:",omitempty"`
125+
HnsNetworkID string `json:",omitempty"`
126+
HostVethName string `json:",omitempty"`
127+
MacAddress string `json:",omitempty"`
128+
NICType cns.NICType `json:",omitempty"`
129+
NetworkNameSpace string `json:",omitempty"`
129130
}
130131

131132
type GetHTTPServiceDataResponse struct {

network/manager.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ type NetworkManager interface {
115115
DetachEndpoint(networkID string, endpointID string) error
116116
UpdateEndpoint(networkID string, existingEpInfo *EndpointInfo, targetEpInfo *EndpointInfo) error
117117
GetNumberOfEndpoints(ifName string, networkID string) int
118-
GetEndpointID(containerID, ifName string) string
118+
GetEndpointID(containerID, ifName string, nicType cns.NICType) string
119119
IsStatelessCNIMode() bool
120120
SaveState(eps []*endpoint) error
121121
DeleteState(epInfos []*EndpointInfo) error
@@ -514,7 +514,7 @@ func (nm *networkManager) DeleteEndpointState(networkID string, epInfo *Endpoint
514514
nw := &network{
515515
Id: networkID, // currently unused in stateless cni
516516
HnsId: epInfo.HNSNetworkID,
517-
Mode: opModeTransparentVlan,
517+
Mode: opModeTransparent,
518518
SnatBridgeIP: "",
519519
NetNs: dummyGUID, // to trigger hns v2, windows
520520
extIf: &externalInterface{
@@ -529,6 +529,7 @@ func (nm *networkManager) DeleteEndpointState(networkID string, epInfo *Endpoint
529529
HNSNetworkID: epInfo.HNSNetworkID, // unused (we use nw.HnsId for deleting the network)
530530
HostIfName: epInfo.HostIfName,
531531
LocalIP: "",
532+
IPAddresses: epInfo.IPAddresses,
532533
VlanID: 0,
533534
AllowInboundFromHostToNC: false, // stateless currently does not support apipa
534535
AllowInboundFromNCToHost: false,
@@ -537,11 +538,12 @@ func (nm *networkManager) DeleteEndpointState(networkID string, epInfo *Endpoint
537538
NetworkContainerID: epInfo.NetworkContainerID, // we don't use this as long as AllowInboundFromHostToNC and AllowInboundFromNCToHost are false
538539
NetNs: dummyGUID, // to trigger hnsv2, windows
539540
NICType: epInfo.NICType,
541+
NetworkNameSpace: epInfo.NetNsPath,
540542
IfName: epInfo.IfName, // TODO: For stateless cni linux populate IfName here to use in deletion in secondary endpoint client
541543
}
542544
logger.Info("Deleting endpoint with", zap.String("Endpoint Info: ", epInfo.PrettyString()), zap.String("HNISID : ", ep.HnsId))
543545

544-
err := nw.deleteEndpointImpl(netlink.NewNetlink(), platform.NewExecClient(logger), nil, nil, nil, nil, nil, ep)
546+
err := nw.deleteEndpointImpl(nm.netlink, nm.plClient, nil, nm.netio, nm.nsClient, nm.iptablesClient, nm.dhcpClient, ep)
545547
if err != nil {
546548
return err
547549
}
@@ -732,8 +734,11 @@ func (nm *networkManager) GetNumberOfEndpoints(ifName string, networkId string)
732734
}
733735

734736
// GetEndpointID returns a unique endpoint ID based on the CNI mode.
735-
func (nm *networkManager) GetEndpointID(containerID, ifName string) string {
737+
func (nm *networkManager) GetEndpointID(containerID, ifName string, nicType cns.NICType) string {
736738
if nm.IsStatelessCNIMode() {
739+
if nicType == cns.DelegatedVMNIC {
740+
return containerID + "-" + ifName
741+
}
737742
return containerID
738743
}
739744
if len(containerID) > ContainerIDLength {
@@ -809,6 +814,7 @@ func cnsEndpointInfotoCNIEpInfos(endpointInfo restserver.EndpointInfo, endpointI
809814
epInfo.NICType = ipInfo.NICType
810815
epInfo.HNSNetworkID = ipInfo.HnsNetworkID
811816
epInfo.MacAddress = net.HardwareAddr(ipInfo.MacAddress)
817+
epInfo.NetNsPath = ipInfo.NetworkNameSpace
812818
ret = append(ret, epInfo)
813819
}
814820
return ret
@@ -837,11 +843,12 @@ func generateCNSIPInfoMap(eps []*endpoint) map[string]*restserver.IPInfo {
837843

838844
for _, ep := range eps {
839845
ifNametoIPInfoMap[ep.IfName] = &restserver.IPInfo{ // in windows, the nicname is args ifname, in linux, it's ethX
840-
NICType: ep.NICType,
841-
HnsEndpointID: ep.HnsId,
842-
HnsNetworkID: ep.HNSNetworkID,
843-
HostVethName: ep.HostIfName,
844-
MacAddress: ep.MacAddress.String(),
846+
NICType: ep.NICType,
847+
HnsEndpointID: ep.HnsId,
848+
HnsNetworkID: ep.HNSNetworkID,
849+
HostVethName: ep.HostIfName,
850+
MacAddress: ep.MacAddress.String(),
851+
NetworkNameSpace: ep.NetworkNameSpace,
845852
}
846853
}
847854

network/manager_mock.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package network
22

33
import (
4+
"github.com/Azure/azure-container-networking/cns"
45
"github.com/Azure/azure-container-networking/common"
56
)
67

@@ -82,7 +83,7 @@ func (nm *MockNetworkManager) IsStatelessCNIMode() bool {
8283
}
8384

8485
// GetEndpointID returns the ContainerID value
85-
func (nm *MockNetworkManager) GetEndpointID(containerID, ifName string) string {
86+
func (nm *MockNetworkManager) GetEndpointID(containerID, ifName string, _ cns.NICType) string {
8687
if nm.IsStatelessCNIMode() {
8788
return containerID
8889
}

0 commit comments

Comments
 (0)