Skip to content

Commit 1a99d09

Browse files
authored
Merge pull request #517 from almaslennikov/pkey-selector
Allow to filter net devices by Infiniband Partition Key
2 parents 7720052 + 93507ca commit 1a99d09

28 files changed

+446
-65
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ These selectors are applicable when "deviceType" is "accelerator".
326326
These selectors are applicable when "deviceType" is "netDevice" (note: this is default)
327327

328328

329-
| Field | Required | Description | Type/Defaults | Example/Accepted values |
329+
| Field | Required | Description | Type/Defaults | Example/Accepted values |
330330
|----------------|----------|--------------------------------------------------------------------------|-----------------------------------------------------|--------------------------------------------------------------------------------------------------|
331331
| "vendors" | N | Target device's vendor Hex code as string | `string` list Default: `null` | "vendors": ["8086", "15b3"] |
332332
| "devices" | N | Target Devices' device Hex code as string | `string` list Default: `null` | "devices": ["154c", "1889", "1018"] |
@@ -337,6 +337,7 @@ These selectors are applicable when "deviceType" is "netDevice" (note: this is d
337337
| "rootDevices" | N | functions from PF matches list of PF PCI addresses | `string` list Default: `null` | "rootDevices": ["0000:86:00.0"] (See follow-up sections for some advance usage of "rootDevices") |
338338
| "linkTypes" | N | The link type of the net device associated with the PCI device | `string` list Default: `null` | "linkTypes": ["ether"] |
339339
| "ddpProfiles" | N | A map of device selectors | `string` list Default: `null` | "ddpProfiles": ["GTPv1-C/U IPv4/IPv6 payload"] |
340+
| "pKeys" | N | Infiniband Partition Keys. Will match only to the devices' default (index0) PKeys. Compatible only with linkTypes = infiniband | `string` list Default: `null` | "pKeys": ["0x1", "0xABCD", "0x50"] |
340341
| "isRdma" | N | Mount RDMA resources. Incompatible with vdpaType | `bool` values `true` or `false` Default: `false` | "isRdma": `true` |
341342
| "needVhostNet" | N | Share /dev/vhost-net and /dev/net/tun | `bool` values `true` or `false` Default: `false` | "needVhostNet": `true` |
342343
| "vdpaType" | N | The type of vDPA device (virtio, vhost). Incompatible with isRdma = true | `string` values `vhost` or `virtio` Default: `null` | "vdpaType": "vhost" |

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/jaypipes/pcidb v1.0.0
1111
github.com/k8snetworkplumbingwg/govdpa v0.1.4
1212
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0
13-
github.com/k8snetworkplumbingwg/sriovnet v1.2.0
13+
github.com/k8snetworkplumbingwg/sriovnet v1.2.1-0.20240128120937-3ca5e43034e6
1414
github.com/onsi/ginkgo v1.16.5
1515
github.com/onsi/gomega v1.30.0
1616
github.com/pkg/errors v0.9.1
@@ -50,7 +50,7 @@ require (
5050
github.com/opencontainers/runtime-spec v1.0.3-0.20220825212826-86290f6a00fb // indirect
5151
github.com/opencontainers/runtime-tools v0.9.1-0.20221107090550-2e043c6bd626 // indirect
5252
github.com/pmezard/go-difflib v1.0.0 // indirect
53-
github.com/spf13/afero v1.9.4 // indirect
53+
github.com/spf13/afero v1.9.5 // indirect
5454
github.com/stretchr/objx v0.5.0 // indirect
5555
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
5656
github.com/vishvananda/netns v0.0.4 // indirect

go.sum

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ github.com/k8snetworkplumbingwg/govdpa v0.1.4 h1:e6mM7JFZkLVJeMQw3px96EigHAhnb4V
203203
github.com/k8snetworkplumbingwg/govdpa v0.1.4/go.mod h1:UQR1xu7A+nnRK1dkLEi12OnNL0OiBPpIKOYDuaQQkck=
204204
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0 h1:VzM3TYHDgqPkettiP6I6q2jOeQFL4nrJM+UcAc4f6Fs=
205205
github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.4.0/go.mod h1:nqCI7aelBJU61wiBeeZWJ6oi4bJy5nrjkM6lWIMA4j0=
206-
github.com/k8snetworkplumbingwg/sriovnet v1.2.0 h1:6ELfAxCB1dvosGUy3DVRmfH+HWTzmPD3W67HKQvMR1M=
207-
github.com/k8snetworkplumbingwg/sriovnet v1.2.0/go.mod h1:jyWzGe6ZtYiPq6ih6aXCOy6mZ49Y9mNyBOLBBXnli+k=
206+
github.com/k8snetworkplumbingwg/sriovnet v1.2.1-0.20240128120937-3ca5e43034e6 h1:Ho6fhRwqgow7ytMgQ1/55j1gG5AbkxIYRbAhJnXr/MM=
207+
github.com/k8snetworkplumbingwg/sriovnet v1.2.1-0.20240128120937-3ca5e43034e6/go.mod h1:LuzcqxxXdSgopWe1yo2kQFSgFTz9Ec5qLu6bb0s5Ut4=
208208
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
209209
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
210210
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
@@ -269,8 +269,8 @@ github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod
269269
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
270270
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
271271
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
272-
github.com/spf13/afero v1.9.4 h1:Sd43wM1IWz/s1aVXdOBkjJvuP8UdyqioeE4AmM0QsBs=
273-
github.com/spf13/afero v1.9.4/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
272+
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
273+
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
274274
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
275275
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
276276
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -322,7 +322,7 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
322322
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
323323
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
324324
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
325-
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
325+
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
326326
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
327327
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
328328
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
@@ -392,6 +392,7 @@ golang.org/x/net v0.0.0-20201031054903-ff519b6c9102/go.mod h1:sp8m0HH+o8qH0wwXwY
392392
golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
393393
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
394394
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
395+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
395396
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
396397
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
397398
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
@@ -462,6 +463,7 @@ golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7w
462463
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
463464
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
464465
golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
466+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
465467
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
466468
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
467469
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -482,6 +484,7 @@ golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3
482484
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
483485
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
484486
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
487+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
485488
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
486489
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
487490
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=

pkg/factory/factory.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ func (rf *resourceFactory) GetSelector(attr string, values []string) (types.Devi
9999
return resources.NewDdpSelector(values), nil
100100
case "auxTypes":
101101
return resources.NewAuxTypeSelector(values), nil
102+
case "pKeys":
103+
return resources.NewPKeySelector(values), nil
102104
default:
103105
return nil, fmt.Errorf("GetSelector(): invalid attribute %s", attr)
104106
}

pkg/factory/factory_test.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ var _ = Describe("Factory", func() {
9797
Entry("rootDevices", "rootDevices", true, reflect.TypeOf(resources.NewRootDeviceSelector([]string{}))),
9898
Entry("linkTypes", "linkTypes", true, reflect.TypeOf(resources.NewLinkTypeSelector([]string{}))),
9999
Entry("ddpProfiles", "ddpProfiles", true, reflect.TypeOf(resources.NewDdpSelector([]string{}))),
100+
Entry("pKeys", "pKeys", true, reflect.TypeOf(resources.NewPKeySelector([]string{}))),
100101
Entry("invalid", "fakeAndInvalid", false, reflect.TypeOf(nil)),
101102
)
102103
Describe("getting resource pool for netdevice", func() {
@@ -119,6 +120,7 @@ var _ = Describe("Factory", func() {
119120
rootDevices := []string{"0000:86:00.0", "0000:86:00.1", "0000:86:00.2", "0000:86:00.3"}
120121
linkTypes := []string{"ether", "infiniband", "other", "other2"}
121122
ddpProfiles := []string{"GTP", "PPPoE", "GTP", "PPPoE"}
123+
pKeys := []string{"0x1", "0x2", "0xABCD", "0x50"}
122124
for i := range devs {
123125
d := &mocks.PciNetDevice{}
124126
d.On("GetVendor").Return(vendors[i]).
@@ -130,7 +132,8 @@ var _ = Describe("Factory", func() {
130132
On("GetPfPciAddr").Return(rootDevices[i]).
131133
On("GetAPIDevice").Return(&pluginapi.Device{}).
132134
On("GetLinkType").Return(linkTypes[i]).
133-
On("GetDDPProfiles").Return(ddpProfiles[i])
135+
On("GetDDPProfiles").Return(ddpProfiles[i]).
136+
On("GetPKey").Return(pKeys[i])
134137
devs[i] = d
135138
}
136139

@@ -145,7 +148,8 @@ var _ = Describe("Factory", func() {
145148
"pfNames": ["enp2s0f2"],
146149
"rootDevices": ["0000:86:00.0"],
147150
"linkTypes": ["ether"],
148-
"ddpProfiles": ["GTP"]
151+
"ddpProfiles": ["GTP"],
152+
"pKeys": ["0x1"]
149153
}
150154
]`),
151155
)
@@ -197,6 +201,7 @@ var _ = Describe("Factory", func() {
197201
rootDevices := []string{"0000:86:00.0", "0000:86:00.1", "0000:86:00.2", "0000:86:00.3"}
198202
linkTypes := []string{"ether", "infiniband", "other", "other2"}
199203
ddpProfiles := []string{"GTP", "PPPoE", "GTP", "PPPoE"}
204+
pKeys := []string{"0x1", "0x2", "0xABCD", "0x50"}
200205
for i := range devs {
201206
d := &mocks.PciNetDevice{}
202207
d.On("GetVendor").Return(vendors[i]).
@@ -209,7 +214,8 @@ var _ = Describe("Factory", func() {
209214
On("GetAPIDevice").Return(&pluginapi.Device{}).
210215
On("GetLinkType").Return(linkTypes[i]).
211216
On("GetDDPProfiles").Return(ddpProfiles[i]).
212-
On("GetFuncID").Return(-1)
217+
On("GetFuncID").Return(-1).
218+
On("GetPKey").Return(pKeys[i])
213219
devs[i] = d
214220
}
215221

@@ -254,7 +260,8 @@ var _ = Describe("Factory", func() {
254260
"pfNames": ["enp2s0f2"],
255261
"rootDevices": ["0000:86:00.0"],
256262
"linkTypes": ["ether"],
257-
"ddpProfiles": ["GTP"]
263+
"ddpProfiles": ["GTP"],
264+
"pKeys": ["0x1"]
258265
}`), []string{"0000:03:02.0"}),
259266
Entry("with a slice of one selector object it should match devices", []byte(`
260267
[{
@@ -265,7 +272,8 @@ var _ = Describe("Factory", func() {
265272
"pfNames": ["enp2s0f2"],
266273
"rootDevices": ["0000:86:00.0"],
267274
"linkTypes": ["ether"],
268-
"ddpProfiles": ["GTP"]
275+
"ddpProfiles": ["GTP"],
276+
"pKeys": ["0x1"]
269277
}]`), []string{"0000:03:02.0"}),
270278
Entry("with more than one selector object, it should match devices from all selector objects", []byte(`
271279
[{
@@ -276,7 +284,8 @@ var _ = Describe("Factory", func() {
276284
"pfNames": ["enp2s0f2"],
277285
"rootDevices": ["0000:86:00.0"],
278286
"linkTypes": ["ether"],
279-
"ddpProfiles": ["GTP"]
287+
"ddpProfiles": ["GTP"],
288+
"pKeys": ["0x1"]
280289
}, {
281290
"vendors": ["8086"],
282291
"devices": ["1111"],
@@ -285,7 +294,8 @@ var _ = Describe("Factory", func() {
285294
"pfNames": ["net2"],
286295
"rootDevices": ["0000:86:00.3"],
287296
"linkTypes": ["other2"],
288-
"ddpProfiles": ["PPPoE"]
297+
"ddpProfiles": ["PPPoE"],
298+
"pKeys": ["0x50"]
289299
}]`), []string{"0000:03:02.0", "0000:03:02.3"}),
290300
)
291301
Describe("getting exclusive resource pool for netdevice", func() {
@@ -308,6 +318,7 @@ var _ = Describe("Factory", func() {
308318
rootDevices := []string{"0000:86:00.0", "0000:86:00.1", "0000:86:00.2", "0000:86:00.3"}
309319
linkTypes := []string{"ether", "infiniband", "other", "other2"}
310320
ddpProfiles := []string{"GTP", "PPPoE", "GTP", "PPPoE"}
321+
pKeys := []string{"0x1", "0x2", "0xABCD", "0x50"}
311322
for i := range devs {
312323
d := &mocks.PciNetDevice{}
313324
d.On("GetVendor").Return(vendors[i]).
@@ -320,7 +331,8 @@ var _ = Describe("Factory", func() {
320331
On("GetAPIDevice").Return(&pluginapi.Device{}).
321332
On("GetLinkType").Return(linkTypes[i]).
322333
On("GetDDPProfiles").Return(ddpProfiles[i]).
323-
On("GetFuncID").Return(-1)
334+
On("GetFuncID").Return(-1).
335+
On("GetPKey").Return(pKeys[i])
324336
devs[i] = d
325337
}
326338

@@ -334,7 +346,8 @@ var _ = Describe("Factory", func() {
334346
"pfNames": ["enp2s0f2"],
335347
"rootDevices": ["0000:86:00.0"],
336348
"linkTypes": ["ether"],
337-
"ddpProfiles": ["GTP"]
349+
"ddpProfiles": ["GTP"],
350+
"pKeys": ["0x1"]
338351
}
339352
`),
340353
)
@@ -350,7 +363,8 @@ var _ = Describe("Factory", func() {
350363
"pfNames": ["enp2s0f2"],
351364
"rootDevices": ["0000:86:00.0"],
352365
"linkTypes": ["ether"],
353-
"ddpProfiles": ["GTP"]
366+
"ddpProfiles": ["GTP"],
367+
"pKeys": ["0x1"]
354368
}]
355369
`),
356370
)

pkg/netdevice/netDeviceProvider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ func (np *netDeviceProvider) GetFilteredDevices(devices []types.HostDevice,
125125
// filter by DDP Profiles list
126126
filteredDevice = rf.FilterBySelector("ddpProfiles", nf.DDPProfiles, filteredDevice)
127127

128+
// filter by PKeys list
129+
filteredDevice = rf.FilterBySelector("pKeys", nf.PKeys, filteredDevice)
130+
128131
// filter for rdma devices
129132
if nf.IsRdma {
130133
rdmaDevices := make([]types.HostDevice, 0)

pkg/netdevice/pciNetDevice.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ type pciNetDevice struct {
3030
devices.GenPciDevice
3131
devices.GenNetDevice
3232
vdpaDev types.VdpaDevice
33+
pKey string
3334
}
3435

3536
// NewPciNetDevice returns an instance of PciNetDevice interface
37+
//
38+
//nolint:gocyclo
3639
func NewPciNetDevice(dev *ghw.PCIDevice,
3740
rFactory types.ResourceFactory, rc *types.ResourceConfig, selectorIndex int) (types.PciNetDevice, error) {
3841
var vdpaDev types.VdpaDevice
@@ -95,11 +98,21 @@ func NewPciNetDevice(dev *ghw.PCIDevice,
9598
return nil, err
9699
}
97100

101+
pKey := ""
102+
if netDev.GetLinkType() == "infiniband" {
103+
pciAddr := pciDev.GetPciAddr()
104+
pKey, err = utils.GetPKey(pciAddr)
105+
if err != nil {
106+
glog.Infof("getPKey(): unable to get PKey for device %s : %q", pciAddr, err)
107+
}
108+
}
109+
98110
return &pciNetDevice{
99111
HostDevice: hostDev,
100112
GenPciDevice: *pciDev,
101113
GenNetDevice: *netDev,
102114
vdpaDev: vdpaDev,
115+
pKey: pKey,
103116
}, nil
104117
}
105118

@@ -116,3 +129,7 @@ func (nd *pciNetDevice) GetDDPProfiles() string {
116129
func (nd *pciNetDevice) GetVdpaDevice() types.VdpaDevice {
117130
return nd.vdpaDev
118131
}
132+
133+
func (nd *pciNetDevice) GetPKey() string {
134+
return nd.pKey
135+
}

pkg/resources/pKeySelector.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package resources
2+
3+
import (
4+
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types"
5+
)
6+
7+
type pKeySelector struct {
8+
pKeys []string
9+
}
10+
11+
// NewPKeySelector returns a DeviceSelector interface to filter devices based on available PKeys
12+
func NewPKeySelector(pKeys []string) types.DeviceSelector {
13+
return &pKeySelector{pKeys: pKeys}
14+
}
15+
16+
func (ds *pKeySelector) Filter(inDevices []types.HostDevice) []types.HostDevice {
17+
filteredList := make([]types.HostDevice, 0)
18+
19+
for _, dev := range inDevices {
20+
pKey := dev.(types.PciNetDevice).GetPKey()
21+
if pKey != "" && contains(ds.pKeys, pKey) {
22+
filteredList = append(filteredList, dev)
23+
}
24+
}
25+
26+
return filteredList
27+
}

pkg/resources/pKeySelector_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package resources_test
2+
3+
import (
4+
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/resources"
5+
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types"
6+
"github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/types/mocks"
7+
8+
. "github.com/onsi/ginkgo"
9+
. "github.com/onsi/gomega"
10+
)
11+
12+
var _ = Describe("PKeySelector", func() {
13+
Describe("PKey selector", func() {
14+
Context("filtering", func() {
15+
It("should return devices matching given PKeys", func() {
16+
pKeys := []string{"0x1", "0x2"}
17+
sel := resources.NewPKeySelector(pKeys)
18+
19+
dev0 := mocks.PciNetDevice{}
20+
dev0.On("GetPKey").Return("0x1")
21+
22+
dev1 := mocks.PciNetDevice{}
23+
dev1.On("GetPKey").Return("0x2")
24+
25+
dev2 := mocks.PciNetDevice{}
26+
dev2.On("GetPKey").Return("0x3")
27+
28+
in := []types.HostDevice{&dev0, &dev1, &dev2}
29+
filtered := sel.Filter(in)
30+
31+
Expect(filtered).To(ContainElement(&dev0))
32+
Expect(filtered).To(ContainElement(&dev1))
33+
Expect(filtered).NotTo(ContainElement(&dev2))
34+
})
35+
})
36+
})
37+
})

pkg/types/mocks/PciNetDevice.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)