Skip to content

feat: check usb/pci/gpu resource name #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions pkg/util/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -166,3 +167,27 @@ func generateHostDeviceAllocation(obj *kubevirtv1.VirtualMachine, allocationDeta
}
return nil, nil
}

func USBDeviceByResourceName(obj *v1beta1.USBDevice) ([]string, error) {
return []string{obj.Status.ResourceName}, nil
}

func VGPUDeviceByResourceName(obj *v1beta1.VGPUDevice) ([]string, error) {
return []string{
GeneratevGPUDeviceName(obj.Status.ConfiguredVGPUTypeName),
}, nil
}

func GeneratevGPUDeviceName(deviceName string) string {
deviceName = strings.TrimSpace(deviceName)
deviceName = strings.ToUpper(deviceName)
deviceName = strings.Replace(deviceName, "/", "_", -1)
deviceName = strings.Replace(deviceName, ".", "_", -1)
//deviceName = strings.Replace(deviceName, "-", "_", -1)
reg, _ := regexp.Compile(`\s+`)
deviceName = reg.ReplaceAllString(deviceName, "_")
// Removes any char other than alphanumeric and underscore
reg, _ = regexp.Compile(`^a-zA-Z0-9_-.]+`)
deviceName = reg.ReplaceAllString(deviceName, "")
return fmt.Sprintf("nvidia.com/%s", deviceName)
}
15 changes: 14 additions & 1 deletion pkg/util/fakeclients/pcidevices.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

const (
IommuGroupByNode = "pcidevice.harvesterhci.io/iommu-by-node"
IommuGroupByNode = "pcidevice.harvesterhci.io/iommu-by-node"
PCIDeviceByResourceName = "harvesterhcio.io/pcidevice-by-resource-name"
)

type PCIDevicesClient func() v1beta1.PCIDeviceInterface
Expand Down Expand Up @@ -80,6 +81,18 @@ func (p PCIDevicesCache) GetByIndex(indexName, key string) ([]*pcidevicev1beta1.
}
}
return resp, err
case PCIDeviceByResourceName:
list, err := p().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
var resp []*pcidevicev1beta1.PCIDevice
for i, v := range list.Items {
if key == v.Status.ResourceName {
resp = append(resp, &list.Items[i])
}
}
return resp, nil
default:
return nil, nil
}
Expand Down
23 changes: 21 additions & 2 deletions pkg/util/fakeclients/usbdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
devicesv1beta1ctl "github.com/harvester/pcidevices/pkg/generated/controllers/devices.harvesterhci.io/v1beta1"
)

const USBDeviceByResourceName = "harvesterhci.io/usbdevice-by-resource-name"

type USBDevicesClient func() v1beta1.USBDeviceInterface

