diff --git a/pkg/apis/resource/fuzzer/fuzzer.go b/pkg/apis/resource/fuzzer/fuzzer.go index f9fa97a102a..31b64886e00 100644 --- a/pkg/apis/resource/fuzzer/fuzzer.go +++ b/pkg/apis/resource/fuzzer/fuzzer.go @@ -62,7 +62,7 @@ var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} { // // This is necessary because randomly generated content // might be valid JSON which changes during re-encoding. - r.Data = &runtime.RawExtension{Raw: []byte(`{"apiVersion":"unknown.group/unknown","kind":"Something","someKey":"someValue"}`)} + r.Data = runtime.RawExtension{Raw: []byte(`{"apiVersion":"unknown.group/unknown","kind":"Something","someKey":"someValue"}`)} }, } } diff --git a/pkg/apis/resource/types.go b/pkg/apis/resource/types.go index ff7b704cb87..9aa9a3981a4 100644 --- a/pkg/apis/resource/types.go +++ b/pkg/apis/resource/types.go @@ -1027,7 +1027,7 @@ type AllocatedDeviceStatus struct { // Data contains arbitrary driver-specific data. // // +optional - Data *runtime.RawExtension + Data runtime.RawExtension // NetworkData contains network-related information specific to the device. // @@ -1044,7 +1044,7 @@ type NetworkDeviceData struct { // network interface. // // +optional - InterfaceName *string + InterfaceName string // Addresses lists the network addresses assigned to the device's network interface. // This can include both IPv4 and IPv6 addresses. @@ -1059,5 +1059,5 @@ type NetworkDeviceData struct { // HWAddress represents the hardware address (e.g. MAC Address) of the device's network interface. // // +optional - HWAddress *string + HWAddress string } diff --git a/pkg/apis/resource/validation/validation.go b/pkg/apis/resource/validation/validation.go index 8349a1a84b5..0b717ad8864 100644 --- a/pkg/apis/resource/validation/validation.go +++ b/pkg/apis/resource/validation/validation.go @@ -257,24 +257,7 @@ func validateDeviceConfiguration(config resource.DeviceConfiguration, fldPath *f func validateOpaqueConfiguration(config resource.OpaqueDeviceConfiguration, fldPath *field.Path, stored bool) field.ErrorList { var allErrs field.ErrorList allErrs = append(allErrs, validateDriverName(config.Driver, fldPath.Child("driver"))...) - // Validation of RawExtension as in https://github.com/kubernetes/kubernetes/pull/125549/ - var v any - if len(config.Parameters.Raw) == 0 { - allErrs = append(allErrs, field.Required(fldPath.Child("parameters"), "")) - } else if !stored && len(config.Parameters.Raw) > resource.OpaqueParametersMaxLength { - // Don't even bother with parsing when too large. - // Only applies on create. Existing parameters are grand-fathered in - // because the limit was introduced in 1.32. This also means that it - // can be changed in the future. - allErrs = append(allErrs, field.TooLong(fldPath.Child("parameters"), "" /* unused */, resource.OpaqueParametersMaxLength)) - } else if err := json.Unmarshal(config.Parameters.Raw, &v); err != nil { - allErrs = append(allErrs, field.Invalid(fldPath.Child("parameters"), "", fmt.Sprintf("error parsing data as JSON: %v", err.Error()))) - } else if v == nil { - allErrs = append(allErrs, field.Required(fldPath.Child("parameters"), "")) - } else if _, isObject := v.(map[string]any); !isObject { - allErrs = append(allErrs, field.Invalid(fldPath.Child("parameters"), "", "parameters must be a valid JSON object")) - } - + allErrs = append(allErrs, validateRawExtension(config.Parameters, fldPath.Child("parameters"))...) return allErrs } @@ -770,20 +753,23 @@ func validateDeviceStatus(device resource.AllocatedDeviceStatus, fldPath *field. var allErrs field.ErrorList return allErrs }, fldPath.Child("conditions"))...) - allErrs = append(allErrs, validateRawExtension(device.Data, fldPath.Child("data"))...) + if len(device.Data.Raw) > 0 { // Data is an optional field. + allErrs = append(allErrs, validateRawExtension(device.Data, fldPath.Child("data"))...) + } allErrs = append(allErrs, validateNetworkDeviceData(device.NetworkData, fldPath.Child("networkData"))...) return allErrs } -func validateRawExtension(rawExtension *runtime.RawExtension, fldPath *field.Path) field.ErrorList { +func validateRawExtension(rawExtension runtime.RawExtension, fldPath *field.Path) field.ErrorList { var allErrs field.ErrorList - if rawExtension == nil { - return allErrs - } // Validation of RawExtension as in https://github.com/kubernetes/kubernetes/pull/125549/ var v any - if err := json.Unmarshal(rawExtension.Raw, &v); err != nil { + if len(rawExtension.Raw) == 0 { + allErrs = append(allErrs, field.Required(fldPath, "")) + } else if err := json.Unmarshal(rawExtension.Raw, &v); err != nil { allErrs = append(allErrs, field.Invalid(fldPath, "", fmt.Sprintf("error parsing data: %v", err.Error()))) + } else if v == nil { + allErrs = append(allErrs, field.Required(fldPath, "")) } else if _, isObject := v.(map[string]any); !isObject { allErrs = append(allErrs, field.Invalid(fldPath, "", "parameters must be a valid JSON object")) } diff --git a/pkg/apis/resource/validation/validation_resourceclaim_test.go b/pkg/apis/resource/validation/validation_resourceclaim_test.go index 72d8b96c29f..c155b4a16d5 100644 --- a/pkg/apis/resource/validation/validation_resourceclaim_test.go +++ b/pkg/apis/resource/validation/validation_resourceclaim_test.go @@ -1000,16 +1000,16 @@ func TestValidateClaimStatusUpdate(t *testing.T) { Status: metav1.ConditionTrue, }, }, - Data: &runtime.RawExtension{ + Data: runtime.RawExtension{ Raw: []byte(`{"kind": "foo", "apiVersion": "dra.example.com/v1"}`), }, NetworkData: &resource.NetworkDeviceData{ - InterfaceName: ptr.To("net-1"), + InterfaceName: "net-1", Addresses: []string{ "10.9.8.0/24", "2001:db8::/64", }, - HWAddress: ptr.To("ea:9f:cb:40:b1:7b"), + HWAddress: "ea:9f:cb:40:b1:7b", }, }, } @@ -1075,7 +1075,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) { Driver: goodName, Pool: goodName, Device: goodName, - Data: &runtime.RawExtension{ + Data: runtime.RawExtension{ Raw: []byte(`foo`), }, }, @@ -1154,7 +1154,7 @@ func TestValidateClaimStatusUpdate(t *testing.T) { Driver: goodName, Pool: goodName, Device: goodName, - Data: &runtime.RawExtension{ + Data: runtime.RawExtension{ Raw: []byte(`foo`), }, }, diff --git a/staging/src/k8s.io/api/resource/v1alpha3/types.go b/staging/src/k8s.io/api/resource/v1alpha3/types.go index cfe018d2709..8ea60822241 100644 --- a/staging/src/k8s.io/api/resource/v1alpha3/types.go +++ b/staging/src/k8s.io/api/resource/v1alpha3/types.go @@ -1038,7 +1038,7 @@ type AllocatedDeviceStatus struct { // Data contains arbitrary driver-specific data. // // +optional - Data *runtime.RawExtension `json:"data,omitempty" protobuf:"bytes,5,opt,name=data"` + Data runtime.RawExtension `json:"data,omitempty" protobuf:"bytes,5,opt,name=data"` // NetworkData contains network-related information specific to the device. // @@ -1055,7 +1055,7 @@ type NetworkDeviceData struct { // network interface. // // +optional - InterfaceName *string `json:"interfaceName,omitempty" protobuf:"bytes,1,opt,name=interfaceName"` + InterfaceName string `json:"interfaceName,omitempty" protobuf:"bytes,1,opt,name=interfaceName"` // Addresses lists the network addresses assigned to the device's network interface. // This can include both IPv4 and IPv6 addresses. @@ -1070,5 +1070,5 @@ type NetworkDeviceData struct { // HWAddress represents the hardware address (e.g. MAC Address) of the device's network interface. // // +optional - HWAddress *string `json:"hwAddress,omitempty" protobuf:"bytes,3,opt,name=hwAddress"` + HWAddress string `json:"hwAddress,omitempty" protobuf:"bytes,3,opt,name=hwAddress"` } diff --git a/test/e2e/dra/dra.go b/test/e2e/dra/dra.go index 7fa3aab928a..eb53bb72243 100644 --- a/test/e2e/dra/dra.go +++ b/test/e2e/dra/dra.go @@ -438,11 +438,11 @@ var _ = framework.SIGDescribe("node")("DRA", feature.DynamicResourceAllocation, Pool: allocatedResourceClaim.Status.Allocation.Devices.Results[0].Pool, Device: allocatedResourceClaim.Status.Allocation.Devices.Results[0].Device, Conditions: []metav1.Condition{{Type: "a", Status: "b", Message: "c", Reason: "d"}}, - Data: &runtime.RawExtension{Raw: []byte(`{"foo":"bar"}`)}, + Data: runtime.RawExtension{Raw: []byte(`{"foo":"bar"}`)}, NetworkData: &resourceapi.NetworkDeviceData{ - InterfaceName: ptr.To("inf1"), + InterfaceName: "inf1", Addresses: []string{"10.9.8.0/24", "2001:db8::/64"}, - HWAddress: ptr.To("bc:1c:b6:3e:b8:25"), + HWAddress: "bc:1c:b6:3e:b8:25", }, }) // Updates the ResourceClaim from the driver on the same node as the pod. @@ -457,11 +457,11 @@ var _ = framework.SIGDescribe("node")("DRA", feature.DynamicResourceAllocation, Pool: allocatedResourceClaim.Status.Allocation.Devices.Results[0].Pool, Device: allocatedResourceClaim.Status.Allocation.Devices.Results[0].Device, Conditions: []metav1.Condition{{Type: "e", Status: "f", Message: "g", Reason: "h"}}, - Data: &runtime.RawExtension{Raw: []byte(`{"bar":"foo"}`)}, + Data: runtime.RawExtension{Raw: []byte(`{"bar":"foo"}`)}, NetworkData: &resourceapi.NetworkDeviceData{ - InterfaceName: ptr.To("inf2"), + InterfaceName: "inf2", Addresses: []string{"10.9.8.1/24", "2001:db8::1/64"}, - HWAddress: ptr.To("bc:1c:b6:3e:b8:26"), + HWAddress: "bc:1c:b6:3e:b8:26", }, } updatedResourceClaim2, err := driver.Nodes[scheduledPod.Spec.NodeName].ExamplePlugin.UpdateStatus(ctx, updatedResourceClaim) diff --git a/test/integration/resourceclaim/feature_enable_disable_test.go b/test/integration/resourceclaim/feature_enable_disable_test.go index 88213c4674b..526d04fc119 100644 --- a/test/integration/resourceclaim/feature_enable_disable_test.go +++ b/test/integration/resourceclaim/feature_enable_disable_test.go @@ -28,7 +28,6 @@ import ( kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing" "k8s.io/kubernetes/pkg/features" "k8s.io/kubernetes/test/integration/framework" - "k8s.io/utils/ptr" ) // TestEnableDisableDRAResourceClaimDeviceStatus first test the feature gate disabled @@ -82,16 +81,16 @@ func TestEnableDisableDRAResourceClaimDeviceStatus(t *testing.T) { Driver: "foo", Pool: "foo", Device: "foo", - Data: &runtime.RawExtension{ + Data: runtime.RawExtension{ Raw: []byte(`{"kind": "foo", "apiVersion": "dra.example.com/v1"}`), }, NetworkData: &v1alpha3.NetworkDeviceData{ - InterfaceName: ptr.To("net-1"), + InterfaceName: "net-1", Addresses: []string{ "10.9.8.0/24", "2001:db8::/64", }, - HWAddress: ptr.To("ea:9f:cb:40:b1:7b"), + HWAddress: "ea:9f:cb:40:b1:7b", }, }, }, @@ -154,16 +153,16 @@ func TestEnableDisableDRAResourceClaimDeviceStatus(t *testing.T) { Driver: "bar", Pool: "bar", Device: "bar", - Data: &runtime.RawExtension{ + Data: runtime.RawExtension{ Raw: []byte(`{"kind": "foo", "apiVersion": "dra.example.com/v1"}`), }, NetworkData: &v1alpha3.NetworkDeviceData{ - InterfaceName: ptr.To("net-1"), + InterfaceName: "net-1", Addresses: []string{ "10.9.8.0/24", "2001:db8::/64", }, - HWAddress: ptr.To("ea:9f:cb:40:b1:7b"), + HWAddress: "ea:9f:cb:40:b1:7b", }, }, }, @@ -190,16 +189,16 @@ func TestEnableDisableDRAResourceClaimDeviceStatus(t *testing.T) { Driver: "bar", Pool: "bar", Device: "bar", - Data: &runtime.RawExtension{ + Data: runtime.RawExtension{ Raw: []byte(`{"kind": "foo", "apiVersion": "dra.example.com/v1"}`), }, NetworkData: &v1alpha3.NetworkDeviceData{ - InterfaceName: ptr.To("net-1"), + InterfaceName: "net-1", Addresses: []string{ "10.9.8.0/24", "2001:db8::/64", }, - HWAddress: ptr.To("ea:9f:cb:40:b1:7b"), + HWAddress: "ea:9f:cb:40:b1:7b", }, }, },