From a03cbf1df14e0796a0aee9c4b45b226e533d0663 Mon Sep 17 00:00:00 2001 From: Jack Yu Date: Mon, 9 Sep 2024 10:04:45 +0800 Subject: [PATCH] fix: userName should be in spec instead of status Signed-off-by: Jack Yu --- charts/templates/crds.yaml | 22 ++++++++----- .../v1beta1/usb_device_claim.go | 6 +++- .../v1beta1/zz_generated_deepcopy.go | 17 ++++++++++ pkg/crd/crd.go | 2 +- pkg/util/gousb/usbid/load_data.go | 32 +++++++++++++------ pkg/webhook/usbdeviceclaim.go | 14 ++++++++ pkg/webhook/usbdeviceclaim_test.go | 15 +++++++++ 7 files changed, 89 insertions(+), 19 deletions(-) diff --git a/charts/templates/crds.yaml b/charts/templates/crds.yaml index c1c46dc0..241a9aef 100644 --- a/charts/templates/crds.yaml +++ b/charts/templates/crds.yaml @@ -457,13 +457,19 @@ spec: - jsonPath: .status.pciAddress name: PCI Address type: string - - jsonPath: .status.userName + - jsonPath: .spec.userName name: User Name type: string name: v1beta1 schema: openAPIV3Schema: properties: + spec: + properties: + userName: + nullable: true + type: string + type: object status: properties: nodeName: @@ -472,9 +478,6 @@ spec: pciAddress: nullable: true type: string - userName: - nullable: true - type: string type: object type: object served: true @@ -940,7 +943,7 @@ spec: - JSONPath: .status.pciAddress name: PCI Address type: string - - JSONPath: .status.userName + - JSONPath: .spec.userName name: User Name type: string group: devices.harvesterhci.io @@ -955,6 +958,12 @@ spec: validation: openAPIV3Schema: properties: + spec: + properties: + userName: + nullable: true + type: string + type: object status: properties: nodeName: @@ -963,9 +972,6 @@ spec: pciAddress: nullable: true type: string - userName: - nullable: true - type: string type: object type: object version: v1beta1 diff --git a/pkg/apis/devices.harvesterhci.io/v1beta1/usb_device_claim.go b/pkg/apis/devices.harvesterhci.io/v1beta1/usb_device_claim.go index 97ad2b04..3616c5be 100644 --- a/pkg/apis/devices.harvesterhci.io/v1beta1/usb_device_claim.go +++ b/pkg/apis/devices.harvesterhci.io/v1beta1/usb_device_claim.go @@ -11,13 +11,17 @@ type USBDeviceClaim struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` + Spec USBDeviceClaimSpec `json:"spec,omitempty"` Status USBDeviceClaimStatus `json:"status,omitempty"` } +type USBDeviceClaimSpec struct { + UserName string `json:"userName"` +} + type USBDeviceClaimStatus struct { NodeName string `json:"nodeName"` PCIAddress string `json:"pciAddress"` - UserName string `json:"userName"` } const ( diff --git a/pkg/apis/devices.harvesterhci.io/v1beta1/zz_generated_deepcopy.go b/pkg/apis/devices.harvesterhci.io/v1beta1/zz_generated_deepcopy.go index 0b3ca623..a78b7148 100644 --- a/pkg/apis/devices.harvesterhci.io/v1beta1/zz_generated_deepcopy.go +++ b/pkg/apis/devices.harvesterhci.io/v1beta1/zz_generated_deepcopy.go @@ -525,6 +525,7 @@ func (in *USBDeviceClaim) DeepCopyInto(out *USBDeviceClaim) { *out = *in out.TypeMeta = in.TypeMeta in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + out.Spec = in.Spec out.Status = in.Status return } @@ -580,6 +581,22 @@ func (in *USBDeviceClaimList) DeepCopyObject() runtime.Object { return nil } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *USBDeviceClaimSpec) DeepCopyInto(out *USBDeviceClaimSpec) { + *out = *in + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new USBDeviceClaimSpec. +func (in *USBDeviceClaimSpec) DeepCopy() *USBDeviceClaimSpec { + if in == nil { + return nil + } + out := new(USBDeviceClaimSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *USBDeviceClaimStatus) DeepCopyInto(out *USBDeviceClaimStatus) { *out = *in diff --git a/pkg/crd/crd.go b/pkg/crd/crd.go index 6ebc22d3..750f146b 100644 --- a/pkg/crd/crd.go +++ b/pkg/crd/crd.go @@ -142,7 +142,7 @@ func List() []crd.CRD { return c. WithColumn("Node Name", ".status.nodeName"). WithColumn("PCI Address", ".status.pciAddress"). - WithColumn("User Name", ".status.userName") + WithColumn("User Name", ".spec.userName") }), } } diff --git a/pkg/util/gousb/usbid/load_data.go b/pkg/util/gousb/usbid/load_data.go index d6cf6147..2b9a42e8 100644 --- a/pkg/util/gousb/usbid/load_data.go +++ b/pkg/util/gousb/usbid/load_data.go @@ -27,8 +27,8 @@ import "time" // // The baked-in data was last generated: // -// 2024-07-02 11:16:54.773304 +0800 CST m=+3.301417917 -var LastUpdate = time.Unix(0, 1719890214773304000) +// 2024-09-09 09:38:25.925192 +0800 CST m=+1.282289042 +var LastUpdate = time.Unix(0, 1725845905925192000) const usbIDListData = `# # List of USB ID's @@ -41,8 +41,8 @@ const usbIDListData = `# # The latest version can be obtained from # http://www.linux-usb.org/usb.ids # -# Version: 2024.03.18 -# Date: 2024-03-18 20:34:02 +# Version: 2024.07.04 +# Date: 2024-07-04 20:34:02 # # Vendors, devices and interfaces. Please keep sorted. @@ -2432,6 +2432,7 @@ const usbIDListData = `# 02e3 Xbox One Elite Controller 02e6 Xbox Wireless Adapter for Windows 02ea Xbox One Controller + 02f3 Xbox One Chatpad 02fd Xbox One S Controller [Bluetooth] 02fe Xbox Wireless Adapter for Windows 0306 Surface Pro 7 SD Card Reader @@ -5024,7 +5025,7 @@ const usbIDListData = `# 0a28 INDI AV-IN Device 1301 Network Controller 1302 i3 Gateway - 1303 3 Micro Module + 1303 i3 Micro Module 1304 i3 Module 1305 i3 Multi Sensing Module 04c1 U.S. Robotics (3Com) @@ -6465,6 +6466,7 @@ const usbIDListData = `# 2060 PT-E550W P-touch Label Printer 2061 PT-P700 P-touch Label Printer 2064 PT-P700 P-touch Label Printer RemovableDisk + 2065 PT-P750W P-Touch Label Writer 2074 PT-D600 P-touch Label Printer 209b QL-800 Label Printer 209c QL-810W Label Printer @@ -7471,6 +7473,7 @@ const usbIDListData = `# 03dd PTH-460 [Intuos Pro BT (S)] tablet 03ec DTH134 [DTH134] touchscreen 03ed DTC121 [DTC121] touchscreen + 03f0 DTH135 [Movink 13] 0400 PenPartner 4x5 4001 TPC4001 4004 TPC4004 @@ -9081,6 +9084,7 @@ const usbIDListData = `# 0752 micros Reader 0760 USB 2.0 Card Reader/Writer 0761 Genesys Mass Storage Device + 0769 SPR2801S [Lightspeeur 2801] 0780 USBFS DFU Adapter 07a0 Pen Flash 0880 Wasp (SL-6612) @@ -10930,7 +10934,7 @@ const usbIDListData = `# 0056 Agfa AP1100 Photo Printer 005d Mobile Mass Storage 005f Laser Pro LL [MFPrinter] - 0062 XG-76NA 802.11bg + 0062 XG-76NA / XG-760N 802.11b/g Wireless adapter 0078 Laser Pro Monochrome MFP 079d Alfadata Computer Corp. 0201 GamePort Adapter @@ -11201,6 +11205,7 @@ const usbIDListData = `# 1228 MPEG-2 Capture Device (M038) 1830 AVerTV Volar Video Capture (H830) 1871 TD310 DVB-T/T2/C dongle + 2553 Live Gamer Ultra 2.1 3835 AVerTV Volar Green HD (A835B) 850a AverTV Volar Black HD (A850) 850b AverTV Red HD+ (A850T) @@ -11212,6 +11217,7 @@ const usbIDListData = `# b300 A300 DVB-T TV receiver b800 MR800 FM Radio c039 DVD EZMaker 7 + d553 Live Gamer Ultra Pro-RGB e880 MPEG-2 Capture Device (E880) e882 MPEG-2 Capture Device (E882) 07cb Kingmax Technology, Inc. @@ -12318,7 +12324,7 @@ const usbIDListData = `# 0a0b WLU5053 802.11abgn Wireless Module [Broadcom BCM43236B] 0a13 AX88179 Gigabit Ethernet [Toshiba] 0b05 PX1220E-1G25 External hard drive - 0b09 PX1396E-3T01 External hard drive + 0b09 PX139xE 3.5 External HDD 0b1a STOR.E ALU 2S 1300 Wireless Broadband (CDMA EV-DO) SM-Bus Minicard Status Port 1301 Wireless Broadband (CDMA EV-DO) Minicard Status Port @@ -12452,6 +12458,7 @@ const usbIDListData = `# 010f nanoKONTROL studio controller 0117 nanoKONTROL2 MIDI Controller 012f SQ-1 + 0154 NTS-1 digital kit mkII 0203 KRONOS 0f03 K-Series K61P MIDI studio controller 0945 Pasco Scientific @@ -13036,6 +13043,7 @@ const usbIDListData = `# 5803 BCM5880 Secure Applications Processor with secure keyboard 5804 BCM5880 Secure Applications Processor with fingerprint swipe sensor 5832 BCM5880 Secure Applications Processor Smartcard reader + 5843 BCM58200 ControlVault 3 (FingerPrint sensor + Contacted SmartCard) 6300 Pirelli Remote NDIS Device 6410 BCM20703A1 Bluetooth 4.1 + LE bd11 BCM4320 802.11bg Wireless Adapter @@ -13052,11 +13060,14 @@ const usbIDListData = `# 0009 LP2844 Printer 0027 ZTC LP2844-Z-200dpi 0050 P120i / WM120i + 0062 GK420d Label Printer + 0065 ZM400 Label Printer 0080 GK420d Label Printer 0081 GK420t Label Printer 0084 GX420d Desktop Label Printer 008b HC100 wristbands Printer 008c ZP 450 Printer + 00a1 TLP2824 Plus 00d1 GC420d Label Printer 0110 ZD500 Desktop Label Printer 011c ZD410 Direct Thermal Label Printer @@ -13067,6 +13078,7 @@ const usbIDListData = `# 0010 MPMan MP-F40 MP3 Player 0a66 ClearCube Technology 0a67 Medeli Electronics Co., Ltd + ffff LCS Audio 0a68 Comaide Corp. 0a69 Chroma ate, Inc. 0a6b Green House Co., Ltd @@ -13278,10 +13290,11 @@ const usbIDListData = `# 0ac9 Micro Solutions, Inc. 0000 Backpack CD-ReWriter 0001 BACKPACK 2 Cable - 0010 BACKPACK + 0010 BACKPACK CD Drive 0011 Backpack 40GB Hard Drive 0110 BACKPACK 0111 BackPack + 10ff BACKPACK 1234 BACKPACK 0aca OPEN Networks Ltd 1060 OPEN NT1 Plus II @@ -13340,7 +13353,7 @@ const usbIDListData = `# 3102 MemoryStick Card Reader 3201 MMC/SD+MemoryStick Card Reader 3216 HS Card Reader - 3260 7-in-1 Card Reader + 3260 ND3260 7-in-1 Card Reader 5010 ND5010 Card Reader 0af0 Option 5000 UMTS Card @@ -13414,6 +13427,7 @@ const usbIDListData = `# 17a0 Xonar U3 sound card 17a1 Eee Note EA800 (mass storage mode) 17ab USB-N13 802.11n Network Adapter (rev. B1) [Realtek RTL8192CU] + 17b5 Broadcom BCM20702A0 Bluetooth 17ba N10 Nano 802.11n Network Adapter [Realtek RTL8192CU] 17c2 ROG Spitfire 17c7 WL-330NUL diff --git a/pkg/webhook/usbdeviceclaim.go b/pkg/webhook/usbdeviceclaim.go index 61ccb362..97c0d9ac 100644 --- a/pkg/webhook/usbdeviceclaim.go +++ b/pkg/webhook/usbdeviceclaim.go @@ -26,6 +26,7 @@ func (udc *usbDeviceClaimValidator) Resource() types.Resource { APIVersion: devicesv1beta1.SchemeGroupVersion.Version, ObjectType: &devicesv1beta1.USBDeviceClaim{}, OperationTypes: []admissionregv1.OperationType{ + admissionregv1.Update, admissionregv1.Delete, }, } @@ -51,3 +52,16 @@ func (udc *usbDeviceClaimValidator) Delete(_ *types.Request, oldObj runtime.Obje return nil } + +func (udc *usbDeviceClaimValidator) Update(_ *types.Request, oldObj runtime.Object, newObj runtime.Object) error { + oldUsbClaimObj := oldObj.(*devicesv1beta1.USBDeviceClaim) + newUsbClaimObj := newObj.(*devicesv1beta1.USBDeviceClaim) + + if oldUsbClaimObj.Spec.UserName != newUsbClaimObj.Spec.UserName { + err := fmt.Errorf("usbdeviceclaim %s username is immutable", newUsbClaimObj.Name) + logrus.Errorf(err.Error()) + return err + } + + return nil +} diff --git a/pkg/webhook/usbdeviceclaim_test.go b/pkg/webhook/usbdeviceclaim_test.go index 8fd65cbe..e60922d4 100644 --- a/pkg/webhook/usbdeviceclaim_test.go +++ b/pkg/webhook/usbdeviceclaim_test.go @@ -56,6 +56,21 @@ var ( } ) +func Test_UploadUSBDeviceClaimNotInUse(t *testing.T) { + assert := require.New(t) + harvesterfakeClient := harvesterfake.NewSimpleClientset() + vmCache := fakeclients.VirtualMachineCache(harvesterfakeClient.KubevirtV1().VirtualMachines) + usbValidator := NewUSBDeviceClaimValidator(vmCache) + old := usbdeviceclaim1.DeepCopy() + old.Spec.UserName = "admin" + newOne := usbdeviceclaim1.DeepCopy() + newOne.Spec.UserName = "admin2" + + err := usbValidator.Update(nil, old, newOne) + + assert.Error(err, "expected error when updating the userName") +} + func Test_DeleteUSBDeviceClaimInUse(t *testing.T) { assert := require.New(t) harvesterfakeClient := harvesterfake.NewSimpleClientset(vmWithValidUSBDeviceName)