Skip to content

Commit

Permalink
fix: userName should be in spec instead of status
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 Sep 9, 2024
1 parent c0733b4 commit b6ed907
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 19 deletions.
22 changes: 14 additions & 8 deletions charts/templates/crds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -472,9 +478,6 @@ spec:
pciAddress:
nullable: true
type: string
userName:
nullable: true
type: string
type: object
type: object
served: true
Expand Down Expand Up @@ -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
Expand All @@ -955,6 +958,12 @@ spec:
validation:
openAPIV3Schema:
properties:
spec:
properties:
userName:
nullable: true
type: string
type: object
status:
properties:
nodeName:
Expand All @@ -963,9 +972,6 @@ spec:
pciAddress:
nullable: true
type: string
userName:
nullable: true
type: string
type: object
type: object
version: v1beta1
Expand Down
6 changes: 5 additions & 1 deletion pkg/apis/devices.harvesterhci.io/v1beta1/usb_device_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
17 changes: 17 additions & 0 deletions pkg/apis/devices.harvesterhci.io/v1beta1/zz_generated_deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/crd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}),
}
}
Expand Down
32 changes: 23 additions & 9 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-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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions pkg/webhook/usbdeviceclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (udc *usbDeviceClaimValidator) Resource() types.Resource {
APIVersion: devicesv1beta1.SchemeGroupVersion.Version,
ObjectType: &devicesv1beta1.USBDeviceClaim{},
OperationTypes: []admissionregv1.OperationType{
admissionregv1.Update,
admissionregv1.Delete,
},
}
Expand All @@ -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
}
15 changes: 15 additions & 0 deletions pkg/webhook/usbdeviceclaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b6ed907

Please sign in to comment.