Skip to content

Commit 5ab954c

Browse files
authored
fix: Revert "refactor: code changes for stateless cni and swift v2 (#2688)" on release/v1.5 (#2847)
Revert "refactor: code changes for stateless cni and swift v2 (#2688)" This reverts commit 2ab9cfe.
1 parent 0ce33eb commit 5ab954c

39 files changed

+1059
-2210
lines changed

cni/network/invoker.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net"
55

66
"github.com/Azure/azure-container-networking/cni"
7+
"github.com/Azure/azure-container-networking/cns"
78
"github.com/Azure/azure-container-networking/network"
89
cniSkel "github.com/containernetworking/cni/pkg/skel"
910
)
@@ -26,16 +27,11 @@ type IPAMAddConfig struct {
2627
}
2728

2829
type IPAMAddResult struct {
29-
interfaceInfo map[string]network.InterfaceInfo
30-
// ncResponse and host subnet prefix were moved into interface info
31-
ipv6Enabled bool
32-
}
33-
34-
func (ipamAddResult IPAMAddResult) PrettyString() string {
35-
pStr := "InterfaceInfo: "
36-
for key := range ipamAddResult.interfaceInfo {
37-
val := ipamAddResult.interfaceInfo[key]
38-
pStr += val.PrettyString()
39-
}
40-
return pStr
30+
// Splitting defaultInterfaceInfo from secondaryInterfacesInfo so we don't need to loop for default CNI result every time
31+
defaultInterfaceInfo network.InterfaceInfo
32+
secondaryInterfacesInfo []network.InterfaceInfo
33+
// ncResponse is used for Swift 1.0 multitenancy
34+
ncResponse *cns.GetNetworkContainerResponse
35+
hostSubnetPrefix net.IPNet
36+
ipv6Enabled bool
4137
}

