Skip to content

Commit

Permalink
refactor: remove cniTypesCurr.Result dependency in InterfaceInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaeryn committed Nov 8, 2023
1 parent 6806d9a commit 0e2b811
Show file tree
Hide file tree
Showing 17 changed files with 423 additions and 376 deletions.
13 changes: 3 additions & 10 deletions cni/network/invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (

"github.com/Azure/azure-container-networking/cni"
"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/network"
cniSkel "github.com/containernetworking/cni/pkg/skel"
cniTypesCurr "github.com/containernetworking/cni/pkg/types/100"
)

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

type IPAMAddResult struct {
// Splitting defaultInterfaceInfo from secondaryInterfacesInfo so we don't need to loop for default CNI result every time
defaultInterfaceInfo InterfaceInfo
secondaryInterfacesInfo []InterfaceInfo
defaultInterfaceInfo network.InterfaceInfo
secondaryInterfacesInfo []network.InterfaceInfo
// ncResponse is used for Swift 1.0 multitenancy
ncResponse *cns.GetNetworkContainerResponse
hostSubnetPrefix net.IPNet
ipv6Enabled bool
}

type InterfaceInfo struct {
ipResult *cniTypesCurr.Result
nicType cns.NICType
macAddress net.HardwareAddr
skipDefaultRoutes bool
}
16 changes: 13 additions & 3 deletions cni/network/invoker_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (invoker *AzureIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, er

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

addResult.defaultInterfaceInfo = InterfaceInfo{ipResult: result, nicType: cns.InfraNIC}
ipconfigs := make([]*network.IPConfig, len(result.IPs))
for i, ipconfig := range result.IPs {
ipconfigs[i] = &network.IPConfig{Address: ipconfig.Address, Gateway: ipconfig.Gateway}
}

routes := make([]network.RouteInfo, len(result.Routes))
for i, route := range result.Routes {
routes[i] = network.RouteInfo{Dst: route.Dst, Gw: route.GW}
}

addResult.defaultInterfaceInfo = network.InterfaceInfo{IPConfigs: ipconfigs, Routes: routes, DNS: network.DNSInfo{Suffix: result.DNS.Domain, Servers: result.DNS.Nameservers}, NICType: cns.InfraNIC}

return addResult, err
}
Expand Down
15 changes: 7 additions & 8 deletions cni/network/invoker_azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ func getSingleResult(ip string) []*cniTypesCurr.Result {
}

