Skip to content

Commit

Permalink
refactor: fix codefactory
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 May 22, 2024
1 parent a1b9beb commit 2fd3e5d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 51 deletions.
101 changes: 58 additions & 43 deletions pkg/controller/usbdevice/usbdevice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (h *Handler) OnDeviceChange(_ string, _ string, obj runtime.Object) ([]rela
func (h *Handler) ReconcileUSBDevices() error {
nodeName := cl.nodeName

err, localUSBDevices := deviceplugins.WalkUSBDevices()
localUSBDevices, err := deviceplugins.WalkUSBDevices()
if err != nil {
logrus.Errorf("failed to walk USB devices: %v\n", err)
return err
Expand All @@ -96,6 +96,62 @@ func (h *Handler) ReconcileUSBDevices() error {
mapStoredUSBDevices[storedUSBDevice.Status.DevicePath] = storedUSBDevice
}

createList, updateList := h.getList(localUSBDevices, mapStoredUSBDevices, nodeName)

err = h.handleList(createList, updateList, mapStoredUSBDevices)
if err != nil {
return err
}

return nil
}

func (h *Handler) handleList(createList []*v1beta1.USBDevice, updateList []*v1beta1.USBDevice, mapStoredUSBDevices map[string]v1beta1.USBDevice) error {
for _, usbDevice := range createList {
createdOne := &v1beta1.USBDevice{
ObjectMeta: metav1.ObjectMeta{
Name: usbDevice.Name,
Labels: usbDevice.Labels,
},
}

newOne, err := h.usbClient.Create(createdOne)
if err != nil {
logrus.Errorf("failed to create USB device: %v\n", err)
return err
}

newOne.Status = usbDevice.Status
if _, err = h.usbClient.UpdateStatus(newOne); err != nil {
logrus.Errorf("failed to update new created USB device status: %v\n", err)
return err
}
}

for _, usbDevice := range updateList {
if _, err := h.usbClient.UpdateStatus(usbDevice); err != nil {
logrus.Errorf("failed to update existed USB device status: %v\n", err)
return err
}
}

// The left devices in mapStoredUSBDevices are not found in localUSBDevices, so we should delete them.
for _, usbDevice := range mapStoredUSBDevices {
if usbDevice.Status.Enabled {
logrus.Warningf("USB device %s is still enabled, but it's not discovered in local usb devices. Please check your node could detect that usb device, skippping delete.\n", usbDevice.Name)
continue
}

if err := h.usbClient.Delete(usbDevice.Name, &metav1.DeleteOptions{}); err != nil {
logrus.Errorf("failed to delete USB device: %v\n", err)
return err
}
}

return nil
}

func (h *Handler) getList(localUSBDevices map[int][]*deviceplugins.USBDevice, mapStoredUSBDevices map[string]v1beta1.USBDevice, nodeName string) ([]*v1beta1.USBDevice, []*v1beta1.USBDevice) {
var (
createList []*v1beta1.USBDevice
updateList []*v1beta1.USBDevice
Expand Down Expand Up @@ -142,48 +198,7 @@ func (h *Handler) ReconcileUSBDevices() error {
}
}

for _, usbDevice := range createList {
createdOne := &v1beta1.USBDevice{
ObjectMeta: metav1.ObjectMeta{
Name: usbDevice.Name,
Labels: usbDevice.Labels,
},
}

newOne, err := h.usbClient.Create(createdOne)
if err != nil {
logrus.Errorf("failed to create USB device: %v\n", err)
return err
}

newOne.Status = usbDevice.Status
if _, err = h.usbClient.UpdateStatus(newOne); err != nil {
logrus.Errorf("failed to update new created USB device status: %v\n", err)
return err
}
}

for _, usbDevice := range updateList {
if _, err := h.usbClient.UpdateStatus(usbDevice); err != nil {
logrus.Errorf("failed to update existed USB device status: %v\n", err)
return err
}
}

// The left devices in mapStoredUSBDevices are not found in localUSBDevices, so we should delete them.
for _, usbDevice := range mapStoredUSBDevices {
if usbDevice.Status.Enabled {
logrus.Warningf("USB device %s is still enabled, but it's not discovered in local usb devices. Please check your node could detect that usb device, skippping delete.\n", usbDevice.Name)
continue
}

if err := h.usbClient.Delete(usbDevice.Name, &metav1.DeleteOptions{}); err != nil {
logrus.Errorf("failed to delete USB device: %v\n", err)
return err
}
}

return nil
return createList, updateList
}

func usbDeviceName(nodeName string, localUSBDevice *deviceplugins.USBDevice) string {
Expand Down
6 changes: 3 additions & 3 deletions pkg/deviceplugins/usb_device_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ func parseSysUeventFile(path string) *USBDevice {
return &u
}

func WalkUSBDevices() (error, map[int][]*USBDevice) {
func WalkUSBDevices() (map[int][]*USBDevice, error) {
usbDevices := make(map[int][]*USBDevice, 0)
err := filepath.Walk("/sys/bus/usb/devices", func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -679,10 +679,10 @@ func WalkUSBDevices() (error, map[int][]*USBDevice) {
})

if err != nil {
return err, nil
return nil, err
}

return nil, usbDevices
return usbDevices, nil
}

func parseUSBSymLinkToPCIAddress(link string) string {
Expand Down
5 changes: 3 additions & 2 deletions pkg/util/gousb/codegen/load_data.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import "time"
// LastUpdate stores the latest time that the library was updated.
//
// The baked-in data was last generated:
// {{.Generated}}
//
// {{.Generated}}
var LastUpdate = time.Unix(0, {{.Generated.UnixNano}})

const usbIDListData = `{{printf "%s" .Data}}`
const usbIDListData = `{{printf "%s" .Data}}`
7 changes: 4 additions & 3 deletions pkg/util/gousb/usbid/load_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import "time"
// LastUpdate stores the latest time that the library was updated.
//
// The baked-in data was last generated:
// 2024-05-22 16:38:38.612281 +0800 CST m=+1.944060751
var LastUpdate = time.Unix(0, 1716367118612281000)
//
// 2024-05-22 17:59:32.168051 +0800 CST m=+1.146710792
var LastUpdate = time.Unix(0, 1716371972168051000)

const usbIDListData = `#
# List of USB ID's
Expand Down Expand Up @@ -25654,4 +25655,4 @@ VT 0400 External Vendor Specific
VT 0401 Composite Video
VT 0402 S-Video
VT 0403 Component Video
`
`

0 comments on commit 2fd3e5d

Please sign in to comment.