@@ -78,27 +78,34 @@ const (
78
78
"vlan": {{.VLAN}}
79
79
}`
80
80
81
- defaultCNIVersion = "0.3.0"
82
- defaultMTU = 1500
83
- sriovDeviceID = "sriov-device-id"
84
- podName = "pod1"
85
- containerID = "container1"
86
- podIP = "1.2.3.4"
87
- networkName = "net"
88
- interfaceName = "eth2"
81
+ defaultCNIVersion = "0.3.0"
82
+ defaultMTU = 1500
83
+ sriovResourceName1 = "intel.com/intel_sriov_netdevice"
84
+ sriovResourceName2 = "mellanox.com/mlnx_connectx5"
85
+ sriovDeviceID11 = "sriov-device-id-11"
86
+ sriovDeviceID12 = "sriov-device-id-12"
87
+ sriovDeviceID21 = "sriov-device-id-21"
88
+ podName = "pod1"
89
+ containerID = "container1"
90
+ podIP = "1.2.3.4"
91
+ networkName = "net"
92
+ interfaceName = "eth2"
89
93
)
90
94
91
95
func testNetwork (name string , networkType networkType ) * netdefv1.NetworkAttachmentDefinition {
92
- return testNetworkExt (name , "" , "" , string ( networkType ) , "" , 0 , 0 , false )
96
+ return testNetworkExt (name , "" , "" , networkType , "" , "" , 0 , 0 , false )
93
97
}
94
98
95
- func testNetworkExt (name , cniVersion , cniType , networkType , ipamType string , mtu , vlan int , noIPAM bool ) * netdefv1.NetworkAttachmentDefinition {
99
+ func testNetworkExt (name , cniVersion , cniType string , networkType networkType , resourceName , ipamType string , mtu , vlan int , noIPAM bool ) * netdefv1.NetworkAttachmentDefinition {
96
100
if cniVersion == "" {
97
101
cniVersion = defaultCNIVersion
98
102
}
99
103
if cniType == "" {
100
104
cniType = "antrea"
101
105
}
106
+ if networkType == sriovNetworkType && resourceName == "" {
107
+ resourceName = sriovResourceName1
108
+ }
102
109
if ipamType == "" {
103
110
ipamType = ipam .AntreaIPAMType
104
111
}
@@ -109,7 +116,7 @@ func testNetworkExt(name, cniVersion, cniType, networkType, ipamType string, mtu
109
116
IPAMType string
110
117
MTU int
111
118
VLAN int
112
- }{cniVersion , cniType , networkType , ipamType , mtu , vlan }
119
+ }{cniVersion , cniType , string ( networkType ) , ipamType , mtu , vlan }
113
120
114
121
var tmpl * template.Template
115
122
if ! noIPAM {
@@ -119,9 +126,14 @@ func testNetworkExt(name, cniVersion, cniType, networkType, ipamType string, mtu
119
126
}
120
127
var b bytes.Buffer
121
128
tmpl .Execute (& b , & data )
129
+ annotations := make (map [string ]string )
130
+ if resourceName != "" {
131
+ annotations [resourceNameAnnotationKey ] = resourceName
132
+ }
122
133
return & netdefv1.NetworkAttachmentDefinition {
123
134
ObjectMeta : metav1.ObjectMeta {
124
- Name : name ,
135
+ Name : name ,
136
+ Annotations : annotations ,
125
137
},
126
138
Spec : netdefv1.NetworkAttachmentDefinitionSpec {
127
139
Config : b .String (),
@@ -189,8 +201,11 @@ func testIPAMResult(cidr string, vlan int) *ipam.IPAMResult {
189
201
}
190
202
191
203
func init () {
192
- getPodContainerDeviceIDsFn = func (name string , namespace string ) ([]string , error ) {
193
- return []string {sriovDeviceID }, nil
204
+ getPodContainerDeviceIDsFn = func (name string , namespace string ) (map [string ][]string , error ) {
205
+ return map [string ][]string {
206
+ sriovResourceName1 : {sriovDeviceID11 , sriovDeviceID12 },
207
+ sriovResourceName2 : {sriovDeviceID21 },
208
+ }, nil
194
209
}
195
210
}
196
211
@@ -258,7 +273,7 @@ func TestPodControllerRun(t *testing.T) {
258
273
containerNetNs (containerID ),
259
274
interfaceName ,
260
275
defaultMTU ,
261
- sriovDeviceID ,
276
+ sriovDeviceID11 ,
262
277
& ipamResult .Result ,
263
278
).Do (func (string , string , string , string , string , int , string , * current.Result ) {
264
279
atomic .AddInt32 (& interfaceConfigured , 1 )
@@ -277,7 +292,7 @@ func TestPodControllerRun(t *testing.T) {
277
292
_ , err = client .CoreV1 ().Pods (testNamespace ).Create (context .Background (), pod , metav1.CreateOptions {})
278
293
require .NoError (t , err , "error when creating test Pod" )
279
294
280
- // Wait for ConfigureSriovSecondaryInterface is called.
295
+ // Wait for ConfigureSriovSecondaryInterface to be called.
281
296
assert .Eventually (t , func () bool {
282
297
return atomic .LoadInt32 (& interfaceConfigured ) == 1
283
298
}, 1 * time .Second , 10 * time .Millisecond )
@@ -292,7 +307,8 @@ func TestPodControllerRun(t *testing.T) {
292
307
containerNetNs (containerID ),
293
308
interfaceName ,
294
309
defaultMTU ,
295
- "" ,
310
+ // We haven't updated the vfDeviceIDUsageMap, so a different device will be allocated.
311
+ sriovDeviceID12 ,
296
312
& ipamResult .Result ,
297
313
).Do (func (string , string , string , string , string , int , string , * current.Result ) {
298
314
atomic .AddInt32 (& interfaceConfigured , 1 )
@@ -329,7 +345,7 @@ func TestPodControllerRun(t *testing.T) {
329
345
mockIPAM .EXPECT ().SecondaryNetworkRelease (podOwner )
330
346
// CNI Del event.
331
347
event .IsAdd = false
332
- // Interfac is not deleted from the interface store, so CNI Del should trigger interface
348
+ // Interface is not deleted from the interface store, so CNI Del should trigger interface
333
349
// deletion again.
334
350
podController .processCNIUpdate (event )
335
351
_ , exists = cniCache .Load (podKey )
@@ -455,7 +471,7 @@ func TestConfigurePodSecondaryNetwork(t *testing.T) {
455
471
containerNetNs (containerID ),
456
472
interfaceName ,
457
473
1500 ,
458
- sriovDeviceID ,
474
+ sriovDeviceID11 ,
459
475
& testIPAMResult ("148.14.24.100/24" , 0 ).Result ,
460
476
)
461
477
},
@@ -561,7 +577,7 @@ func TestConfigurePodSecondaryNetwork(t *testing.T) {
561
577
562
578
if ! tc .doNotCreateNetwork {
563
579
network1 := testNetworkExt (networkName , tc .cniVersion , tc .cniType ,
564
- string ( tc .networkType ) , tc .ipamType , tc .mtu , tc .vlan , tc .noIPAM )
580
+ tc .networkType , "" , tc .ipamType , tc .mtu , tc .vlan , tc .noIPAM )
565
581
pc .netAttachDefClient .NetworkAttachmentDefinitions (testNamespace ).Create (context .Background (),
566
582
network1 , metav1.CreateOptions {})
567
583
}
@@ -579,6 +595,74 @@ func TestConfigurePodSecondaryNetwork(t *testing.T) {
579
595
580
596
}
581
597
598
+ func TestConfigurePodSecondaryNetworkMultipleSriovDevices (t * testing.T ) {
599
+ ctx := context .Background ()
600
+ element1 := netdefv1.NetworkSelectionElement {
601
+ Namespace : testNamespace ,
602
+ Name : "net1" ,
603
+ InterfaceRequest : "eth10" ,
604
+ }
605
+ element2 := netdefv1.NetworkSelectionElement {
606
+ Namespace : testNamespace ,
607
+ Name : "net2" ,
608
+ InterfaceRequest : "eth11" ,
609
+ }
610
+ pod , cniInfo := testPod (podName , containerID , podIP , element1 , element2 )
611
+ ctrl := gomock .NewController (t )
612
+ pc , _ , interfaceConfigurator := testPodControllerStart (ctrl )
613
+
614
+ network1 := testNetworkExt ("net1" , "" , "" , sriovNetworkType , sriovResourceName1 , "" , 1500 , 0 , true /* noIPAM */ )
615
+ pc .netAttachDefClient .NetworkAttachmentDefinitions (testNamespace ).Create (ctx , network1 , metav1.CreateOptions {})
616
+ network2 := testNetworkExt ("net2" , "" , "" , sriovNetworkType , sriovResourceName2 , "" , 1500 , 0 , true /* noIPAM */ )
617
+ pc .netAttachDefClient .NetworkAttachmentDefinitions (testNamespace ).Create (ctx , network2 , metav1.CreateOptions {})
618
+
619
+ gomock .InOrder (
620
+ interfaceConfigurator .EXPECT ().ConfigureSriovSecondaryInterface (
621
+ podName ,
622
+ testNamespace ,
623
+ containerID ,
624
+ containerNetNs (containerID ),
625
+ element2 .InterfaceRequest ,
626
+ 1500 ,
627
+ sriovDeviceID21 ,
628
+ gomock .Any (),
629
+ ),
630
+ interfaceConfigurator .EXPECT ().ConfigureSriovSecondaryInterface (
631
+ podName ,
632
+ testNamespace ,
633
+ containerID ,
634
+ containerNetNs (containerID ),
635
+ element1 .InterfaceRequest ,
636
+ 1500 ,
637
+ sriovDeviceID11 ,
638
+ gomock .Any (),
639
+ ),
640
+ )
641
+ assert .NoError (t , pc .configurePodSecondaryNetwork (pod , []* netdefv1.NetworkSelectionElement {& element2 , & element1 }, cniInfo ))
642
+
643
+ podKey := podKeyGet (pod .Name , pod .Namespace )
644
+ deviceCache , ok := pc .vfDeviceIDUsageMap .Load (podKey )
645
+ require .True (t , ok )
646
+ expectedDeviceCache := []podSriovVFDeviceIDInfo {
647
+ {
648
+ resourceName : sriovResourceName1 ,
649
+ vfDeviceID : sriovDeviceID11 ,
650
+ ifName : element1 .InterfaceRequest ,
651
+ },
652
+ {
653
+ resourceName : sriovResourceName1 ,
654
+ vfDeviceID : sriovDeviceID12 ,
655
+ ifName : "" ,
656
+ },
657
+ {
658
+ resourceName : sriovResourceName2 ,
659
+ vfDeviceID : sriovDeviceID21 ,
660
+ ifName : element2 .InterfaceRequest ,
661
+ },
662
+ }
663
+ assert .ElementsMatch (t , expectedDeviceCache , deviceCache .([]podSriovVFDeviceIDInfo ))
664
+ }
665
+
582
666
func TestPodControllerAddPod (t * testing.T ) {
583
667
pod , _ := testPod (podName , containerID , podIP , netdefv1.NetworkSelectionElement {
584
668
Name : networkName ,
@@ -623,7 +707,7 @@ func TestPodControllerAddPod(t *testing.T) {
623
707
)
624
708
network1 := testNetwork ("net1" , sriovNetworkType )
625
709
testVLAN := 100
626
- network2 := testNetworkExt ("net2" , "" , "" , string ( vlanNetworkType ) , "" , defaultMTU , testVLAN , false )
710
+ network2 := testNetworkExt ("net2" , "" , "" , vlanNetworkType , "" , "" , defaultMTU , testVLAN , false )
627
711
628
712
podOwner1 := & crdv1beta1.PodOwner {Name : podName , Namespace : testNamespace ,
629
713
ContainerID : containerID , IFName : "eth10" }
@@ -771,7 +855,7 @@ func TestPodControllerAddPod(t *testing.T) {
771
855
containerNetNs (containerID ),
772
856
interfaceName ,
773
857
defaultMTU ,
774
- sriovDeviceID ,
858
+ sriovDeviceID11 ,
775
859
gomock .Any (),
776
860
)
777
861
mockIPAM .EXPECT ().SecondaryNetworkAllocate (gomock .Any (), gomock .Any ()).Return (testIPAMResult ("148.14.24.100/24" , 0 ), nil )
@@ -885,7 +969,7 @@ func TestPodControllerAddPod(t *testing.T) {
885
969
t .Run ("updating deviceID cache per Pod" , func (t * testing.T ) {
886
970
ctrl := gomock .NewController (t )
887
971
podController , _ , _ := testPodController (ctrl )
888
- _ , err := podController .assignUnusedSriovVFDeviceID (podName , testNamespace , interfaceName )
972
+ _ , err := podController .assignUnusedSriovVFDeviceID (podName , testNamespace , sriovResourceName1 , interfaceName )
889
973
_ , exists := podController .vfDeviceIDUsageMap .Load (podKey )
890
974
assert .True (t , exists )
891
975
require .NoError (t , err , "error while assigning unused VfDevice ID" )
0 commit comments