Skip to content

Commit

Permalink
feat: check vGPU resource name
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Yu <jack.yu@suse.com>
  • Loading branch information
Yu-Jack committed Dec 20, 2024
1 parent 46a338b commit 272a84c
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 109 deletions.
21 changes: 21 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 @@ -170,3 +171,23 @@ func generateHostDeviceAllocation(obj *kubevirtv1.VirtualMachine, allocationDeta
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)
}
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)
}
19 changes: 11 additions & 8 deletions pkg/webhook/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +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"
USBDeviceByResourceName = "harvesterhci.io/usbdevice-by-resource-name"
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 @@ -35,6 +36,8 @@ func RegisterIndexers(clients *Clients) {
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
1 change: 1 addition & 0 deletions pkg/webhook/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func Validation(clients *Clients) (http.Handler, []types.Resource, error) {
NewDeviceHostValidation(
clients.DeviceFactory.Devices().V1beta1().USBDevice().Cache(),
clients.DeviceFactory.Devices().V1beta1().PCIDevice().Cache(),
clients.DeviceFactory.Devices().V1beta1().VGPUDevice().Cache(),
),
NewUSBDeviceValidator(),
}
Expand Down
Loading

0 comments on commit 272a84c

Please sign in to comment.