Skip to content

Commit 43d23b8

Browse files
committed
[KEP-4817] Use structured.MakeDeviceID
Signed-off-by: Lionel Jouin <[email protected]>
1 parent 8ab33b8 commit 43d23b8

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

pkg/apis/resource/validation/validation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func gatherRequestNames(deviceClaim *resource.DeviceClaim) sets.Set[string] {
130130
func gatherAllocatedDevices(allocationResult *resource.DeviceAllocationResult) sets.Set[structured.DeviceID] {
131131
allocatedDevices := sets.New[structured.DeviceID]()
132132
for _, result := range allocationResult.Results {
133-
deviceID := structured.DeviceID{Driver: result.Driver, Pool: result.Pool, Device: result.Device}
133+
deviceID := structured.MakeDeviceID(result.Driver, result.Pool, result.Device)
134134
allocatedDevices.Insert(deviceID)
135135
}
136136
return allocatedDevices
@@ -276,7 +276,7 @@ func validateResourceClaimStatusUpdate(status, oldStatus *resource.ResourceClaim
276276
return validateDeviceStatus(device, fldPath, allocatedDevices)
277277
},
278278
func(device resource.AllocatedDeviceStatus) (structured.DeviceID, string) {
279-
return structured.DeviceID{Driver: device.Driver, Pool: device.Pool, Device: device.Device}, "deviceID"
279+
return structured.MakeDeviceID(device.Driver, device.Pool, device.Device), "deviceID"
280280
},
281281
fldPath.Child("devices"))...)
282282

@@ -744,7 +744,7 @@ func validateDeviceStatus(device resource.AllocatedDeviceStatus, fldPath *field.
744744
allErrs = append(allErrs, validateDriverName(device.Driver, fldPath.Child("driver"))...)
745745
allErrs = append(allErrs, validatePoolName(device.Pool, fldPath.Child("pool"))...)
746746
allErrs = append(allErrs, validateDeviceName(device.Device, fldPath.Child("device"))...)
747-
deviceID := structured.DeviceID{Driver: device.Driver, Pool: device.Pool, Device: device.Device}
747+
deviceID := structured.MakeDeviceID(device.Driver, device.Pool, device.Device)
748748
if !allocatedDevices.Has(deviceID) {
749749
allErrs = append(allErrs, field.Invalid(fldPath, deviceID, "must be an allocated device in the claim"))
750750
}

pkg/apis/resource/validation/validation_resourceclaim_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) {
10241024
"invalid-device-status-duplicate": {
10251025
wantFailures: field.ErrorList{
10261026
field.Duplicate(field.NewPath("status", "devices").Index(0).Child("networkData", "addresses").Index(1), "2001:db8::1/64"),
1027-
field.Duplicate(field.NewPath("status", "devices").Index(1).Child("deviceID"), structured.DeviceID{Driver: goodName, Pool: goodName, Device: goodName}),
1027+
field.Duplicate(field.NewPath("status", "devices").Index(1).Child("deviceID"), structured.MakeDeviceID(goodName, goodName, goodName)),
10281028
},
10291029
oldClaim: func() *resource.ResourceClaim { return validAllocatedClaim }(),
10301030
update: func(claim *resource.ResourceClaim) *resource.ResourceClaim {
@@ -1098,7 +1098,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) {
10981098
},
10991099
"invalid-device-status-no-device": {
11001100
wantFailures: field.ErrorList{
1101-
field.Invalid(field.NewPath("status", "devices").Index(0), structured.DeviceID{Driver: "b", Pool: "a", Device: "r"}, "must be an allocated device in the claim"),
1101+
field.Invalid(field.NewPath("status", "devices").Index(0), structured.MakeDeviceID("b", "a", "r"), "must be an allocated device in the claim"),
11021102
},
11031103
oldClaim: func() *resource.ResourceClaim { return validAllocatedClaim }(),
11041104
update: func(claim *resource.ResourceClaim) *resource.ResourceClaim {
@@ -1116,7 +1116,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) {
11161116
"invalid-device-status-duplicate-disabled-feature-gate": {
11171117
wantFailures: field.ErrorList{
11181118
field.Duplicate(field.NewPath("status", "devices").Index(0).Child("networkData", "addresses").Index(1), "2001:db8::1/64"),
1119-
field.Duplicate(field.NewPath("status", "devices").Index(1).Child("deviceID"), structured.DeviceID{Driver: goodName, Pool: goodName, Device: goodName}),
1119+
field.Duplicate(field.NewPath("status", "devices").Index(1).Child("deviceID"), structured.MakeDeviceID(goodName, goodName, goodName)),
11201120
},
11211121
oldClaim: func() *resource.ResourceClaim { return validAllocatedClaim }(),
11221122
update: func(claim *resource.ResourceClaim) *resource.ResourceClaim {
@@ -1190,7 +1190,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) {
11901190
},
11911191
"invalid-device-status-no-device-disabled-feature-gate": {
11921192
wantFailures: field.ErrorList{
1193-
field.Invalid(field.NewPath("status", "devices").Index(0), structured.DeviceID{Driver: "b", Pool: "a", Device: "r"}, "must be an allocated device in the claim"),
1193+
field.Invalid(field.NewPath("status", "devices").Index(0), structured.MakeDeviceID("b", "a", "r"), "must be an allocated device in the claim"),
11941194
},
11951195
oldClaim: func() *resource.ResourceClaim { return validAllocatedClaim }(),
11961196
update: func(claim *resource.ResourceClaim) *resource.ResourceClaim {

pkg/registry/resource/resourceclaim/strategy.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,27 +257,23 @@ func dropDeallocatedStatusDevices(newClaim, oldClaim *resource.ResourceClaim) {
257257
if oldClaim.Status.Allocation != nil {
258258
// Get all devices in the oldClaim.
259259
for _, result := range oldClaim.Status.Allocation.Devices.Results {
260-
deviceID := structured.DeviceID{Driver: result.Driver, Pool: result.Pool, Device: result.Device}
260+
deviceID := structured.MakeDeviceID(result.Driver, result.Pool, result.Device)
261261
deallocatedDevices.Insert(deviceID)
262262
}
263263
}
264264

265265
// Remove devices from deallocatedDevices that are still in newClaim.
266266
if newClaim.Status.Allocation != nil {
267267
for _, result := range newClaim.Status.Allocation.Devices.Results {
268-
deviceID := structured.DeviceID{Driver: result.Driver, Pool: result.Pool, Device: result.Device}
268+
deviceID := structured.MakeDeviceID(result.Driver, result.Pool, result.Device)
269269
deallocatedDevices.Delete(deviceID)
270270
}
271271
}
272272

273273
// Remove from newClaim.Status.Devices.
274274
n := 0
275275
for _, device := range newClaim.Status.Devices {
276-
deviceID := structured.DeviceID{
277-
Driver: device.Driver,
278-
Pool: device.Pool,
279-
Device: device.Device,
280-
}
276+
deviceID := structured.MakeDeviceID(device.Driver, device.Pool, device.Device)
281277
if !deallocatedDevices.Has(deviceID) {
282278
newClaim.Status.Devices[n] = device
283279
n++

0 commit comments

Comments
 (0)