// getResult will return a slice of IPConfigs
func getResult(ips ...string) *cniTypesCurr.Result {
res := &cniTypesCurr.Result{}
func getResult(ips ...string) []*network.IPConfig {
res := make([]*network.IPConfig, 0)
for _, ip := range ips {
res.IPs = append(res.IPs, &cniTypesCurr.IPConfig{Address: *getCIDRNotationForAddress(ip)})
res = append(res, &network.IPConfig{Address: *getCIDRNotationForAddress(ip)})
}
return res
}
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestAzureIPAMInvoker_Add(t *testing.T) {
name string
fields fields
args args
want *cniTypesCurr.Result
want []*network.IPConfig
wantErr bool
}{
{
Expand Down Expand Up @@ -238,8 +238,8 @@ func TestAzureIPAMInvoker_Add(t *testing.T) {
require.Nil(err)
}

fmt.Printf("want:%+v\nrest:%+v\n", tt.want, ipamAddResult.defaultInterfaceInfo.ipResult)
require.Exactly(tt.want, ipamAddResult.defaultInterfaceInfo.ipResult)
fmt.Printf("want:%+v\nrest:%+v\n", tt.want, ipamAddResult.defaultInterfaceInfo.IPConfigs)
require.Exactly(tt.want, ipamAddResult.defaultInterfaceInfo.IPConfigs)
})
}
}
Expand Down Expand Up @@ -395,8 +395,7 @@ func TestRemoveIpamState_Add(t *testing.T) {
name string
fields fields
args args
want *cniTypesCurr.Result
want1 *cniTypesCurr.Result
want []*network.IPConfig
wantErrMsg string
wantErr bool
}{
Expand Down
61 changes: 28 additions & 33 deletions cni/network/invoker_cns.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
"github.com/Azure/azure-container-networking/network"
"github.com/Azure/azure-container-networking/network/networkutils"
cniSkel "github.com/containernetworking/cni/pkg/skel"
cniTypes "github.com/containernetworking/cni/pkg/types"
cniTypesCurr "github.com/containernetworking/cni/pkg/types/100"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -178,8 +176,8 @@ func (invoker *CNSIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, erro
}
default:
// only count dualstack interface once
if addResult.defaultInterfaceInfo.ipResult == nil {
addResult.defaultInterfaceInfo.ipResult = &cniTypesCurr.Result{}
if addResult.defaultInterfaceInfo.IPConfigs == nil {
addResult.defaultInterfaceInfo.IPConfigs = make([]*network.IPConfig, 0)
if !info.skipDefaultRoutes {
numInterfacesWithDefaultRoutes++
}
Expand Down Expand Up @@ -331,8 +329,8 @@ func (invoker *CNSIPAMInvoker) Delete(address *net.IPNet, nwCfg *cni.NetworkConf
return nil
}

func getRoutes(cnsRoutes []cns.Route, skipDefaultRoutes bool) ([]*cniTypes.Route, error) {
routes := make([]*cniTypes.Route, 0)
func getRoutes(cnsRoutes []cns.Route, skipDefaultRoutes bool) ([]network.RouteInfo, error) {
routes := make([]network.RouteInfo, 0)
for _, route := range cnsRoutes {
_, dst, routeErr := net.ParseCIDR(route.IPAddress)
if routeErr != nil {
Expand All @@ -345,9 +343,9 @@ func getRoutes(cnsRoutes []cns.Route, skipDefaultRoutes bool) ([]*cniTypes.Route
}

routes = append(routes,
&cniTypes.Route{
network.RouteInfo{
Dst: *dst,
GW: gw,
Gw: gw,
})
}

Expand Down Expand Up @@ -386,15 +384,15 @@ func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, add
}

if ip := net.ParseIP(info.podIPAddress); ip != nil {
defaultInterfaceInfo := addResult.defaultInterfaceInfo.ipResult
defaultInterfaceInfo := &addResult.defaultInterfaceInfo
defaultRouteDstPrefix := network.Ipv4DefaultRouteDstPrefix
if ip.To4() == nil {
defaultRouteDstPrefix = network.Ipv6DefaultRouteDstPrefix
addResult.ipv6Enabled = true
}

defaultInterfaceInfo.IPs = append(defaultInterfaceInfo.IPs,
&cniTypesCurr.IPConfig{
defaultInterfaceInfo.IPConfigs = append(defaultInterfaceInfo.IPConfigs,
&network.IPConfig{
Address: net.IPNet{
IP: ip,
Mask: ncIPNet.Mask,
Expand All @@ -410,14 +408,13 @@ func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, add
if len(routes) > 0 {
defaultInterfaceInfo.Routes = append(defaultInterfaceInfo.Routes, routes...)
} else { // add default routes if none are provided
defaultInterfaceInfo.Routes = append(defaultInterfaceInfo.Routes, &cniTypes.Route{
defaultInterfaceInfo.Routes = append(defaultInterfaceInfo.Routes, network.RouteInfo{
Dst: defaultRouteDstPrefix,
GW: ncgw,
Gw: ncgw,
})
}

addResult.defaultInterfaceInfo.ipResult = defaultInterfaceInfo
addResult.defaultInterfaceInfo.skipDefaultRoutes = info.skipDefaultRoutes
addResult.defaultInterfaceInfo.SkipDefaultRoutes = info.skipDefaultRoutes
}

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

addResult.hostSubnetPrefix = *hostIPNet
addResult.defaultInterfaceInfo.nicType = cns.InfraNIC
addResult.defaultInterfaceInfo.NICType = cns.InfraNIC

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

result := InterfaceInfo{
ipResult: &cniTypesCurr.Result{
IPs: []*cniTypesCurr.IPConfig{
{
Address: net.IPNet{
IP: ip,
Mask: ipnet.Mask,
},
},
},
},
nicType: info.nicType,
macAddress: macAddress,
skipDefaultRoutes: info.skipDefaultRoutes,
}

routes, err := getRoutes(info.routes, info.skipDefaultRoutes)
if err != nil {
return err
}

result.ipResult.Routes = append(result.ipResult.Routes, routes...)
result := network.InterfaceInfo{
IPConfigs: []*network.IPConfig{
{
Address: net.IPNet{
IP: ip,
Mask: ipnet.Mask,
},
},
},
Routes: routes,
NICType: info.nicType,
MacAddress: macAddress,
SkipDefaultRoutes: info.skipDefaultRoutes,
}

addResult.secondaryInterfacesInfo = append(addResult.secondaryInterfacesInfo, result)

return nil
Expand Down
Loading

0 comments on commit 0e2b811

Please sign in to comment.