Skip to content

Commit

Permalink
fix: pass right interface name to generate hns endpoint name as expec…
Browse files Browse the repository at this point in the history
…ted (Azure#2915)

* fix endpointNames

* fix comments

* fix an linter issue

* remove network import on UT

* fix linter: rename local var

* remove the UT

* fix UTs

* start endpointIndex from 1
  • Loading branch information
paulyufan2 authored Aug 13, 2024
1 parent a39d8c4 commit ea43a60
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
14 changes: 8 additions & 6 deletions cni/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ func (plugin *NetPlugin) Add(args *cniSkel.CmdArgs) error {
}()

infraSeen := false
endpointIndex := 0
endpointIndex := 1
for key := range ipamAddResult.interfaceInfo {
ifInfo := ipamAddResult.interfaceInfo[key]

Expand Down Expand Up @@ -732,7 +732,6 @@ func (plugin *NetPlugin) createEpInfo(opt *createEpInfoOpt) (*network.EndpointIn
// populate endpoint info
epDNSInfo, err := getEndpointDNSSettings(opt.nwCfg, opt.ifInfo.DNS, opt.k8sNamespace) // Probably won't panic if given bad values
if err != nil {

err = plugin.Errorf("Failed to getEndpointDNSSettings: %v", err)
return nil, err
}
Expand All @@ -753,13 +752,16 @@ func (plugin *NetPlugin) createEpInfo(opt *createEpInfoOpt) (*network.EndpointIn
}

// generate endpoint info
var endpointID string
var endpointID, ifName string

if opt.ifInfo.NICType == cns.InfraNIC && !*opt.infraSeen {
// so we do not break existing scenarios, only the first infra gets the original endpoint id generation
endpointID = plugin.nm.GetEndpointID(opt.args.ContainerID, opt.args.IfName)
ifName = opt.args.IfName
endpointID = plugin.nm.GetEndpointID(opt.args.ContainerID, ifName)
*opt.infraSeen = true
} else {
endpointID = plugin.nm.GetEndpointID(opt.args.ContainerID, strconv.Itoa(opt.endpointIndex))
ifName = "eth" + strconv.Itoa(opt.endpointIndex)
endpointID = plugin.nm.GetEndpointID(opt.args.ContainerID, ifName)
}

endpointInfo := network.EndpointInfo{
Expand All @@ -777,7 +779,7 @@ func (plugin *NetPlugin) createEpInfo(opt *createEpInfoOpt) (*network.EndpointIn
EndpointID: endpointID,
ContainerID: opt.args.ContainerID,
NetNsPath: opt.args.Netns, // probably same value as epInfo.NetNs
IfName: opt.args.IfName,
IfName: ifName,
Data: make(map[string]interface{}),
EndpointDNS: epDNSInfo,
// endpoint policies are populated later
Expand Down
40 changes: 20 additions & 20 deletions cni/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1392,12 +1392,12 @@ func TestPluginSwiftV2MultipleAddDelete(t *testing.T) {
Plugin: plugin,
nm: acnnetwork.NewMockNetworkmanager(acnnetwork.NewMockEndpointClient(nil)),
ipamInvoker: NewCustomMockIpamInvoker(map[string]acnnetwork.InterfaceInfo{
"eth0-1": {
NICType: cns.NodeNetworkInterfaceFrontendNIC,
},
"eth0": {
NICType: cns.InfraNIC,
},
"eth2": {
NICType: cns.NodeNetworkInterfaceFrontendNIC,
},
}),
report: &telemetry.CNIReport{},
tb: &telemetry.TelemetryBuffer{},
Expand All @@ -1417,7 +1417,7 @@ func TestPluginSwiftV2MultipleAddDelete(t *testing.T) {
Plugin: plugin,
nm: acnnetwork.NewMockNetworkmanager(acnnetwork.NewMockEndpointClient(nil)),
ipamInvoker: NewCustomMockIpamInvoker(map[string]acnnetwork.InterfaceInfo{
"eth0-1": {
"eth1": {
NICType: cns.NodeNetworkInterfaceAccelnetFrontendNIC,
},
"eth0": {
Expand All @@ -1442,15 +1442,15 @@ func TestPluginSwiftV2MultipleAddDelete(t *testing.T) {
Plugin: plugin,
nm: acnnetwork.NewMockNetworkmanager(acnnetwork.NewMockEndpointClient(nil)),
ipamInvoker: NewCustomMockIpamInvoker(map[string]acnnetwork.InterfaceInfo{
"eth0-2": {
"eth0": {
NICType: cns.InfraNIC,
},
"eth1": {
NICType: cns.NodeNetworkInterfaceAccelnetFrontendNIC,
},
"eth0-1": {
"eth2": {
NICType: cns.NodeNetworkInterfaceFrontendNIC,
},
"eth0": {
NICType: cns.InfraNIC,
},
}),
report: &telemetry.CNIReport{},
tb: &telemetry.TelemetryBuffer{},
Expand All @@ -1470,12 +1470,12 @@ func TestPluginSwiftV2MultipleAddDelete(t *testing.T) {
Plugin: plugin,
nm: acnnetwork.NewMockNetworkmanager(acnnetwork.NewMockEndpointClient(nil)),
ipamInvoker: NewCustomMockIpamInvoker(map[string]acnnetwork.InterfaceInfo{
"eth0-1": {
NICType: cns.BackendNIC,
},
"eth0": {
NICType: cns.InfraNIC,
},
"eth1": {
NICType: cns.BackendNIC,
},
}),
report: &telemetry.CNIReport{},
tb: &telemetry.TelemetryBuffer{},
Expand All @@ -1495,10 +1495,10 @@ func TestPluginSwiftV2MultipleAddDelete(t *testing.T) {
Plugin: plugin,
nm: acnnetwork.NewMockNetworkmanager(acnnetwork.NewMockEndpointClient(nil)),
ipamInvoker: NewCustomMockIpamInvoker(map[string]acnnetwork.InterfaceInfo{
"eth0": {
"eth1": {
NICType: cns.NodeNetworkInterfaceFrontendNIC,
},
"eth0-1": {
"eth2": {
NICType: cns.NodeNetworkInterfaceFrontendNIC,
},
}),
Expand All @@ -1520,10 +1520,10 @@ func TestPluginSwiftV2MultipleAddDelete(t *testing.T) {
Plugin: plugin,
nm: acnnetwork.NewMockNetworkmanager(acnnetwork.NewMockEndpointClient(nil)),
ipamInvoker: NewCustomMockIpamInvoker(map[string]acnnetwork.InterfaceInfo{
"eth0": {
"eth1": {
NICType: cns.NodeNetworkInterfaceAccelnetFrontendNIC,
},
"eth0-1": {
"eth2": {
NICType: cns.NodeNetworkInterfaceAccelnetFrontendNIC,
},
}),
Expand Down Expand Up @@ -1556,7 +1556,7 @@ func TestPluginSwiftV2MultipleAddDelete(t *testing.T) {
"eth0": {
NICType: cns.InfraNIC,
},
"eth0-1": {
"eth1": {
NICType: cns.NodeNetworkInterfaceFrontendNIC,
},
}),
Expand Down Expand Up @@ -1588,7 +1588,7 @@ func TestPluginSwiftV2MultipleAddDelete(t *testing.T) {
"eth0": {
NICType: cns.NodeNetworkInterfaceFrontendNIC,
},
"eth0-1": {
"eth1": {
NICType: cns.NodeNetworkInterfaceAccelnetFrontendNIC,
},
}),
Expand Down Expand Up @@ -1620,7 +1620,7 @@ func TestPluginSwiftV2MultipleAddDelete(t *testing.T) {
"eth0": {
NICType: cns.InfraNIC,
},
"eth0-1": {
"eth1": {
NICType: cns.NodeNetworkInterfaceAccelnetFrontendNIC,
},
}),
Expand Down Expand Up @@ -1655,7 +1655,7 @@ func TestPluginSwiftV2MultipleAddDelete(t *testing.T) {
if ep.NICType == cns.InfraNIC {
require.Equal(t, "test-con-"+tt.args.IfName, ep.EndpointID, "infra nic must use ifname for its endpoint id")
} else {
require.Regexp(t, `test-con-\d+$`, ep.EndpointID, "other nics must use an index for their endpoint ids")
require.Regexp(t, `\d+$`, ep.EndpointID, "other nics must use an index for their endpoint ids")
}
}

Expand Down

0 comments on commit ea43a60

Please sign in to comment.