Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: code changes for stateless cni and swift v2 #2688

Merged
merged 110 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
3b32b9c
ci: changes up to endpointInternal
jpayne3506 Mar 18, 2024
a7647d7
ci: remove defaultInterface from invoker
jpayne3506 Mar 18, 2024
ee9300b
ci: change up to CreateEndpoint
jpayne3506 Mar 18, 2024
c3ba7cf
ci: changes up to CreateEndpoint()
jpayne3506 Mar 19, 2024
a97d798
ci: invoker cns and UT fixes
jpayne3506 Mar 19, 2024
30f646b
ci: add fixes to UT(s), capture non populated defaultInterface failures
jpayne3506 Mar 19, 2024
2048eb7
ci: multitenancy changes
jpayne3506 Mar 19, 2024
ed5d5f9
ci: invoker azure changes & remove all defaultInterfaceInfo possible
jpayne3506 Mar 19, 2024
e676314
ci add NICType to baremetal flow
jpayne3506 Mar 19, 2024
ef86f03
chore: address comments
jpayne3506 Mar 19, 2024
59fc47e
merge nw info fields to ep info and draft new createEndpoint function
QxBytes Mar 27, 2024
393b481
restruct ipamAddResult struct
paulyufan2 Mar 27, 2024
68d87bc
reorder code to create epinfo first, and then create network and ep b…
QxBytes Mar 28, 2024
157ee72
add getNwInfo and generate ipamAddResult
paulyufan2 Mar 28, 2024
6a5452a
fix network windows.go
paulyufan2 Mar 28, 2024
bd1d254
create nw info first and create nw and ep dns info
QxBytes Mar 28, 2024
f6debfa
Merge branch 'alew/merge-structs' of github.com:Azure/azure-container…
QxBytes Mar 28, 2024
7e92823
fix testIpamAddFail ut referencing wrong redeclared err variable, fix…
QxBytes Mar 29, 2024
8abd904
UT fix part one
paulyufan2 Mar 29, 2024
ddffc9d
fix the getNetworkID and getNetworkInfo
paulyufan2 Apr 1, 2024
e5ed570
move create endpoint to network package, remove ifIndex as needed
QxBytes Apr 1, 2024
bb2e979
use function to get network id
QxBytes Apr 1, 2024
75c194d
unify creation of nw and endpoint info by removing switch
QxBytes Apr 2, 2024
69f78f7
change functions to consume ep info instead of nw info
QxBytes Apr 2, 2024
c790e8b
remove unused variable accidentally added earlier
QxBytes Apr 2, 2024
d137832
update old azure ipam invoker to use ep info and change ids to networ…
QxBytes Apr 2, 2024
9e0f029
rename endpoint info id field to EndpointID to remove ambiguity
QxBytes Apr 2, 2024
ed31532
change nw info to ep info in windows
QxBytes Apr 2, 2024
c4f2cbd
adjust comments
QxBytes Apr 2, 2024
811610a
move all methods in create ep info dependent on nw info to use ep inf…
QxBytes Apr 2, 2024
50db70f
make cnm compile (not necessarily correct)
QxBytes Apr 2, 2024
9383597
make all tests compile except endpoint test secondary client (windows…
QxBytes Apr 2, 2024
b5b0a8f
comment out endpoint test secondary endpoint client case to make test…
QxBytes Apr 2, 2024
59d9109
address todos and comments from meeting
QxBytes Apr 3, 2024
655ccee
remove duplicated code for populating address in ep info generation
QxBytes Apr 3, 2024
fe071bd
update EndpointCreate to support multiple infra nic
QxBytes Apr 3, 2024
1f67a11
save all endpoints to state, regardless of type, use either stateless…
QxBytes Apr 4, 2024
9945d1e
fix dual nic support conditional and finding master interface ip
QxBytes Apr 5, 2024
608b372
fix empty network name
QxBytes Apr 5, 2024
4686fe4
make network_test.go compile (linux and windows compile)
QxBytes Apr 5, 2024
daae8c0
add NICType to endpoint struct and populate it
QxBytes Apr 5, 2024
ded5be5
ci: InterfaceInfo Map
jpayne3506 Mar 29, 2024
f5de076
fix multitenancy_test ut by changing key
QxBytes Apr 8, 2024
60888e1
add endpoint id to secondary ep info test since we populate the id in…
QxBytes Apr 8, 2024
2a01025
fix cni network_test linux and ensure secondary create ep info does n…
QxBytes Apr 8, 2024
ea5b806
make invoker_cns_test linux pass
QxBytes Apr 8, 2024
b2f2954
modify delete flow to handle multiple epinfos to delete
QxBytes Apr 12, 2024
bde67f1
address feedback
QxBytes Apr 12, 2024
7fc4b64
Make change to UpdateEndpointState API to support SwiftV2 for Statele…
behzad-mir Apr 12, 2024
0a56c89
change save state to only call update endpoint state once with a slic…
QxBytes Apr 12, 2024
0395e00
fix using nonexistent key by passing in current interface info directly
QxBytes Apr 15, 2024
179b92b
fix azure ipam invoker not getting a populated network info for legac…
QxBytes Apr 16, 2024
c8a1156
Merge remote-tracking branch 'upstream/master' into alew/merge-structs
QxBytes Apr 16, 2024
2af1543
add L1VH windows support
paulyufan2 Apr 16, 2024
7b380eb
add nic type to windows endpoints
QxBytes Apr 17, 2024
44a56c1
move adding an external interface code to run only when creating a ne…
QxBytes Apr 18, 2024
4222b83
update comments, first todo check pass
QxBytes Apr 20, 2024
44a4731
address some linter issues
QxBytes Apr 20, 2024
b2c1ffe
rename networkId to networkID in endpoint info
QxBytes Apr 20, 2024
fc16324
address linter issues
QxBytes Apr 20, 2024
0f00492
preserve more logs and reduce timeout for restart for debugging
QxBytes Apr 22, 2024
2f69c3f
Merge remote-tracking branch 'upstream/master' into alew/merge-structs
QxBytes Apr 22, 2024
2e35953
clean comments and rename for clarity
QxBytes Apr 23, 2024
9166293
address more linter issues
QxBytes Apr 24, 2024
fec5605
Revert "preserve more logs and reduce timeout for restart for debugging"
QxBytes Apr 26, 2024
14afa4c
Merge remote-tracking branch 'upstream/master' into alew/merge-structs
QxBytes May 3, 2024
7fe4df2
ignore error on delete flow network query
QxBytes May 8, 2024
41a9e4f
Merge remote-tracking branch 'upstream/master' into alew/merge-structs
QxBytes May 8, 2024
03b2042
delete network on endpoint delete if stateless and delegated vmnic (w…
QxBytes May 10, 2024
1c80ac3
add nic name, set nicname in linux to master interface name
QxBytes May 11, 2024
eea0332
return secondary interface as cni result if no infra nic found, inclu…
QxBytes May 14, 2024
ccf3d8e
address linter issue
QxBytes May 15, 2024
8a2ae3f
fix critical error where failing to add in windows stateless would le…
QxBytes May 17, 2024
b6eb342
set nicname used in stateless cni according to feedback
QxBytes May 17, 2024
3d23c6d
add dummy guid to stateless delete since we assume stateless is alway…
QxBytes May 17, 2024
5356b4d
clean up createEpInfo, declare endpoint info once
QxBytes May 18, 2024
6df1b9f
address feedback from vipul
QxBytes May 20, 2024
182a3df
change comments only
QxBytes May 21, 2024
317d9a6
Merge remote-tracking branch 'upstream/master' into alew/merge-structs
QxBytes May 21, 2024
b9ea97d
revert change to cns package
QxBytes May 21, 2024
270c113
fix stateless cni migration flow not having nictype on migrate
QxBytes May 21, 2024
f5e939b
keep nwInfo variables named the same as before pr (noop)
QxBytes May 21, 2024
0badbf2
separate endpoint and network policies in endpoint info
QxBytes May 21, 2024
87f1bfe
address feedback from reviewers
QxBytes May 22, 2024
139a3a1
address feedback and account for case where cns provides info without…
QxBytes May 22, 2024
b13e84f
address feedback to declare endpoint info once and populate all field…
QxBytes May 23, 2024
a1af26a
use ifname instead of nicname field in endpoint struct as key in stat…
QxBytes May 23, 2024
0a9c272
convert macaddress only nictype is delegatedvmnic
paulyufan2 May 23, 2024
2c177f9
address feedback by removing network dns settings
QxBytes May 23, 2024
b1242bd
address linter issues (noop)
QxBytes May 24, 2024
9ee7b91
address feedback and linter (noop)
QxBytes May 24, 2024
8fed0ec
remove unused consecutive add funcs (noop)
QxBytes May 24, 2024
17c3fa0
fix release ips when create a container without nictype using older c…
QxBytes May 25, 2024
24a8396
prevent eps with delegated nic type present on ep from also calling t…
QxBytes May 28, 2024
8ed93a5
mock get interface method for ut
QxBytes May 29, 2024
b2bdd8c
address feedback (noop)
QxBytes May 29, 2024
07ce9d7
add ut for handling empty nictype on cns add (noop)
QxBytes May 29, 2024
209650c
add multitenancy delete net not found ut (noop)
QxBytes May 29, 2024
2f15ae4
add uts for multi interface infos single add call, verify endpoint id…
QxBytes May 30, 2024
fd45d0c
add ut for all pods associated with container id delete in one del ca…
QxBytes May 30, 2024
bde0294
add two UTs
paulyufan2 May 30, 2024
c877fd4
fix a linter issue
paulyufan2 May 30, 2024
9bd8745
add ut to check endpoint ifname on new endpoint creation based on nic…
QxBytes May 30, 2024
1c45f4d
Merge remote-tracking branch 'upstream/master' into alew/merge-structs
QxBytes May 30, 2024
8f9f9cb
add ut for fail to find interface by subnet (noop)
QxBytes May 30, 2024
cafa8d2
Adding support for Stateless CNI Delete Edge case when there in no HN…
behzad-mir May 30, 2024
78162be
fix uts
paulyufan2 May 31, 2024
ff234e9
fix linter issues
paulyufan2 May 31, 2024
87aefd0
fix ut
QxBytes May 31, 2024
b8af665
Merge remote-tracking branch 'upstream/master' into alew/merge-structs
QxBytes May 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions cni/network/invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net"

"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"
)
Expand All @@ -27,11 +26,16 @@ type IPAMAddConfig struct {
}

type IPAMAddResult struct {
// Splitting defaultInterfaceInfo from secondaryInterfacesInfo so we don't need to loop for default CNI result every time
defaultInterfaceInfo network.InterfaceInfo
secondaryInterfacesInfo []network.InterfaceInfo
// ncResponse is used for Swift 1.0 multitenancy
ncResponse *cns.GetNetworkContainerResponse
hostSubnetPrefix net.IPNet
ipv6Enabled bool
interfaceInfo map[string]network.InterfaceInfo
// ncResponse and host subnet prefix were moved into interface info
ipv6Enabled bool
}

func (ipamAddResult IPAMAddResult) PrettyString() string {
pStr := "InterfaceInfo: "
for key := range ipamAddResult.interfaceInfo {
val := ipamAddResult.interfaceInfo[key]
pStr += val.PrettyString()
}
return pStr
}
33 changes: 24 additions & 9 deletions cni/network/invoker_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (

type AzureIPAMInvoker struct {
plugin delegatePlugin
nwInfo *network.NetworkInfo
nwInfo *network.EndpointInfo
QxBytes marked this conversation as resolved.
Show resolved Hide resolved
}

type delegatePlugin interface {
Expand All @@ -39,15 +39,15 @@ type delegatePlugin interface {
}

// Create an IPAM instance every time a CNI action is called.
func NewAzureIpamInvoker(plugin *NetPlugin, nwInfo *network.NetworkInfo) *AzureIPAMInvoker {
func NewAzureIpamInvoker(plugin *NetPlugin, nwInfo *network.EndpointInfo) *AzureIPAMInvoker {
vipul-21 marked this conversation as resolved.
Show resolved Hide resolved
return &AzureIPAMInvoker{
plugin: plugin,
nwInfo: nwInfo,
}
}

func (invoker *AzureIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, error) {
addResult := IPAMAddResult{}
addResult := IPAMAddResult{interfaceInfo: make(map[string]network.InterfaceInfo)}

if addConfig.nwCfg == nil {
return addResult, invoker.plugin.Errorf("nil nwCfg passed to CNI ADD, stack: %+v", string(debug.Stack()))
Expand All @@ -69,14 +69,11 @@ func (invoker *AzureIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, er
err = invoker.plugin.Errorf("Failed to allocate pool: %v", err)
return addResult, err
}
if len(result.IPs) > 0 {
addResult.hostSubnetPrefix = result.IPs[0].Address
}

defer func() {
if err != nil {
if len(addResult.defaultInterfaceInfo.IPConfigs) > 0 {
if er := invoker.Delete(&addResult.defaultInterfaceInfo.IPConfigs[0].Address, addConfig.nwCfg, nil, addConfig.options); er != nil {
if len(addResult.interfaceInfo) > 0 && len(addResult.interfaceInfo[invoker.getInterfaceInfoKey(cns.InfraNIC)].IPConfigs) > 0 {
if er := invoker.Delete(&addResult.interfaceInfo[invoker.getInterfaceInfoKey(cns.InfraNIC)].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 @@ -116,7 +113,21 @@ func (invoker *AzureIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, er
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}
// TODO: changed how host subnet prefix populated (check)
hostSubnetPrefix := net.IPNet{}
if len(result.IPs) > 0 {
hostSubnetPrefix = result.IPs[0].Address
}
addResult.interfaceInfo[invoker.getInterfaceInfoKey(cns.InfraNIC)] = network.InterfaceInfo{
IPConfigs: ipconfigs,
Routes: routes,
DNS: network.DNSInfo{
Suffix: result.DNS.Domain,
Servers: result.DNS.Nameservers,
},
NICType: cns.InfraNIC,
HostSubnetPrefix: hostSubnetPrefix,
}

return addResult, err
}
Expand Down Expand Up @@ -197,3 +208,7 @@ func (invoker *AzureIPAMInvoker) Delete(address *net.IPNet, nwCfg *cni.NetworkCo

return nil
}

func (invoker *AzureIPAMInvoker) getInterfaceInfoKey(nicType cns.NICType) string {
return string(nicType)
}
40 changes: 30 additions & 10 deletions cni/network/invoker_azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/Azure/azure-container-networking/cni"
"github.com/Azure/azure-container-networking/cni/log"
"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/ipam"
"github.com/Azure/azure-container-networking/network"
cniSkel "github.com/containernetworking/cni/pkg/skel"
Expand Down Expand Up @@ -81,6 +82,9 @@ func (m *mockDelegatePlugin) Errorf(format string, args ...interface{}) *cniType
}
}

// net.ParseCIDR will first get the ip, which contains byte data for the ip and mask,
// and the ipnet, which has a field for the *masked* ip and a field for the mask
// this function then replaces the masked ip with the "ip" field retrieved earlier and returns the ipnet
func getCIDRNotationForAddress(ipaddresswithcidr string) *net.IPNet {
ip, ipnet, err := net.ParseCIDR(ipaddresswithcidr)
if err != nil {
Expand All @@ -90,6 +94,15 @@ func getCIDRNotationForAddress(ipaddresswithcidr string) *net.IPNet {
return ipnet
}

// returns an ipnet, which contains the *masked* ip (zeroed out based on CIDR) and the mask itself
func parseCIDR(ipaddresswithcidr string) *net.IPNet {
_, ipnet, err := net.ParseCIDR(ipaddresswithcidr)
if err != nil {
panic(fmt.Sprintf("failed to parse cidr with err: %v", err))
}
return ipnet
}
QxBytes marked this conversation as resolved.
Show resolved Hide resolved

func getSingleResult(ip string) []*cniTypesCurr.Result {
return []*cniTypesCurr.Result{
{
Expand All @@ -111,26 +124,26 @@ func getResult(ips ...string) []*network.IPConfig {
return res
}

func getNwInfo(subnetv4, subnetv6 string) *network.NetworkInfo {
nwinfo := &network.NetworkInfo{}
func getNwInfo(subnetv4, subnetv6 string) *network.EndpointInfo {
nwInfo := &network.EndpointInfo{}
if subnetv4 != "" {
nwinfo.Subnets = append(nwinfo.Subnets, network.SubnetInfo{
nwInfo.Subnets = append(nwInfo.Subnets, network.SubnetInfo{
Prefix: *getCIDRNotationForAddress(subnetv4),
})
}
if subnetv6 != "" {
nwinfo.Subnets = append(nwinfo.Subnets, network.SubnetInfo{
nwInfo.Subnets = append(nwInfo.Subnets, network.SubnetInfo{
Prefix: *getCIDRNotationForAddress(subnetv6),
})
}
return nwinfo
return nwInfo
}

func TestAzureIPAMInvoker_Add(t *testing.T) {
require := require.New(t)
type fields struct {
plugin delegatePlugin
nwInfo *network.NetworkInfo
nwInfo *network.EndpointInfo
}
type args struct {
nwCfg *cni.NetworkConfig
Expand Down Expand Up @@ -238,8 +251,15 @@ func TestAzureIPAMInvoker_Add(t *testing.T) {
require.Nil(err)
}

fmt.Printf("want:%+v\nrest:%+v\n", tt.want, ipamAddResult.defaultInterfaceInfo.IPConfigs)
require.Exactly(tt.want, ipamAddResult.defaultInterfaceInfo.IPConfigs)
for key, ifInfo := range ipamAddResult.interfaceInfo {
if ifInfo.NICType == cns.InfraNIC {
fmt.Printf("want:%+v\nrest:%+v\n", tt.want, ifInfo.IPConfigs)
require.Exactly(tt.want, ifInfo.IPConfigs)
}
// azure ipam invoker always sets key as infra nic
require.Equal(string(cns.InfraNIC), key)
require.Equal(cns.InfraNIC, ifInfo.NICType)
}
})
}
}
Expand All @@ -248,7 +268,7 @@ func TestAzureIPAMInvoker_Delete(t *testing.T) {
require := require.New(t)
type fields struct {
plugin delegatePlugin
nwInfo *network.NetworkInfo
nwInfo *network.EndpointInfo
}
type args struct {
address *net.IPNet
Expand Down Expand Up @@ -383,7 +403,7 @@ func TestRemoveIpamState_Add(t *testing.T) {
requires := require.New(t)
type fields struct {
plugin delegatePlugin
nwInfo *network.NetworkInfo
nwInfo *network.EndpointInfo
}
type args struct {
nwCfg *cni.NetworkConfig
Expand Down
79 changes: 51 additions & 28 deletions cni/network/invoker_cns.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ func (invoker *CNSIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, erro
}
}

addResult := IPAMAddResult{}
addResult := IPAMAddResult{interfaceInfo: make(map[string]network.InterfaceInfo)}
numInterfacesWithDefaultRoutes := 0

for i := 0; i < len(response.PodIPInfo); i++ {
info := IPResultInfo{
podIPAddress: response.PodIPInfo[i].PodIPConfig.IPAddress,
Expand All @@ -164,29 +163,42 @@ func (invoker *CNSIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, erro
zap.Any("podInfo", podInfo))

//nolint:exhaustive // ignore exhaustive types check
// Do we want to leverage this lint skip in other places of our code?
key := invoker.getInterfaceInfoKey(info.nicType, info.macAddress)
switch info.nicType {
case cns.DelegatedVMNIC:
// only handling single v4 PodIPInfo for DelegatedVMNICs at the moment, will have to update once v6 gets added
if !info.skipDefaultRoutes {
numInterfacesWithDefaultRoutes++
}

if err := configureSecondaryAddResult(&info, &addResult, &response.PodIPInfo[i].PodIPConfig); err != nil {
// Add secondary interface info from podIPInfo to ipamAddResult
info.hostSubnet = response.PodIPInfo[i].HostPrimaryIPInfo.Subnet
info.hostPrimaryIP = response.PodIPInfo[i].HostPrimaryIPInfo.PrimaryIP
info.hostGateway = response.PodIPInfo[i].HostPrimaryIPInfo.Gateway

if err := configureSecondaryAddResult(&info, &addResult, &response.PodIPInfo[i].PodIPConfig, key); err != nil {
return IPAMAddResult{}, err
}
default:
case cns.InfraNIC, "":
// if we change from legacy cns, the nicType will be empty, so we assume it is infra nic
info.nicType = cns.InfraNIC

// only count dualstack interface once
if addResult.defaultInterfaceInfo.IPConfigs == nil {
addResult.defaultInterfaceInfo.IPConfigs = make([]*network.IPConfig, 0)
_, exist := addResult.interfaceInfo[key]
if !exist {
addResult.interfaceInfo[key] = network.InterfaceInfo{}
if !info.skipDefaultRoutes {
numInterfacesWithDefaultRoutes++
}
}

overlayMode := (invoker.ipamMode == util.V4Overlay) || (invoker.ipamMode == util.DualStackOverlay) || (invoker.ipamMode == util.Overlay)
if err := configureDefaultAddResult(&info, &addConfig, &addResult, overlayMode); err != nil {
if err := configureDefaultAddResult(&info, &addConfig, &addResult, overlayMode, key); err != nil {
return IPAMAddResult{}, err
}
default:
logger.Warn("Unknown NIC type received from cns pod ip info", zap.String("nicType", string(info.nicType)))
}
}

Expand Down Expand Up @@ -353,15 +365,15 @@ func getRoutes(cnsRoutes []cns.Route, skipDefaultRoutes bool) ([]network.RouteIn
return routes, nil
}

func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, addResult *IPAMAddResult, overlayMode bool) error {
func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, addResult *IPAMAddResult, overlayMode bool, key string) error {
// set the NC Primary IP in options
// SNATIPKey is not set for ipv6
if net.ParseIP(info.ncPrimaryIP).To4() != nil {
addConfig.options[network.SNATIPKey] = info.ncPrimaryIP
}

ip, ncIPNet, err := net.ParseCIDR(info.podIPAddress + "/" + fmt.Sprint(info.ncSubnetPrefix))
if ip == nil {
if ip == nil || err != nil {
return errors.Wrap(err, "Unable to parse IP from response: "+info.podIPAddress+" with err %w")
}

Expand All @@ -384,15 +396,21 @@ func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, add
}
}

// get the name of the primary IP address
_, hostIPNet, err := net.ParseCIDR(info.hostSubnet)
if err != nil {
return errors.Wrap(err, "unable to parse hostSubnet")
}

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

defaultInterfaceInfo.IPConfigs = append(defaultInterfaceInfo.IPConfigs,
ipConfigs := addResult.interfaceInfo[key].IPConfigs
ipConfigs = append(ipConfigs,
&network.IPConfig{
Address: net.IPNet{
IP: ip,
Expand All @@ -406,27 +424,26 @@ func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, add
return getRoutesErr
}

resRoute := addResult.interfaceInfo[key].Routes
if len(routes) > 0 {
defaultInterfaceInfo.Routes = append(defaultInterfaceInfo.Routes, routes...)
resRoute = append(resRoute, routes...)
} else { // add default routes if none are provided
defaultInterfaceInfo.Routes = append(defaultInterfaceInfo.Routes, network.RouteInfo{
resRoute = append(resRoute, network.RouteInfo{
Dst: defaultRouteDstPrefix,
Gw: ncgw,
})
}

addResult.defaultInterfaceInfo.SkipDefaultRoutes = info.skipDefaultRoutes
}

// get the name of the primary IP address
_, hostIPNet, err := net.ParseCIDR(info.hostSubnet)
if err != nil {
return fmt.Errorf("unable to parse hostSubnet: %w", err)
// if we have multiple infra ip result infos, we effectively append routes and ip configs to that same interface info each time
// the host subnet prefix (in ipv4 or ipv6) will always refer to the same interface regardless of which ip result info we look at
addResult.interfaceInfo[key] = network.InterfaceInfo{
NICType: cns.InfraNIC,
SkipDefaultRoutes: info.skipDefaultRoutes,
IPConfigs: ipConfigs,
Routes: resRoute,
HostSubnetPrefix: *hostIPNet,
}
}

addResult.hostSubnetPrefix = *hostIPNet
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
// TODO: Remove v4overlay and dualstackoverlay options, after 'overlay' rolls out in AKS-RP
Expand All @@ -439,7 +456,7 @@ func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, add
return nil
}

func configureSecondaryAddResult(info *IPResultInfo, addResult *IPAMAddResult, podIPConfig *cns.IPSubnet) error {
func configureSecondaryAddResult(info *IPResultInfo, addResult *IPAMAddResult, podIPConfig *cns.IPSubnet, key string) error {
ip, ipnet, err := podIPConfig.GetIPNet()
if ip == nil {
return errors.Wrap(err, "Unable to parse IP from response: "+info.podIPAddress+" with err %w")
Expand All @@ -455,13 +472,14 @@ func configureSecondaryAddResult(info *IPResultInfo, addResult *IPAMAddResult, p
return err
}

result := network.InterfaceInfo{
addResult.interfaceInfo[key] = network.InterfaceInfo{
IPConfigs: []*network.IPConfig{
{
Address: net.IPNet{
IP: ip,
Mask: ipnet.Mask,
},
Gateway: net.ParseIP(info.ncGatewayIPAddress),
},
},
Routes: routes,
Expand All @@ -470,7 +488,12 @@ func configureSecondaryAddResult(info *IPResultInfo, addResult *IPAMAddResult, p
SkipDefaultRoutes: info.skipDefaultRoutes,
}

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

return nil
}

func (invoker *CNSIPAMInvoker) getInterfaceInfoKey(nicType cns.NICType, macAddress string) string {
if nicType == cns.DelegatedVMNIC {
return macAddress
}
return string(nicType)
}
Loading
Loading