Skip to content

Commit

Permalink
fix: use cache and change crd spec
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 Jun 3, 2024
1 parent 5a3a3a5 commit f69845f
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 24 deletions.
18 changes: 12 additions & 6 deletions charts/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ spec:
- jsonPath: .status.pciAddress
name: PCI Address
type: string
- jsonPath: .status.userName
name: User Name
type: string
name: v1beta1
schema:
openAPIV3Schema:
Expand All @@ -465,15 +468,15 @@ spec:
type: object
status:
properties:
claimedBy:
nullable: true
type: string
nodeName:
nullable: true
type: string
pciAddress:
nullable: true
type: string
userName:
nullable: true
type: string
type: object
type: object
served: true
Expand Down Expand Up @@ -939,6 +942,9 @@ spec:
- JSONPath: .status.pciAddress
name: PCI Address
type: string
- JSONPath: .status.userName
name: User Name
type: string
group: devices.harvesterhci.io
names:
kind: USBDeviceClaim
Expand All @@ -955,15 +961,15 @@ spec:
type: object
status:
properties:
claimedBy:
nullable: true
type: string
nodeName:
nullable: true
type: string
pciAddress:
nullable: true
type: string
userName:
nullable: true
type: string
type: object
type: object
version: v1beta1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ type USBDeviceClaimSpec struct {
type USBDeviceClaimStatus struct {
NodeName string `json:"nodeName"`
PCIAddress string `json:"pciAddress"`
ClaimedBy string `json:"claimedBy"`
UserName string `json:"userName"`
}
2 changes: 1 addition & 1 deletion pkg/controller/nodes/node_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (h *handler) reconcileNodeDevices(name string, node *v1beta1.Node) (*v1beta
return nil, fmt.Errorf("error reconciling pcidevices for node %s: %v", h.nodeName, err)
}

usbHandler := usbdevice.NewHandler(h.usbCtl, h.usbClaimCtl)
usbHandler := usbdevice.NewHandler(h.usbCtl, h.usbClaimCtl, h.usbCtl.Cache(), h.usbClaimCtl.Cache())
if err := usbHandler.ReconcileUSBDevices(); err != nil {
return nil, fmt.Errorf("error reconciling usb devices for node %s: %v", h.nodeName, err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/usbdevice/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Register(ctx context.Context, management *config.FactoryManager) error {
usbDeviceClaimCtrl := management.DeviceFactory.Devices().V1beta1().USBDeviceClaim()
virtClient := management.KubevirtFactory.Kubevirt().V1().KubeVirt()

handler := NewHandler(usbDeviceCtrl, usbDeviceClaimCtrl)
handler := NewHandler(usbDeviceCtrl, usbDeviceClaimCtrl, usbDeviceCtrl.Cache(), usbDeviceClaimCtrl.Cache())
usbDeviceClaimController := NewClaimHandler(usbDeviceCtrl.Cache(), usbDeviceClaimCtrl, usbDeviceCtrl, virtClient, deviceplugins.NewUSBDevicePlugin)

usbDeviceClaimCtrl.OnChange(ctx, "usbClaimClient-device-claim", usbDeviceClaimController.OnUSBDeviceClaimChanged)
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/usbdevice/usbdevice_claim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewClaimHandler(
}

func (h *DevClaimHandler) OnUSBDeviceClaimChanged(_ string, usbDeviceClaim *v1beta1.USBDeviceClaim) (*v1beta1.USBDeviceClaim, error) {
if usbDeviceClaim == nil {
if usbDeviceClaim == nil || usbDeviceClaim.DeletionTimestamp == nil {
return usbDeviceClaim, nil
}

Expand Down Expand Up @@ -174,7 +174,7 @@ func (h *DevClaimHandler) findDevicePlugin(pluginDevices map[string][]*deviceplu
}

func (h *DevClaimHandler) OnRemove(_ string, claim *v1beta1.USBDeviceClaim) (*v1beta1.USBDeviceClaim, error) {
if claim == nil {
if claim == nil || claim.DeletionTimestamp == nil {
return claim, nil
}

Expand Down
27 changes: 19 additions & 8 deletions pkg/controller/usbdevice/usbdevice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/rancher/wrangler/pkg/relatedresource"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"

"github.com/harvester/pcidevices/pkg/apis/devices.harvesterhci.io/v1beta1"
Expand All @@ -19,6 +20,8 @@ import (
type DevHandler struct {
usbClient ctldevicerv1vbeta1.USBDeviceClient
usbClaimClient ctldevicerv1vbeta1.USBDeviceClaimClient
usbCache ctldevicerv1vbeta1.USBDeviceCache
usbClaimCache ctldevicerv1vbeta1.USBDeviceClaimCache
}

var walkUSBDevices = deviceplugins.WalkUSBDevices
Expand All @@ -39,10 +42,17 @@ func (dev *USBDevice) GetID() string {
return fmt.Sprintf("%04x:%04x-%02d:%02d", dev.Vendor, dev.Product, dev.Bus, dev.DeviceNumber)
}

func NewHandler(usbClient ctldevicerv1vbeta1.USBDeviceClient, usbClaimClient ctldevicerv1vbeta1.USBDeviceClaimClient) *DevHandler {
func NewHandler(
usbClient ctldevicerv1vbeta1.USBDeviceClient,
usbClaimClient ctldevicerv1vbeta1.USBDeviceClaimClient,
usbCache ctldevicerv1vbeta1.USBDeviceCache,
usbClaimCache ctldevicerv1vbeta1.USBDeviceClaimCache,
) *DevHandler {
return &DevHandler{
usbClient: usbClient,
usbClaimClient: usbClaimClient,
usbCache: usbCache,
usbClaimCache: usbClaimCache,
}
}

Expand All @@ -59,14 +69,14 @@ func (h *DevHandler) OnDeviceChange(_ string, _ string, obj runtime.Object) ([]r
}

if ud.Status.NodeName == cl.nodeName {
udcList, err := h.usbClaimClient.List(metav1.ListOptions{LabelSelector: cl.selector()})
udcList, err := h.usbClaimCache.List(labels.SelectorFromSet(cl.labels()))
if err != nil {
logrus.Errorf("error listing USBDeviceClaims during device watch: %v", err)
return nil, err
}

var rr []relatedresource.Key
for _, v := range udcList.Items {
for _, v := range udcList {
rr = append(rr, relatedresource.NewKey(v.Namespace, v.Name))
}
return rr, nil
Expand All @@ -84,14 +94,15 @@ func (h *DevHandler) ReconcileUSBDevices() error {
return err
}

storedUSBDevices, err := h.usbClient.List(metav1.ListOptions{})
storedUSBDevices, err := h.usbCache.List(labels.SelectorFromSet(cl.labels()))
if err != nil {
logrus.Errorf("failed to list USB devices: %v\n", err)
return err
}

mapStoredUSBDevices := make(map[string]v1beta1.USBDevice)
for _, storedUSBDevice := range storedUSBDevices.Items {
mapStoredUSBDevices := make(map[string]*v1beta1.USBDevice)
for _, storedUSBDevice := range storedUSBDevices {
storedUSBDevice := storedUSBDevice
mapStoredUSBDevices[storedUSBDevice.Status.DevicePath] = storedUSBDevice
}

Expand All @@ -105,7 +116,7 @@ func (h *DevHandler) ReconcileUSBDevices() error {
return nil
}

func (h *DevHandler) handleList(createList []*v1beta1.USBDevice, updateList []*v1beta1.USBDevice, mapStoredUSBDevices map[string]v1beta1.USBDevice) error {
func (h *DevHandler) handleList(createList []*v1beta1.USBDevice, updateList []*v1beta1.USBDevice, mapStoredUSBDevices map[string]*v1beta1.USBDevice) error {
for _, usbDevice := range createList {
createdOne := &v1beta1.USBDevice{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -150,7 +161,7 @@ func (h *DevHandler) handleList(createList []*v1beta1.USBDevice, updateList []*v
return nil
}

func (h *DevHandler) getList(localUSBDevices map[int][]*deviceplugins.USBDevice, mapStoredUSBDevices map[string]v1beta1.USBDevice, nodeName string) ([]*v1beta1.USBDevice, []*v1beta1.USBDevice) {
func (h *DevHandler) 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
2 changes: 2 additions & 0 deletions pkg/controller/usbdevice/usbdevice_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func Test_ReconcileUSBDevices(t *testing.T) {
usbHandler := NewHandler(
fakeclients.USBDevicesClient(client.DevicesV1beta1().USBDevices),
fakeclients.USBDeviceClaimsClient(client.DevicesV1beta1().USBDeviceClaims),
fakeclients.USBDeviceCache(client.DevicesV1beta1().USBDevices),
fakeclients.USBDeviceClaimsCache(client.DevicesV1beta1().USBDeviceClaims),
)

err := usbHandler.ReconcileUSBDevices()
Expand Down
3 changes: 2 additions & 1 deletion pkg/crd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ func List() []crd.CRD {
c.NonNamespace = true
return c.
WithColumn("Node Name", ".status.nodeName").
WithColumn("PCI Address", ".status.pciAddress")
WithColumn("PCI Address", ".status.pciAddress").
WithColumn("User Name", ".status.userName")
}),
}
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/util/fakeclients/usbdevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,23 @@ func (p USBDeviceCache) Get(name string) (*devicev1beta1.USBDevice, error) {
return p().Get(context.TODO(), name, metav1.GetOptions{})
}

func (p USBDeviceCache) List(labels.Selector) ([]*devicev1beta1.USBDevice, error) {
panic("implement me")
func (p USBDeviceCache) List(selector labels.Selector) ([]*devicev1beta1.USBDevice, error) {
devices, err := p().List(context.TODO(), metav1.ListOptions{
LabelSelector: selector.String(),
})

if err != nil {
return nil, err
}

result := make([]*devicev1beta1.USBDevice, 0, len(devices.Items))

for _, device := range devices.Items {
obj := device
result = append(result, &obj)
}

return result, nil
}

func (p USBDeviceCache) AddIndexer(_ string, _ devicesv1beta1ctl.USBDeviceIndexer) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/gousb/usbid/load_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import "time"
//
// The baked-in data was last generated:
//
// 2024-05-24 18:06:00.452809 +0800 CST m=+1.712571876
var LastUpdate = time.Unix(0, 1716545160452809000)
// 2024-06-03 14:47:45.299917 +0800 CST m=+1.385160042
var LastUpdate = time.Unix(0, 1717397265299917000)

const usbIDListData = `#
# List of USB ID's
Expand Down

0 comments on commit f69845f

Please sign in to comment.