Skip to content

Commit

Permalink
refactor: fix codefactor
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Yu <[email protected]>
  • Loading branch information
Yu-Jack committed Oct 9, 2024
1 parent c63833e commit 1cd026b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
6 changes: 2 additions & 4 deletions pkg/controller/usbdevice/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@ const (
)

func Register(ctx context.Context, management *config.FactoryManager) error {
reconcileUSBDevice := make(chan struct{}, 1)

usbDeviceCtrl := management.DeviceFactory.Devices().V1beta1().USBDevice()
usbDeviceClaimCtrl := management.DeviceFactory.Devices().V1beta1().USBDeviceClaim()
virtClient := management.KubevirtFactory.Kubevirt().V1().KubeVirt()

handler := NewHandler(usbDeviceCtrl, usbDeviceClaimCtrl, usbDeviceCtrl.Cache(), usbDeviceClaimCtrl.Cache())
usbDeviceClaimController := NewClaimHandler(usbDeviceCtrl.Cache(), usbDeviceClaimCtrl, usbDeviceCtrl, virtClient, reconcileUSBDevice)
usbDeviceClaimController := NewClaimHandler(usbDeviceCtrl.Cache(), usbDeviceClaimCtrl, usbDeviceCtrl, virtClient, handler.reconcileSignal)

// Initial reconcile
if err := handler.reconcile(); err != nil {
logrus.Errorf("error initial reconcile usb devices: %v", err)
return err
}

if err := handler.WatchUSBDevices(ctx, reconcileUSBDevice); err != nil {
if err := handler.WatchUSBDevices(ctx); err != nil {
logrus.Errorf("error watching usb devices: %v", err)
return err
}
Expand Down
18 changes: 7 additions & 11 deletions pkg/controller/usbdevice/usbdevice_claim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,23 @@ func (h *DevClaimHandler) OnUSBDeviceClaimChanged(_ string, usbDeviceClaim *v1be
devicePlugin = usbDevicePlugin
}

usbDeviceCp := usbDevice.DeepCopy()

if !devicePlugin.IsStarted() {
devicePlugin.StartDevicePlugin()

usbDeviceCp := usbDevice.DeepCopy()
// Reset status and message regardless of the original status when the device plugin is started
usbDeviceCp.Status.Status = ""
usbDeviceCp.Status.Message = ""

if !reflect.DeepEqual(usbDevice.Status, usbDeviceCp.Status) {
if usbDevice, err = h.usbClient.UpdateStatus(usbDeviceCp); err != nil {
logrus.Errorf("failed to enable usb device %s status: %v", usbDeviceCp.Name, err)
return usbDeviceClaim, err
}
}
}

if !usbDevice.Status.Enabled {
usbDeviceCp := usbDevice.DeepCopy()
usbDeviceCp.Status.Enabled = true
if _, err = h.usbClient.UpdateStatus(usbDeviceCp); err != nil {
logrus.Errorf("failed to enable usb device %s status: %v", usbDeviceCp.Name, err)
}

if !reflect.DeepEqual(usbDevice.Status, usbDeviceCp.Status) {
if usbDevice, err = h.usbClient.UpdateStatus(usbDeviceCp); err != nil {
logrus.Errorf("failed to update usb device %s status: %v", usbDeviceCp.Name, err)
return usbDeviceClaim, err
}
}
Expand Down
15 changes: 9 additions & 6 deletions pkg/controller/usbdevice/usbdevice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type DevHandler struct {
usbClaimClient ctldevicerv1vbeta1.USBDeviceClaimClient
usbCache ctldevicerv1vbeta1.USBDeviceCache
usbClaimCache ctldevicerv1vbeta1.USBDeviceClaimCache

reconcileSignal chan struct{}
}

type UsageList struct {
Expand All @@ -44,10 +46,11 @@ func NewHandler(
usbClaimCache ctldevicerv1vbeta1.USBDeviceClaimCache,
) *DevHandler {
return &DevHandler{
usbClient: usbClient,
usbClaimClient: usbClaimClient,
usbCache: usbCache,
usbClaimCache: usbClaimCache,
usbClient: usbClient,
usbClaimClient: usbClaimClient,
usbCache: usbCache,
usbClaimCache: usbClaimCache,
reconcileSignal: make(chan struct{}, 1),
}
}

Expand Down Expand Up @@ -80,7 +83,7 @@ func (h *DevHandler) OnDeviceChange(_ string, _ string, obj runtime.Object) ([]r
return nil, nil
}

func (h *DevHandler) WatchUSBDevices(ctx context.Context, reconcile <-chan struct{}) error {
func (h *DevHandler) WatchUSBDevices(ctx context.Context) error {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return fmt.Errorf("failed to creating a fsnotify watcher: %v", err)
Expand Down Expand Up @@ -108,7 +111,7 @@ func (h *DevHandler) WatchUSBDevices(ctx context.Context, reconcile <-chan struc
select {
case <-ctx.Done():
return
case <-orChan(watcher.Events, reconcile):
case <-orChan(watcher.Events, h.reconcileSignal):
// we need reconcile whatever there is a change in /dev/bus/usb/xxx or reconcile signal is received
if err := h.reconcile(); err != nil {
logrus.Errorf("failed to reconcile USB devices: %v", err)
Expand Down

0 comments on commit 1cd026b

Please sign in to comment.