[KEP-4817] Use structured.MakeDeviceID

Signed-off-by: Lionel Jouin <lionel.jouin@est.tech>
This commit is contained in:
Lionel Jouin 2024-11-06 10:49:37 +01:00
parent 8ab33b8413
commit 43d23b8994
3 changed files with 10 additions and 14 deletions

View File

@ -130,7 +130,7 @@ func gatherRequestNames(deviceClaim *resource.DeviceClaim) sets.Set[string] {
func gatherAllocatedDevices(allocationResult *resource.DeviceAllocationResult) sets.Set[structured.DeviceID] {
allocatedDevices := sets.New[structured.DeviceID]()
for _, result := range allocationResult.Results {
deviceID := structured.DeviceID{Driver: result.Driver, Pool: result.Pool, Device: result.Device}
deviceID := structured.MakeDeviceID(result.Driver, result.Pool, result.Device)
allocatedDevices.Insert(deviceID)
}
return allocatedDevices
@ -276,7 +276,7 @@ func validateResourceClaimStatusUpdate(status, oldStatus *resource.ResourceClaim
return validateDeviceStatus(device, fldPath, allocatedDevices)
},
func(device resource.AllocatedDeviceStatus) (structured.DeviceID, string) {
return structured.DeviceID{Driver: device.Driver, Pool: device.Pool, Device: device.Device}, "deviceID"
return structured.MakeDeviceID(device.Driver, device.Pool, device.Device), "deviceID"
},
fldPath.Child("devices"))...)
@ -744,7 +744,7 @@ func validateDeviceStatus(device resource.AllocatedDeviceStatus, fldPath *field.
allErrs = append(allErrs, validateDriverName(device.Driver, fldPath.Child("driver"))...)
allErrs = append(allErrs, validatePoolName(device.Pool, fldPath.Child("pool"))...)
allErrs = append(allErrs, validateDeviceName(device.Device, fldPath.Child("device"))...)
deviceID := structured.DeviceID{Driver: device.Driver, Pool: device.Pool, Device: device.Device}
deviceID := structured.MakeDeviceID(device.Driver, device.Pool, device.Device)
if !allocatedDevices.Has(deviceID) {
allErrs = append(allErrs, field.Invalid(fldPath, deviceID, "must be an allocated device in the claim"))
}

View File

@ -1024,7 +1024,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) {
"invalid-device-status-duplicate": {
wantFailures: field.ErrorList{
field.Duplicate(field.NewPath("status", "devices").Index(0).Child("networkData", "addresses").Index(1), "2001:db8::1/64"),
field.Duplicate(field.NewPath("status", "devices").Index(1).Child("deviceID"), structured.DeviceID{Driver: goodName, Pool: goodName, Device: goodName}),
field.Duplicate(field.NewPath("status", "devices").Index(1).Child("deviceID"), structured.MakeDeviceID(goodName, goodName, goodName)),
},
oldClaim: func() *resource.ResourceClaim { return validAllocatedClaim }(),
update: func(claim *resource.ResourceClaim) *resource.ResourceClaim {
@ -1098,7 +1098,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) {
},
"invalid-device-status-no-device": {
wantFailures: field.ErrorList{
field.Invalid(field.NewPath("status", "devices").Index(0), structured.DeviceID{Driver: "b", Pool: "a", Device: "r"}, "must be an allocated device in the claim"),
field.Invalid(field.NewPath("status", "devices").Index(0), structured.MakeDeviceID("b", "a", "r"), "must be an allocated device in the claim"),
},
oldClaim: func() *resource.ResourceClaim { return validAllocatedClaim }(),
update: func(claim *resource.ResourceClaim) *resource.ResourceClaim {
@ -1116,7 +1116,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) {
"invalid-device-status-duplicate-disabled-feature-gate": {
wantFailures: field.ErrorList{
field.Duplicate(field.NewPath("status", "devices").Index(0).Child("networkData", "addresses").Index(1), "2001:db8::1/64"),
field.Duplicate(field.NewPath("status", "devices").Index(1).Child("deviceID"), structured.DeviceID{Driver: goodName, Pool: goodName, Device: goodName}),
field.Duplicate(field.NewPath("status", "devices").Index(1).Child("deviceID"), structured.MakeDeviceID(goodName, goodName, goodName)),
},
oldClaim: func() *resource.ResourceClaim { return validAllocatedClaim }(),
update: func(claim *resource.ResourceClaim) *resource.ResourceClaim {
@ -1190,7 +1190,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) {
},
"invalid-device-status-no-device-disabled-feature-gate": {
wantFailures: field.ErrorList{
field.Invalid(field.NewPath("status", "devices").Index(0), structured.DeviceID{Driver: "b", Pool: "a", Device: "r"}, "must be an allocated device in the claim"),
field.Invalid(field.NewPath("status", "devices").Index(0), structured.MakeDeviceID("b", "a", "r"), "must be an allocated device in the claim"),
},
oldClaim: func() *resource.ResourceClaim { return validAllocatedClaim }(),
update: func(claim *resource.ResourceClaim) *resource.ResourceClaim {

View File

@ -257,7 +257,7 @@ func dropDeallocatedStatusDevices(newClaim, oldClaim *resource.ResourceClaim) {
if oldClaim.Status.Allocation != nil {
// Get all devices in the oldClaim.
for _, result := range oldClaim.Status.Allocation.Devices.Results {
deviceID := structured.DeviceID{Driver: result.Driver, Pool: result.Pool, Device: result.Device}
deviceID := structured.MakeDeviceID(result.Driver, result.Pool, result.Device)
deallocatedDevices.Insert(deviceID)
}
}
@ -265,7 +265,7 @@ func dropDeallocatedStatusDevices(newClaim, oldClaim *resource.ResourceClaim) {
// Remove devices from deallocatedDevices that are still in newClaim.
if newClaim.Status.Allocation != nil {
for _, result := range newClaim.Status.Allocation.Devices.Results {
deviceID := structured.DeviceID{Driver: result.Driver, Pool: result.Pool, Device: result.Device}
deviceID := structured.MakeDeviceID(result.Driver, result.Pool, result.Device)
deallocatedDevices.Delete(deviceID)
}
}
@ -273,11 +273,7 @@ func dropDeallocatedStatusDevices(newClaim, oldClaim *resource.ResourceClaim) {
// Remove from newClaim.Status.Devices.
n := 0
for _, device := range newClaim.Status.Devices {
deviceID := structured.DeviceID{
Driver: device.Driver,
Pool: device.Pool,
Device: device.Device,
}
deviceID := structured.MakeDeviceID(device.Driver, device.Pool, device.Device)
if !deallocatedDevices.Has(deviceID) {
newClaim.Status.Devices[n] = device
n++