func (p USBDevicesClient) Update(d *devicev1beta1.USBDevice) (*devicev1beta1.USBDevice, error) {
Expand Down Expand Up @@ -76,6 +78,23 @@ func (p USBDeviceCache) AddIndexer(_ string, _ devicesv1beta1ctl.USBDeviceIndexe
panic("implement me")
}

func (p USBDeviceCache) GetByIndex(_, _ string) ([]*devicev1beta1.USBDevice, error) {
panic("implement me")
func (p USBDeviceCache) GetByIndex(indexName, name string) ([]*devicev1beta1.USBDevice, error) {
switch indexName {
case USBDeviceByResourceName:
var usbDevices []*devicev1beta1.USBDevice
devices, err := p.List(labels.NewSelector())
if err != nil {
return []*devicev1beta1.USBDevice{}, err
}

for _, device := range devices {
if device.Status.ResourceName == name {
usbDevices = append(usbDevices, device)
}
}
return usbDevices, err
default:
}

return []*devicev1beta1.USBDevice{}, nil
}
22 changes: 20 additions & 2 deletions pkg/util/fakeclients/vgpudevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
pcidevicev1beta1 "github.com/harvester/pcidevices/pkg/apis/devices.harvesterhci.io/v1beta1"
"github.com/harvester/pcidevices/pkg/generated/clientset/versioned/typed/devices.harvesterhci.io/v1beta1"
pcidevicesv1beta1ctl "github.com/harvester/pcidevices/pkg/generated/controllers/devices.harvesterhci.io/v1beta1"
"github.com/harvester/pcidevices/pkg/util/common"
)

const vGPUDeviceByResourceName = "harvesterhci.io/vgpu-device-by-resource-name"

type VGPUDeviceClient func() v1beta1.VGPUDeviceInterface

func (s VGPUDeviceClient) Update(d *pcidevicev1beta1.VGPUDevice) (*pcidevicev1beta1.VGPUDevice, error) {
Expand Down Expand Up @@ -72,6 +75,21 @@ func (s VGPUDeviceCache) AddIndexer(_ string, _ pcidevicesv1beta1ctl.VGPUDeviceI
panic("implement me")
}

func (s VGPUDeviceCache) GetByIndex(_, _ string) ([]*pcidevicev1beta1.VGPUDevice, error) {
panic("implement me")
func (s VGPUDeviceCache) GetByIndex(index, key string) ([]*pcidevicev1beta1.VGPUDevice, error) {
switch index {
case vGPUDeviceByResourceName:
devices, err := s.List(labels.NewSelector())
if err != nil {
return nil, err
}
for _, device := range devices {
if common.GeneratevGPUDeviceName(device.Status.ConfiguredVGPUTypeName) == key {
return []*pcidevicev1beta1.VGPUDevice{device}, nil
}
}
return nil, nil
default:
}

return nil, nil
}
13 changes: 1 addition & 12 deletions pkg/util/gpuhelper/gpuhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strings"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -295,15 +294,5 @@ func evalPhysFn(devicePath string) (string, error) {
}

func GenerateDeviceName(deviceName string) string {
deviceName = strings.TrimSpace(deviceName)
deviceName = strings.ToUpper(deviceName)
deviceName = strings.Replace(deviceName, "/", "_", -1)
deviceName = strings.Replace(deviceName, ".", "_", -1)
//deviceName = strings.Replace(deviceName, "-", "_", -1)
reg, _ := regexp.Compile(`\s+`)
deviceName = reg.ReplaceAllString(deviceName, "_")
// Removes any char other than alphanumeric and underscore
reg, _ = regexp.Compile(`^a-zA-Z0-9_-.]+`)
deviceName = reg.ReplaceAllString(deviceName, "")
return fmt.Sprintf("nvidia.com/%s", deviceName)
return common.GeneratevGPUDeviceName(deviceName)
}
20 changes: 13 additions & 7 deletions pkg/webhook/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
)

const (
VMByName = "harvesterhci.io/vm-by-name"
PCIDeviceByResourceName = "harvesterhcio.io/pcidevice-by-resource-name"
IommuGroupByNode = "pcidevice.harvesterhci.io/iommu-by-node"
USBDeviceByAddress = "pcidevice.harvesterhci.io/usb-device-by-address"
VMByPCIDeviceClaim = "harvesterhci.io/vm-by-pcideviceclaim"
VMByUSBDeviceClaim = "harvesterhci.io/vm-by-usbdeviceclaim"
VMByVGPU = "harvesterhci.io/vm-by-vgpu"
VMByName = "harvesterhci.io/vm-by-name"
PCIDeviceByResourceName = "harvesterhcio.io/pcidevice-by-resource-name"
IommuGroupByNode = "pcidevice.harvesterhci.io/iommu-by-node"
USBDeviceByAddress = "pcidevice.harvesterhci.io/usb-device-by-address"
VMByPCIDeviceClaim = "harvesterhci.io/vm-by-pcideviceclaim"
VMByUSBDeviceClaim = "harvesterhci.io/vm-by-usbdeviceclaim"
VMByVGPU = "harvesterhci.io/vm-by-vgpu"
USBDeviceByResourceName = "harvesterhci.io/usbdevice-by-resource-name"
vGPUDeviceByResourceName = "harvesterhci.io/vgpu-device-by-resource-name"
)

func RegisterIndexers(clients *Clients) {
Expand All @@ -30,8 +32,12 @@ func RegisterIndexers(clients *Clients) {
deviceCache := clients.DeviceFactory.Devices().V1beta1().PCIDevice().Cache()
deviceCache.AddIndexer(PCIDeviceByResourceName, pciDeviceByResourceName)
deviceCache.AddIndexer(IommuGroupByNode, iommuGroupByNodeName)
usbDeviceCache := clients.DeviceFactory.Devices().V1beta1().USBDevice().Cache()
usbDeviceCache.AddIndexer(USBDeviceByResourceName, common.USBDeviceByResourceName)
usbDeviceClaimCache := clients.DeviceFactory.Devices().V1beta1().USBDeviceClaim().Cache()
usbDeviceClaimCache.AddIndexer(USBDeviceByAddress, usbDeviceClaimByAddress)
vgpuCache := clients.DeviceFactory.Devices().V1beta1().VGPUDevice().Cache()
vgpuCache.AddIndexer(vGPUDeviceByResourceName, common.VGPUDeviceByResourceName)
}

func vmByName(obj *kubevirtv1.VirtualMachine) ([]string, error) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/webhook/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ func Mutation(clients *Clients) (http.Handler, []types.Resource, error) {
clients.DeviceFactory.Devices().V1beta1().VGPUDevice().Cache()),
NewPCIVMMutator(clients.DeviceFactory.Devices().V1beta1().PCIDevice().Cache(),
clients.DeviceFactory.Devices().V1beta1().PCIDeviceClaim().Cache(),
clients.DeviceFactory.Devices().V1beta1().PCIDeviceClaim()),
clients.DeviceFactory.Devices().V1beta1().PCIDeviceClaim(),
),
}

router := webhook.NewRouter()
Expand Down
6 changes: 5 additions & 1 deletion pkg/webhook/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ func Validation(clients *Clients) (http.Handler, []types.Resource, error) {
NewVGPUValidator(clients.KubevirtFactory.Kubevirt().V1().VirtualMachine().Cache()),
NewSRIOVGPUValidator(clients.KubevirtFactory.Kubevirt().V1().VirtualMachine().Cache()),
NewUSBDeviceClaimValidator(clients.KubevirtFactory.Kubevirt().V1().VirtualMachine().Cache()),
NewDeviceHostValidation(clients.DeviceFactory.Devices().V1beta1().USBDevice().Cache(), clients.DeviceFactory.Devices().V1beta1().PCIDevice().Cache()),
NewDeviceHostValidation(
clients.DeviceFactory.Devices().V1beta1().USBDevice().Cache(),
clients.DeviceFactory.Devices().V1beta1().PCIDevice().Cache(),
clients.DeviceFactory.Devices().V1beta1().VGPUDevice().Cache(),
),
NewUSBDeviceValidator(),
}

Expand Down
Loading
Loading