@@ -170,11 +170,11 @@ func add(args *skel.CmdArgs, cniTypes typeswrapper.CNITYPES, grpcClient grpcwrap
170
170
requiresMultiNICAttachment = true
171
171
} else {
172
172
log .Debugf ("Multi-NIC annotation mismatch: found=%q, required=%q. Falling back to default configuration." ,
173
- multiNICAttachment , conf .RuntimeConfig .PodAnnotations [multiNICPodAnnotation ])
173
+ conf .RuntimeConfig .PodAnnotations [multiNICPodAnnotation ], multiNICAttachment )
174
174
}
175
175
}
176
176
177
- log .Debugf ("pod requires Multi-NIC attachment: %t" , requiresMultiNICAttachment )
177
+ log .Debugf ("pod requires multi-nic attachment: %t" , requiresMultiNICAttachment )
178
178
179
179
// Set up a connection to the ipamD server.
180
180
conn , err := grpcClient .Dial (ipamdAddress , grpc .WithTransportCredentials (insecure .NewCredentials ()))
@@ -245,12 +245,11 @@ func add(args *skel.CmdArgs, cniTypes typeswrapper.CNITYPES, grpcClient grpcwrap
245
245
HostVethName : hostVethName ,
246
246
ContainerVethName : containerVethName ,
247
247
})
248
-
249
- // Check if we can use PCIID to store RT-ID ?
250
- // The RT here is the host route table Id not the container
248
+ // CNI stores the route table ID in the MAC field of the interface. The Route table ID is on the host side
249
+ // and is used during cleanup to remove the ip rules when IPAMD is not reachable.
251
250
podInterfaces = append (podInterfaces ,
252
251
& current.Interface {Name : hostVethName },
253
- & current.Interface {Name : containerVethName , Sandbox : args .Netns , PciID : fmt .Sprint (ip .RouteTableId )},
252
+ & current.Interface {Name : containerVethName , Sandbox : args .Netns , Mac : fmt .Sprint (ip .RouteTableId )},
254
253
)
255
254
256
255
// This index always points to the container interface so that we get the IP address corresponding to the interface
@@ -276,7 +275,7 @@ func add(args *skel.CmdArgs, cniTypes typeswrapper.CNITYPES, grpcClient grpcwrap
276
275
} else {
277
276
err = driverClient .SetupPodNetwork (vethMetadata , args .Netns , mtu , log )
278
277
// For non-branch ENI, the pod VLAN ID value of 0 is packed in Interface.Mac, while the interface device number is packed in Interface.Sandbox
279
- dummyInterface = & current.Interface {Name : dummyInterfaceName , Mac : fmt .Sprint (0 ), Sandbox : fmt .Sprint (vethMetadata [0 ].DeviceNumber ), SocketPath : fmt . Sprint ( len ( r . IPAddress )) }
278
+ dummyInterface = & current.Interface {Name : dummyInterfaceName , Mac : fmt .Sprint (0 ), Sandbox : fmt .Sprint (vethMetadata [0 ].DeviceNumber )}
280
279
}
281
280
282
281
log .Debugf ("Using dummy interface: %v" , dummyInterface )
@@ -311,6 +310,8 @@ func add(args *skel.CmdArgs, cniTypes typeswrapper.CNITYPES, grpcClient grpcwrap
311
310
}
312
311
313
312
// dummy interface is appended to PrevResult for use during cleanup
313
+ // The interfaces field should only include host,container and dummy interfaces in the list.
314
+ // Revisit the prevResult cleanup logic if this changes
314
315
result .Interfaces = append (result .Interfaces , dummyInterface )
315
316
316
317
// Set up a connection to the network policy agent
@@ -345,7 +346,6 @@ func add(args *skel.CmdArgs, cniTypes typeswrapper.CNITYPES, grpcClient grpcwrap
345
346
}
346
347
347
348
log .Debugf ("Network Policy agent for EnforceNpToPod returned Success : %v" , npr .Success )
348
-
349
349
return cniTypes .PrintResult (result , conf .CNIVersion )
350
350
}
351
351
@@ -532,8 +532,13 @@ func getContainerNetworkMetadata(prevResult *current.Result, contVethName string
532
532
// returns true if the del request is handled.
533
533
func tryDelWithPrevResult (driverClient driver.NetworkAPIs , conf * NetConf , k8sArgs K8sArgs , contVethName string , netNS string , log logger.Logger ) (bool , error ) {
534
534
// prevResult might not be available, if we are still using older cni spec < 0.4.0.
535
- prevResult , ok := conf .PrevResult .(* current.Result )
536
- if ! ok {
535
+ if conf .PrevResult == nil {
536
+ return false , nil
537
+ }
538
+
539
+ prevResult , err := current .NewResultFromResult (conf .PrevResult )
540
+ if err != nil {
541
+ log .Info ("PrevResult not available for pod or parsing failed" )
537
542
return false , nil
538
543
}
539
544
@@ -574,8 +579,13 @@ func tryDelWithPrevResult(driverClient driver.NetworkAPIs, conf *NetConf, k8sArg
574
579
// Returns true if pod network is torn down
575
580
func teardownPodNetworkWithPrevResult (driverClient driver.NetworkAPIs , conf * NetConf , k8sArgs K8sArgs , contVethName string , log logger.Logger ) bool {
576
581
// For non-branch ENI, prevResult is only available in v1.12.1+
577
- prevResult , ok := conf .PrevResult .(* current.Result )
578
- if ! ok {
582
+ if conf .PrevResult == nil {
583
+ log .Infof ("PrevResult not available for pod. Pod may have already been deleted." )
584
+ return false
585
+ }
586
+
587
+ prevResult , err := current .NewResultFromResult (conf .PrevResult )
588
+ if err != nil {
579
589
log .Infof ("PrevResult not available for pod. Pod may have already been deleted." )
580
590
return false
581
591
}
@@ -600,32 +610,24 @@ func teardownPodNetworkWithPrevResult(driverClient driver.NetworkAPIs, conf *Net
600
610
// RT ID for NC-0 is also stored in the container interface entry. So we have a path for migration where
601
611
// getting the device number from dummy interface can be deprecated entirely. This is currently done to keep it backwards compatible
602
612
routeTableId := deviceNumber + 1
603
-
604
- var interfacesAttached = 1
605
- if dummyIface .SocketPath != "" {
606
- interfacesAttached , err = strconv .Atoi (dummyIface .SocketPath )
607
- if err != nil {
608
- log .Errorf ("error getting number of interfaces attached to the pod: %s" , dummyIface .SocketPath )
609
- return false
610
- }
611
- }
612
-
613
+ // The number of interfaces attached to the pod is taken as the length of the interfaces array - 1 (for dummy interface) divided by 2 (for host and container interface)
614
+ var interfacesAttached = (len (prevResult .Interfaces ) - 1 ) / 2
613
615
var vethMetadata []driver.VirtualInterfaceMetadata
614
616
for v := range interfacesAttached {
615
617
containerInterfaceName := networkutils .GenerateContainerVethName (contVethName , containerVethNamePrefix , v )
616
618
containerIP , containerInterface , err := getContainerNetworkMetadata (prevResult , containerInterfaceName )
617
619
if err != nil {
618
- log .Errorf ("Failed to get container IP: %v" , err )
619
- return false
620
+ log .Errorf ("container interface name %s does not exist %v" , containerInterfaceName , err )
621
+ continue
620
622
}
621
623
622
624
// If this property is set, that means the container metadata has the route table ID which we can use
623
625
// If this is not set, it is a pod launched before this change was introduced.
624
- // So it is only managing network card 0 at that time and device number + 1 is the route table ID which we have above
625
- if dummyIface . SocketPath != "" {
626
- routeTableId , err = strconv .Atoi (containerInterface .PciID )
626
+ // So it is only managing network card 0 at that time and device number + 1 is the route table ID which we calculate from device number
627
+ if containerInterface . Mac != "" {
628
+ routeTableId , err = strconv .Atoi (containerInterface .Mac )
627
629
if err != nil {
628
- log .Errorf ("error getting route table number of the interface %s" , containerInterface .PciID )
630
+ log .Errorf ("error getting route table number of the interface %s" , containerInterface .Mac )
629
631
return false
630
632
}
631
633
}
0 commit comments