@@ -19,6 +19,7 @@ import (
1919 "k8s.io/apimachinery/pkg/runtime"
2020 "k8s.io/apimachinery/pkg/types"
2121 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
22+ "sigs.k8s.io/controller-runtime/pkg/client"
2223 k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
2324 "sigs.k8s.io/controller-runtime/pkg/client/fake"
2425
@@ -39,6 +40,13 @@ func mustMarshallSelector(t *testing.T, input *dptypes.NetDeviceSelectors) *json
3940 return & ret
4041}
4142
43+ func mustUnmarshallSelector (input * json.RawMessage ) * dptypes.NetDeviceSelectors {
44+ ret := dptypes.NetDeviceSelectors {}
45+ err := json .Unmarshal (* input , & ret )
46+ ExpectWithOffset (1 , err ).ToNot (HaveOccurred ())
47+ return & ret
48+ }
49+
4250func TestRenderDevicePluginConfigData (t * testing.T ) {
4351 table := []struct {
4452 tname string
@@ -542,4 +550,129 @@ var _ = Describe("SriovNetworkNodePolicyReconciler", Ordered, func() {
542550 Expect (errors .IsNotFound (r .Get (ctx , types.NamespacedName {Name : nodeState .Name }, nodeState ))).To (BeTrue ())
543551 })
544552 })
553+
554+ Context ("renderDevicePluginConfigData" , func () {
555+ It ("should render device plugin config data when policies with the same resource name target different devices" , func () {
556+
557+ intelNode := & corev1.Node {ObjectMeta : metav1.ObjectMeta {Name : "intelNode" , Labels : map [string ]string {"node-role.kubernetes.io/worker" : "" }}}
558+ mlxNode := & corev1.Node {ObjectMeta : metav1.ObjectMeta {Name : "mlxNode" , Labels : map [string ]string {"node-role.kubernetes.io/worker" : "" }}}
559+
560+ objs := []client.Object {
561+ intelNode ,
562+ & sriovnetworkv1.SriovNetworkNodeState {ObjectMeta : metav1.ObjectMeta {Name : "intelNode" , Namespace : testNamespace }, Status : sriovnetworkv1.SriovNetworkNodeStateStatus {
563+ Interfaces : sriovnetworkv1.InterfaceExts {
564+ {Driver : "ice" , DeviceID : "159b" , Vendor : "8086" , PciAddress : "0000:31:00.0" },
565+ },
566+ }},
567+ mlxNode ,
568+ & sriovnetworkv1.SriovNetworkNodeState {ObjectMeta : metav1.ObjectMeta {Name : "mlxNode" , Namespace : testNamespace }, Status : sriovnetworkv1.SriovNetworkNodeStateStatus {
569+ Interfaces : sriovnetworkv1.InterfaceExts {
570+ {Driver : "mlx5_core" , DeviceID : "101d" , Vendor : "15b3" , PciAddress : "0000:ca:00.0" },
571+ },
572+ }},
573+ }
574+
575+ r := & SriovNetworkNodePolicyReconciler {Client : fake .NewClientBuilder ().WithObjects (objs ... ).Build ()}
576+
577+ pl := & sriovnetworkv1.SriovNetworkNodePolicyList {
578+ Items : []sriovnetworkv1.SriovNetworkNodePolicy {
579+ {
580+ ObjectMeta : metav1.ObjectMeta {Name : "intel-vfio-pci" },
581+ Spec : sriovnetworkv1.SriovNetworkNodePolicySpec {
582+ ResourceName : "resource1" ,
583+ DeviceType : "vfio-pci" ,
584+ NicSelector : sriovnetworkv1.SriovNetworkNicSelector {
585+ Vendor : "8086" ,
586+ DeviceID : "159b" ,
587+ RootDevices : []string {"0000:31:00.0" },
588+ },
589+ NumVfs : 128 ,
590+ NodeSelector : map [string ]string {"node-role.kubernetes.io/worker" : "" },
591+ },
592+ },
593+ {
594+ ObjectMeta : metav1.ObjectMeta {Name : "mellanox-rdma" },
595+ Spec : sriovnetworkv1.SriovNetworkNodePolicySpec {
596+ ResourceName : "resource1" ,
597+ DeviceType : "netdevice" ,
598+ IsRdma : true ,
599+ NicSelector : sriovnetworkv1.SriovNetworkNicSelector {
600+ Vendor : "15b3" ,
601+ DeviceID : "101d" ,
602+ RootDevices : []string {"0000:ca:00.0" },
603+ },
604+ NumVfs : 128 ,
605+ NodeSelector : map [string ]string {"node-role.kubernetes.io/worker" : "" },
606+ },
607+ },
608+ },
609+ }
610+ rcl , err := r .renderDevicePluginConfigData (context .Background (), pl , mlxNode )
611+ Expect (err ).ToNot (HaveOccurred ())
612+ Expect (len (rcl .ResourceList )).To (Equal (1 ))
613+ Expect (rcl .ResourceList [0 ].ResourceName ).To (Equal ("resource1" ))
614+ selectors := mustUnmarshallSelector (rcl .ResourceList [0 ].Selectors )
615+ Expect (selectors .Vendors ).To (ContainElements ("8086" , "15b3" ))
616+ Expect (selectors .RootDevices ).To (ContainElements ("0000:31:00.0" , "0000:ca:00.0" ))
617+
618+ // Having drivers in the selector cause the device plugin to fail to select the mellanox devices in the mlxNode
619+ Expect (selectors .Drivers ).To (BeEmpty ())
620+ })
621+
622+ It ("should render device plugin config data when policies configure vfio-pci and netdevice" , func () {
623+ node1 := & corev1.Node {ObjectMeta : metav1.ObjectMeta {Name : "node1" , Labels : map [string ]string {"node-role.kubernetes.io/worker" : "" }}}
624+ objs := []client.Object {
625+ node1 ,
626+ & sriovnetworkv1.SriovNetworkNodeState {ObjectMeta : metav1.ObjectMeta {Name : "node1" , Namespace : testNamespace }, Status : sriovnetworkv1.SriovNetworkNodeStateStatus {
627+ Interfaces : sriovnetworkv1.InterfaceExts {
628+ {Driver : "ice" , DeviceID : "159b" , Vendor : "8086" , PciAddress : "0000:31:00.0" , Name : "ens0" },
629+ },
630+ }},
631+ }
632+
633+ r := & SriovNetworkNodePolicyReconciler {Client : fake .NewClientBuilder ().WithObjects (objs ... ).Build ()}
634+
635+ pl := & sriovnetworkv1.SriovNetworkNodePolicyList {
636+ Items : []sriovnetworkv1.SriovNetworkNodePolicy {
637+ {
638+ ObjectMeta : metav1.ObjectMeta {Name : "intel-vfio-pci" },
639+ Spec : sriovnetworkv1.SriovNetworkNodePolicySpec {
640+ ResourceName : "resvfiopci" ,
641+ DeviceType : "vfio-pci" ,
642+ NicSelector : sriovnetworkv1.SriovNetworkNicSelector {
643+ Vendor : "8086" ,
644+ DeviceID : "159b" ,
645+ PfNames : []string {"ens0#0-9" },
646+ },
647+ NumVfs : 128 ,
648+ NodeSelector : map [string ]string {"node-role.kubernetes.io/worker" : "" },
649+ },
650+ },
651+ {
652+ ObjectMeta : metav1.ObjectMeta {Name : "mellanox-rdma" },
653+ Spec : sriovnetworkv1.SriovNetworkNodePolicySpec {
654+ ResourceName : "resnetdevice" ,
655+ DeviceType : "netdevice" ,
656+ NicSelector : sriovnetworkv1.SriovNetworkNicSelector {
657+ Vendor : "8086" ,
658+ DeviceID : "159b" ,
659+ PfNames : []string {"ens0#10-19" },
660+ },
661+ NumVfs : 128 ,
662+ NodeSelector : map [string ]string {"node-role.kubernetes.io/worker" : "" },
663+ },
664+ },
665+ },
666+ }
667+ rcl , err := r .renderDevicePluginConfigData (context .Background (), pl , node1 )
668+ Expect (err ).ToNot (HaveOccurred ())
669+ Expect (len (rcl .ResourceList )).To (Equal (2 ))
670+ selectors := map [string ]string {
671+ rcl .ResourceList [0 ].ResourceName : string (* rcl .ResourceList [0 ].Selectors ),
672+ rcl .ResourceList [1 ].ResourceName : string (* rcl .ResourceList [1 ].Selectors ),
673+ }
674+ Expect (selectors ).To (HaveKeyWithValue ("resvfiopci" , `{"vendors":["8086"],"pfNames":["ens0#0-9"],"IsRdma":false,"NeedVhostNet":false}` ))
675+ Expect (selectors ).To (HaveKeyWithValue ("resnetdevice" , `{"vendors":["8086"],"pfNames":["ens0#10-19"],"IsRdma":false,"NeedVhostNet":false}` ))
676+ })
677+ })
545678})
0 commit comments