cni/network/invoker_azure.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const (
2929

3030
type AzureIPAMInvoker struct {
3131
plugin delegatePlugin
32-
nwInfo *network.EndpointInfo
32+
nwInfo *network.NetworkInfo
3333
}
3434

3535
type delegatePlugin interface {
@@ -39,15 +39,15 @@ type delegatePlugin interface {
3939
}
4040

4141
// Create an IPAM instance every time a CNI action is called.
42-
func NewAzureIpamInvoker(plugin *NetPlugin, nwInfo *network.EndpointInfo) *AzureIPAMInvoker {
42+
func NewAzureIpamInvoker(plugin *NetPlugin, nwInfo *network.NetworkInfo) *AzureIPAMInvoker {
4343
return &AzureIPAMInvoker{
4444
plugin: plugin,
4545
nwInfo: nwInfo,
4646
}
4747
}
4848

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

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

7376
defer func() {
7477
if err != nil {
75-
if len(addResult.interfaceInfo) > 0 && len(addResult.interfaceInfo[invoker.getInterfaceInfoKey(cns.InfraNIC)].IPConfigs) > 0 {
76-
if er := invoker.Delete(&addResult.interfaceInfo[invoker.getInterfaceInfoKey(cns.InfraNIC)].IPConfigs[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 {
7780
err = invoker.plugin.Errorf("Failed to clean up IP's during Delete with error %v, after Add failed with error %w", er, err)
7881
}
7982
} else {
@@ -113,21 +116,7 @@ func (invoker *AzureIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, er
113116
routes[i] = network.RouteInfo{Dst: route.Dst, Gw: route.GW}
114117
}
115118

116-
// TODO: changed how host subnet prefix populated (check)
117-
hostSubnetPrefix := net.IPNet{}
118-
if len(result.IPs) > 0 {
119-
hostSubnetPrefix = result.IPs[0].Address
120-
}
121-
addResult.interfaceInfo[invoker.getInterfaceInfoKey(cns.InfraNIC)] = network.InterfaceInfo{
122-
IPConfigs: ipconfigs,
123-
Routes: routes,
124-
DNS: network.DNSInfo{
125-
Suffix: result.DNS.Domain,
126-
Servers: result.DNS.Nameservers,
127-
},
128-
NICType: cns.InfraNIC,
129-
HostSubnetPrefix: hostSubnetPrefix,
130-
}
119+
addResult.defaultInterfaceInfo = network.InterfaceInfo{IPConfigs: ipconfigs, Routes: routes, DNS: network.DNSInfo{Suffix: result.DNS.Domain, Servers: result.DNS.Nameservers}, NICType: cns.InfraNIC}
131120

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

209198
return nil
210199
}
211-
212-
func (invoker *AzureIPAMInvoker) getInterfaceInfoKey(nicType cns.NICType) string {
213-
return string(nicType)
214-
}

cni/network/invoker_azure_test.go

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/Azure/azure-container-networking/cni"
1010
"github.com/Azure/azure-container-networking/cni/log"
11-
"github.com/Azure/azure-container-networking/cns"
1211
"github.com/Azure/azure-container-networking/ipam"
1312
"github.com/Azure/azure-container-networking/network"
1413
cniSkel "github.com/containernetworking/cni/pkg/skel"
@@ -82,9 +81,6 @@ func (m *mockDelegatePlugin) Errorf(format string, args ...interface{}) *cniType
8281
}
8382
}
8483

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

97-
// returns an ipnet, which contains the *masked* ip (zeroed out based on CIDR) and the mask itself
98-
func parseCIDR(ipaddresswithcidr string) *net.IPNet {
99-
_, ipnet, err := net.ParseCIDR(ipaddresswithcidr)
100-
if err != nil {
101-
panic(fmt.Sprintf("failed to parse cidr with err: %v", err))
102-
}
103-
return ipnet
104-
}
105-
10693
func getSingleResult(ip string) []*cniTypesCurr.Result {
10794
return []*cniTypesCurr.Result{
10895
{
@@ -124,26 +111,26 @@ func getResult(ips ...string) []*network.IPConfig {
124111
return res
125112
}
126113

127-
func getNwInfo(subnetv4, subnetv6 string) *network.EndpointInfo {
128-
nwInfo := &network.EndpointInfo{}
114+
func getNwInfo(subnetv4, subnetv6 string) *network.NetworkInfo {
115+
nwinfo := &network.NetworkInfo{}
129116
if subnetv4 != "" {
130-
nwInfo.Subnets = append(nwInfo.Subnets, network.SubnetInfo{
117+
nwinfo.Subnets = append(nwinfo.Subnets, network.SubnetInfo{
131118
Prefix: *getCIDRNotationForAddress(subnetv4),
132119
})
133120
}
134121
if subnetv6 != "" {
135-
nwInfo.Subnets = append(nwInfo.Subnets, network.SubnetInfo{
122+
nwinfo.Subnets = append(nwinfo.Subnets, network.SubnetInfo{
136123
Prefix: *getCIDRNotationForAddress(subnetv6),
137124
})
138125
}
139-
return nwInfo
126+
return nwinfo
140127
}
141128

142129
func TestAzureIPAMInvoker_Add(t *testing.T) {
143130
require := require.New(t)
144131
type fields struct {
145132
plugin delegatePlugin
146-
nwInfo *network.EndpointInfo
133+
nwInfo *network.NetworkInfo
147134
}
148135
type args struct {
149136
nwCfg *cni.NetworkConfig
@@ -251,15 +238,8 @@ func TestAzureIPAMInvoker_Add(t *testing.T) {
251238
require.Nil(err)
252239
}
253240

254-
for key, ifInfo := range ipamAddResult.interfaceInfo {
255-
if ifInfo.NICType == cns.InfraNIC {
256-
fmt.Printf("want:%+v\nrest:%+v\n", tt.want, ifInfo.IPConfigs)
257-
require.Exactly(tt.want, ifInfo.IPConfigs)
258-
}
259-
// azure ipam invoker always sets key as infra nic
260-
require.Equal(string(cns.InfraNIC), key)
261-
require.Equal(cns.InfraNIC, ifInfo.NICType)
262-
}
241+
fmt.Printf("want:%+v\nrest:%+v\n", tt.want, ipamAddResult.defaultInterfaceInfo.IPConfigs)
242+
require.Exactly(tt.want, ipamAddResult.defaultInterfaceInfo.IPConfigs)
263243
})
264244
}
265245
}
@@ -268,7 +248,7 @@ func TestAzureIPAMInvoker_Delete(t *testing.T) {
268248
require := require.New(t)
269249
type fields struct {
270250
plugin delegatePlugin
271-
nwInfo *network.EndpointInfo
251+
nwInfo *network.NetworkInfo
272252
}
273253
type args struct {
274254
address *net.IPNet
@@ -403,7 +383,7 @@ func TestRemoveIpamState_Add(t *testing.T) {
403383
requires := require.New(t)
404384
type fields struct {
405385
plugin delegatePlugin
406-
nwInfo *network.EndpointInfo
386+
nwInfo *network.NetworkInfo
407387
}
408388
type args struct {
409389
nwCfg *cni.NetworkConfig

cni/network/invoker_cns.go

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,9 @@ func (invoker *CNSIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, erro
141141
}
142142
}
143143

144-
addResult := IPAMAddResult{interfaceInfo: make(map[string]network.InterfaceInfo)}
144+
addResult := IPAMAddResult{}
145145
numInterfacesWithDefaultRoutes := 0
146+
146147
for i := 0; i < len(response.PodIPInfo); i++ {
147148
info := IPResultInfo{
148149
podIPAddress: response.PodIPInfo[i].PodIPConfig.IPAddress,
@@ -163,42 +164,29 @@ func (invoker *CNSIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, erro
163164
zap.Any("podInfo", podInfo))
164165

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

175-
// Add secondary interface info from podIPInfo to ipamAddResult
176-
info.hostSubnet = response.PodIPInfo[i].HostPrimaryIPInfo.Subnet
177-
info.hostPrimaryIP = response.PodIPInfo[i].HostPrimaryIPInfo.PrimaryIP
178-
info.hostGateway = response.PodIPInfo[i].HostPrimaryIPInfo.Gateway
179-
180-
if err := configureSecondaryAddResult(&info, &addResult, &response.PodIPInfo[i].PodIPConfig, key); err != nil {
174+
if err := configureSecondaryAddResult(&info, &addResult, &response.PodIPInfo[i].PodIPConfig); err != nil {
181175
return IPAMAddResult{}, err
182176
}
183-
case cns.InfraNIC, "":
184-
// if we change from legacy cns, the nicType will be empty, so we assume it is infra nic
185-
info.nicType = cns.InfraNIC
186-
177+
default:
187178
// only count dualstack interface once
188-
_, exist := addResult.interfaceInfo[key]
189-
if !exist {
190-
addResult.interfaceInfo[key] = network.InterfaceInfo{}
179+
if addResult.defaultInterfaceInfo.IPConfigs == nil {
180+
addResult.defaultInterfaceInfo.IPConfigs = make([]*network.IPConfig, 0)
191181
if !info.skipDefaultRoutes {
192182
numInterfacesWithDefaultRoutes++
193183
}
194184
}
195185

196186
overlayMode := (invoker.ipamMode == util.V4Overlay) || (invoker.ipamMode == util.DualStackOverlay) || (invoker.ipamMode == util.Overlay)
197-
if err := configureDefaultAddResult(&info, &addConfig, &addResult, overlayMode, key); err != nil {
187+
if err := configureDefaultAddResult(&info, &addConfig, &addResult, overlayMode); err != nil {
198188
return IPAMAddResult{}, err
199189
}
200-
default:
201-
logger.Warn("Unknown NIC type received from cns pod ip info", zap.String("nicType", string(info.nicType)))
202190
}
203191
}
204192

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

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

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

@@ -396,21 +384,15 @@ func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, add
396384
}
397385
}
398386

399-
// get the name of the primary IP address
400-
_, hostIPNet, err := net.ParseCIDR(info.hostSubnet)
401-
if err != nil {
402-
return errors.Wrap(err, "unable to parse hostSubnet")
403-
}
404-
405387
if ip := net.ParseIP(info.podIPAddress); ip != nil {
388+
defaultInterfaceInfo := &addResult.defaultInterfaceInfo
406389
defaultRouteDstPrefix := network.Ipv4DefaultRouteDstPrefix
407390
if ip.To4() == nil {
408391
defaultRouteDstPrefix = network.Ipv6DefaultRouteDstPrefix
409392
addResult.ipv6Enabled = true
410393
}
411394

412-
ipConfigs := addResult.interfaceInfo[key].IPConfigs
413-
ipConfigs = append(ipConfigs,
395+
defaultInterfaceInfo.IPConfigs = append(defaultInterfaceInfo.IPConfigs,
414396
&network.IPConfig{
415397
Address: net.IPNet{
416398
IP: ip,
@@ -424,26 +406,27 @@ func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, add
424406
return getRoutesErr
425407
}
426408

427-
resRoute := addResult.interfaceInfo[key].Routes
428409
if len(routes) > 0 {
429-
resRoute = append(resRoute, routes...)
410+
defaultInterfaceInfo.Routes = append(defaultInterfaceInfo.Routes, routes...)
430411
} else { // add default routes if none are provided
431-
resRoute = append(resRoute, network.RouteInfo{
412+
defaultInterfaceInfo.Routes = append(defaultInterfaceInfo.Routes, network.RouteInfo{
432413
Dst: defaultRouteDstPrefix,
433414
Gw: ncgw,
434415
})
435416
}
436-
// if we have multiple infra ip result infos, we effectively append routes and ip configs to that same interface info each time
437-
// the host subnet prefix (in ipv4 or ipv6) will always refer to the same interface regardless of which ip result info we look at
438-
addResult.interfaceInfo[key] = network.InterfaceInfo{
439-
NICType: cns.InfraNIC,
440-
SkipDefaultRoutes: info.skipDefaultRoutes,
441-
IPConfigs: ipConfigs,
442-
Routes: resRoute,
443-
HostSubnetPrefix: *hostIPNet,
444-
}
417+
418+
addResult.defaultInterfaceInfo.SkipDefaultRoutes = info.skipDefaultRoutes
419+
}
420+
421+
// get the name of the primary IP address
422+
_, hostIPNet, err := net.ParseCIDR(info.hostSubnet)
423+
if err != nil {
424+
return fmt.Errorf("unable to parse hostSubnet: %w", err)
445425
}
446426

427+
addResult.hostSubnetPrefix = *hostIPNet
428+
addResult.defaultInterfaceInfo.NICType = cns.InfraNIC
429+
447430
// set subnet prefix for host vm
448431
// setHostOptions will execute if IPAM mode is not v4 overlay and not dualStackOverlay mode
449432
// TODO: Remove v4overlay and dualstackoverlay options, after 'overlay' rolls out in AKS-RP
@@ -456,7 +439,7 @@ func configureDefaultAddResult(info *IPResultInfo, addConfig *IPAMAddConfig, add
456439
return nil
457440
}
458441

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

475-
addResult.interfaceInfo[key] = network.InterfaceInfo{
458+
result := network.InterfaceInfo{
476459
IPConfigs: []*network.IPConfig{
477460
{
478461
Address: net.IPNet{
479462
IP: ip,
480463
Mask: ipnet.Mask,
481464
},
482-
Gateway: net.ParseIP(info.ncGatewayIPAddress),
483465
},
484466
},
485467
Routes: routes,
@@ -488,12 +470,7 @@ func configureSecondaryAddResult(info *IPResultInfo, addResult *IPAMAddResult, p
488470
SkipDefaultRoutes: info.skipDefaultRoutes,
489471
}
490472

491-
return nil
492-
}
473+
addResult.secondaryInterfacesInfo = append(addResult.secondaryInterfacesInfo, result)
493474

494-
func (invoker *CNSIPAMInvoker) getInterfaceInfoKey(nicType cns.NICType, macAddress string) string {
495-
if nicType == cns.DelegatedVMNIC {
496-
return macAddress
497-
}
498-
return string(nicType)
475+
return nil
499476
}

0 commit comments

Comments
 (0)