@@ -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
@@ -348,7 +349,6 @@ func add(args *skel.CmdArgs, cniTypes typeswrapper.CNITYPES, grpcClient grpcwrap
348
349
}
349
350
350
351
log .Debugf ("Network Policy agent for EnforceNpToPod returned Success : %v" , npr .Success )
351
-
352
352
return cniTypes .PrintResult (result , conf .CNIVersion )
353
353
}
354
354
@@ -539,8 +539,13 @@ func getContainerNetworkMetadata(prevResult *current.Result, contVethName string
539
539
// returns true if the del request is handled.
540
540
func tryDelWithPrevResult (driverClient driver.NetworkAPIs , conf * NetConf , k8sArgs K8sArgs , contVethName string , netNS string , log logger.Logger ) (bool , error ) {
541
541
// prevResult might not be available, if we are still using older cni spec < 0.4.0.
542
- prevResult , ok := conf .PrevResult .(* current.Result )
543
- if ! ok {
542
+ if conf .PrevResult == nil {
543
+ return false , nil
544
+ }
545
+
546
+ prevResult , err := current .NewResultFromResult (conf .PrevResult )
547
+ if err != nil {
548
+ log .Info ("PrevResult not available for pod or parsing failed" )
544
549
return false , nil
545
550
}
546
551
@@ -581,8 +586,13 @@ func tryDelWithPrevResult(driverClient driver.NetworkAPIs, conf *NetConf, k8sArg
581
586
// Returns true if pod network is torn down
582
587
func teardownPodNetworkWithPrevResult (driverClient driver.NetworkAPIs , conf * NetConf , k8sArgs K8sArgs , contVethName string , log logger.Logger ) bool {
583
588
// For non-branch ENI, prevResult is only available in v1.12.1+
584
- prevResult , ok := conf .PrevResult .(* current.Result )
585
- if ! ok {
589
+ if conf .PrevResult == nil {
590
+ log .Infof ("PrevResult not available for pod. Pod may have already been deleted." )
591
+ return false
592
+ }
593
+
594
+ prevResult , err := current .NewResultFromResult (conf .PrevResult )
595
+ if err != nil {
586
596
log .Infof ("PrevResult not available for pod. Pod may have already been deleted." )
587
597
return false
588
598
}
@@ -607,32 +617,24 @@ func teardownPodNetworkWithPrevResult(driverClient driver.NetworkAPIs, conf *Net
607
617
// RT ID for NC-0 is also stored in the container interface entry. So we have a path for migration where
608
618
// getting the device number from dummy interface can be deprecated entirely. This is currently done to keep it backwards compatible
609
619
routeTableId := deviceNumber + 1
610
-
611
- var interfacesAttached = 1
612
- if dummyIface .SocketPath != "" {
613
- interfacesAttached , err = strconv .Atoi (dummyIface .SocketPath )
614
- if err != nil {
615
- log .Errorf ("error getting number of interfaces attached to the pod: %s" , dummyIface .SocketPath )
616
- return false
617
- }
618
- }
619
-
620
+ // 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)
621
+ var interfacesAttached = (len (prevResult .Interfaces ) - 1 ) / 2
620
622
var vethMetadata []driver.VirtualInterfaceMetadata
621
623
for v := range interfacesAttached {
622
624
containerInterfaceName := networkutils .GenerateContainerVethName (contVethName , containerVethNamePrefix , v )
623
625
containerIP , containerInterface , err := getContainerNetworkMetadata (prevResult , containerInterfaceName )
624
626
if err != nil {
625
- log .Errorf ("Failed to get container IP: %v" , err )
626
- return false
627
+ log .Errorf ("container interface name %s does not exist %v" , containerInterfaceName , err )
628
+ continue
627
629
}
628
630
629
631
// If this property is set, that means the container metadata has the route table ID which we can use
630
632
// If this is not set, it is a pod launched before this change was introduced.
631
- // So it is only managing network card 0 at that time and device number + 1 is the route table ID which we have above
632
- if dummyIface . SocketPath != "" {
633
- routeTableId , err = strconv .Atoi (containerInterface .PciID )
633
+ // 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
634
+ if containerInterface . Mac != "" {
635
+ routeTableId , err = strconv .Atoi (containerInterface .Mac )
634
636
if err != nil {
635
- log .Errorf ("error getting route table number of the interface %s" , containerInterface .PciID )
637
+ log .Errorf ("error getting route table number of the interface %s" , containerInterface .Mac )
636
638
return false
637
639
}
638
640
}
0 commit comments