Skip to content

Commit 44e5f37

Browse files
jaer-tsunJaeryn
andauthored
refactor: remove cniTypesCurr.Result dependency in InterfaceInfo (#2361)
* refactor: remove cniTypesCurr.Result dependency in InterfaceInfo * update baremetal * add interface in cni result conversion func * update gateway route --------- Co-authored-by: Jaeryn <[email protected]>
1 parent 9330999 commit 44e5f37

16 files changed

+314
-269
lines changed

cni/network/invoker.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55

66
"github.com/Azure/azure-container-networking/cni"
77
"github.com/Azure/azure-container-networking/cns"
8+
"github.com/Azure/azure-container-networking/network"
89
cniSkel "github.com/containernetworking/cni/pkg/skel"
9-
cniTypesCurr "github.com/containernetworking/cni/pkg/types/100"
1010
)
1111

1212
// IPAMInvoker is used by the azure-vnet CNI plugin to call different sources for IPAM.
@@ -28,17 +28,10 @@ type IPAMAddConfig struct {
2828

2929
type IPAMAddResult struct {
3030
// Splitting defaultInterfaceInfo from secondaryInterfacesInfo so we don't need to loop for default CNI result every time
31-
defaultInterfaceInfo InterfaceInfo
32-
secondaryInterfacesInfo []InterfaceInfo
31+
defaultInterfaceInfo network.InterfaceInfo
32+
secondaryInterfacesInfo []network.InterfaceInfo
3333
// ncResponse is used for Swift 1.0 multitenancy
3434
ncResponse *cns.GetNetworkContainerResponse
3535
hostSubnetPrefix net.IPNet
3636
ipv6Enabled bool
3737
}
38-
39-
type InterfaceInfo struct {
40-
ipResult *cniTypesCurr.Result
41-
nicType cns.NICType
42-
macAddress net.HardwareAddr
43-
skipDefaultRoutes bool
44-
}

cni/network/invoker_azure.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func (invoker *AzureIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, er
7575

7676
defer func() {
7777
if err != nil {
78-
if len(addResult.defaultInterfaceInfo.ipResult.IPs) > 0 {
79-
if er := invoker.Delete(&addResult.defaultInterfaceInfo.ipResult.IPs[0].Address, addConfig.nwCfg, nil, addConfig.options); er != nil {
78+
if len(addResult.defaultInterfaceInfo.IPConfigs) > 0 {
79+
if er := invoker.Delete(&addResult.defaultInterfaceInfo.IPConfigs[0].Address, addConfig.nwCfg, nil, addConfig.options); er != nil {
8080
err = invoker.plugin.Errorf("Failed to clean up IP's during Delete with error %v, after Add failed with error %w", er, err)
8181
}
8282
} else {
@@ -106,7 +106,17 @@ func (invoker *AzureIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, er
106106
}
107107
}
108108

109-
addResult.defaultInterfaceInfo = InterfaceInfo{ipResult: result, nicType: cns.InfraNIC}
109+
ipconfigs := make([]*network.IPConfig, len(result.IPs))
110+
for i, ipconfig := range result.IPs {
111+
ipconfigs[i] = &network.IPConfig{Address: ipconfig.Address, Gateway: ipconfig.Gateway}
112+
}
113+
114+
routes := make([]network.RouteInfo, len(result.Routes))
115+
for i, route := range result.Routes {
116+
routes[i] = network.RouteInfo{Dst: route.Dst, Gw: route.GW}
117+
}
118+
119+
addResult.defaultInterfaceInfo = network.InterfaceInfo{IPConfigs: ipconfigs, Routes: routes, DNS: network.DNSInfo{Suffix: result.DNS.Domain, Servers: result.DNS.Nameservers}, NICType: cns.InfraNIC}
110120

111121
return addResult, err
112122
}

cni/network/invoker_azure_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ func getSingleResult(ip string) []*cniTypesCurr.Result {
103103
}
104104

105105
// getResult will return a slice of IPConfigs
106-
func getResult(ips ...string) *cniTypesCurr.Result {
107-
res := &cniTypesCurr.Result{}
106+
func getResult(ips ...string) []*network.IPConfig {
107+
res := make([]*network.IPConfig, 0)
108108
for _, ip := range ips {
109-
res.IPs = append(res.IPs, &cniTypesCurr.IPConfig{Address: *getCIDRNotationForAddress(ip)})
109+
res = append(res, &network.IPConfig{Address: *getCIDRNotationForAddress(ip)})
110110
}
111111
return res
112112
}
@@ -142,7 +142,7 @@ func TestAzureIPAMInvoker_Add(t *testing.T) {
142142
name string
143143
fields fields
144144
args args
145-
want *cniTypesCurr.Result
145+
want []*network.IPConfig
146146
wantErr bool
147147
}{
148148
{
@@ -238,8 +238,8 @@ func TestAzureIPAMInvoker_Add(t *testing.T) {
238238
require.Nil(err)
239239
}
240240

241-
fmt.Printf("want:%+v\nrest:%+v\n", tt.want, ipamAddResult.defaultInterfaceInfo.ipResult)
242-
require.Exactly(tt.want, ipamAddResult.defaultInterfaceInfo.ipResult)
241+
fmt.Printf("want:%+v\nrest:%+v\n", tt.want, ipamAddResult.defaultInterfaceInfo.IPConfigs)
242+
require.Exactly(tt.want, ipamAddResult.defaultInterfaceInfo.IPConfigs)
243243
})
244244
}
245245
}
@@ -395,8 +395,7 @@ func TestRemoveIpamState_Add(t *testing.T) {
395395
name string
396396
fields fields
397397
args args
398-
want *cniTypesCurr.Result
399-
want1 *cniTypesCurr.Result
398+
want []*network.IPConfig
400399
wantErrMsg string
401400
wantErr bool
402401
}{

cni/network/invoker_cns.go

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
"github.com/Azure/azure-container-networking/network"
1616
"github.com/Azure/azure-container-networking/network/networkutils"
1717
cniSkel "github.com/containernetworking/cni/pkg/skel"
18-
cniTypes "github.com/containernetworking/cni/pkg/types"
19-
cniTypesCurr "github.com/containernetworking/cni/pkg/types/100"
2018
"github.com/pkg/errors"
2119
"go.uber.org/zap"
2220
"go.uber.org/zap/zapcore"
@@ -178,8 +176,8 @@ func (invoker *CNSIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, erro
178176
}
179177
default:
180178
// only count dualstack interface once
181-
if addResult.defaultInterfaceInfo.ipResult == nil {
182-
addResult.defaultInterfaceInfo.ipResult = &cniTypesCurr.Result{}
179+
if addResult.defaultInterfaceInfo.IPConfigs == nil {
180+
addResult.defaultInterfaceInfo.IPConfigs = make([]*network.IPConfig, 0)
183181
if !info.skipDefaultRoutes {
184182
numInterfacesWithDefaultRoutes++
185183
}
@@ -331,8 +329,8 @@ func (invoker *CNSIPAMInvoker) Delete(address *net.IPNet, nwCfg *cni.NetworkConf
331329
return nil
332330
}
333331

334-
func getRoutes(cnsRoutes []cns.Route, skipDefaultRoutes bool) ([]*cniTypes.Route, error) {
335-
routes := make([]*cniTypes.Route, 0)
332+
func getRoutes(cnsRoutes []cns.Route, skipDefaultRoutes bool) ([]network.RouteInfo, error) {
333+
routes := make([]network.RouteInfo, 0)
336334
for _, route := range cnsRoutes {
337335
_, dst, routeErr := net.ParseCIDR(route.IPAddress)
338336
if routeErr != nil {
@@ -345,9 +343,9 @@ func getRoutes(cnsRoutes []cns.Route, skipDefaultRoutes bool) ([]*cniTypes.Route
345343
}
346344

347345
routes = append(routes,
348-
&cniTypes.Route{
346+
network.RouteInfo{
349347
Dst: *dst,
350-
GW: gw,
348+
Gw: gw,
351349
})
352350
}
353351

@@ -386,15 +384,15 @@ func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, add
386384
}
387385

388386
if ip := net.ParseIP(info.podIPAddress); ip != nil {
389-
defaultInterfaceInfo := addResult.defaultInterfaceInfo.ipResult
387+
defaultInterfaceInfo := &addResult.defaultInterfaceInfo
390388
defaultRouteDstPrefix := network.Ipv4DefaultRouteDstPrefix
391389
if ip.To4() == nil {
392390
defaultRouteDstPrefix = network.Ipv6DefaultRouteDstPrefix
393391
addResult.ipv6Enabled = true
394392
}
395393

396-
defaultInterfaceInfo.IPs = append(defaultInterfaceInfo.IPs,
397-
&cniTypesCurr.IPConfig{
394+
defaultInterfaceInfo.IPConfigs = append(defaultInterfaceInfo.IPConfigs,
395+
&network.IPConfig{
398396
Address: net.IPNet{
399397
IP: ip,
400398
Mask: ncIPNet.Mask,
@@ -410,14 +408,13 @@ func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, add
410408
if len(routes) > 0 {
411409
defaultInterfaceInfo.Routes = append(defaultInterfaceInfo.Routes, routes...)
412410
} else { // add default routes if none are provided
413-
defaultInterfaceInfo.Routes = append(defaultInterfaceInfo.Routes, &cniTypes.Route{
411+
defaultInterfaceInfo.Routes = append(defaultInterfaceInfo.Routes, network.RouteInfo{
414412
Dst: defaultRouteDstPrefix,
415-
GW: ncgw,
413+
Gw: ncgw,
416414
})
417415
}
418416

419-
addResult.defaultInterfaceInfo.ipResult = defaultInterfaceInfo
420-
addResult.defaultInterfaceInfo.skipDefaultRoutes = info.skipDefaultRoutes
417+
addResult.defaultInterfaceInfo.SkipDefaultRoutes = info.skipDefaultRoutes
421418
}
422419

423420
// get the name of the primary IP address
@@ -427,7 +424,7 @@ func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, add
427424
}
428425

429426
addResult.hostSubnetPrefix = *hostIPNet
430-
addResult.defaultInterfaceInfo.nicType = cns.InfraNIC
427+
addResult.defaultInterfaceInfo.NICType = cns.InfraNIC
431428

432429
// set subnet prefix for host vm
433430
// setHostOptions will execute if IPAM mode is not v4 overlay and not dualStackOverlay mode
@@ -452,28 +449,26 @@ func configureSecondaryAddResult(info *IPResultInfo, addResult *IPAMAddResult, p
452449
return errors.Wrap(err, "Invalid mac address")
453450
}
454451

455-
result := InterfaceInfo{
456-
ipResult: &cniTypesCurr.Result{
457-
IPs: []*cniTypesCurr.IPConfig{
458-
{
459-
Address: net.IPNet{
460-
IP: ip,
461-
Mask: ipnet.Mask,
462-
},
463-
},
464-
},
465-
},
466-
nicType: info.nicType,
467-
macAddress: macAddress,
468-
skipDefaultRoutes: info.skipDefaultRoutes,
469-
}
470-
471452
routes, err := getRoutes(info.routes, info.skipDefaultRoutes)
472453
if err != nil {
473454
return err
474455
}
475456

476-
result.ipResult.Routes = append(result.ipResult.Routes, routes...)
457+
result := network.InterfaceInfo{
458+
IPConfigs: []*network.IPConfig{
459+
{
460+
Address: net.IPNet{
461+
IP: ip,
462+
Mask: ipnet.Mask,
463+
},
464+
},
465+
},
466+
Routes: routes,
467+
NICType: info.nicType,
468+
MacAddress: macAddress,
469+
SkipDefaultRoutes: info.skipDefaultRoutes,
470+
}
471+
477472
addResult.secondaryInterfacesInfo = append(addResult.secondaryInterfacesInfo, result)
478473

479474
return nil

0 commit comments

Comments
 (0)