Add declarative validation tests for EndpointSlice addressType immutability

This commit is contained in:
CLBRITTON2
2026-01-09 08:36:47 -05:00
parent 1ee5d304a9
commit 5ee4b49ebd
8 changed files with 20 additions and 1 deletions

View File

@@ -101,6 +101,10 @@ func Validate_EndpointSlice(ctx context.Context, op operation.Operation, fldPath
}
// call field-attached validations
earlyReturn := false
if e := validate.Immutable(ctx, op, fldPath, obj, oldObj); len(e) != 0 {
errs = append(errs, e...)
earlyReturn = true
}
if e := validate.RequiredValue(ctx, op, fldPath, obj, oldObj); len(e) != 0 {
errs = append(errs, e...)
earlyReturn = true

View File

@@ -100,6 +100,10 @@ func Validate_EndpointSlice(ctx context.Context, op operation.Operation, fldPath
}
// call field-attached validations
earlyReturn := false
if e := validate.Immutable(ctx, op, fldPath, obj, oldObj); len(e) != 0 {
errs = append(errs, e...)
earlyReturn = true
}
if e := validate.RequiredValue(ctx, op, fldPath, obj, oldObj); len(e) != 0 {
errs = append(errs, e...)
earlyReturn = true

View File

@@ -86,7 +86,7 @@ func ValidateEndpointSliceCreate(endpointSlice *discovery.EndpointSlice) field.E
// ValidateEndpointSliceUpdate validates an EndpointSlice when it is updated.
func ValidateEndpointSliceUpdate(newEndpointSlice, oldEndpointSlice *discovery.EndpointSlice) field.ErrorList {
allErrs := ValidateEndpointSlice(newEndpointSlice, oldEndpointSlice)
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newEndpointSlice.AddressType, oldEndpointSlice.AddressType, field.NewPath("addressType"))...)
allErrs = append(allErrs, apivalidation.ValidateImmutableField(newEndpointSlice.AddressType, oldEndpointSlice.AddressType, field.NewPath("addressType")).WithOrigin("immutable").MarkCoveredByDeclarative()...)
return allErrs
}

View File

@@ -122,6 +122,13 @@ func testDeclarativeValidateUpdate(t *testing.T, apiVersion string) {
field.TooMany(field.NewPath("endpoints").Index(0).Child("addresses"), 101, 100).WithOrigin("maxItems"),
},
},
"invalid update addressType immutable": {
oldObj: mkValidEndpointSlice(),
updateObj: mkValidEndpointSlice(tweakAddressType(discovery.AddressTypeIPv6)),
expectedErrs: field.ErrorList{
field.Invalid(field.NewPath("addressType"), discovery.AddressTypeIPv6, "field is immutable").WithOrigin("immutable"),
},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {

View File

@@ -186,6 +186,7 @@ message EndpointSlice {
// the "FQDN" type.
// +required
// +k8s:required
// +k8s:immutable
optional string addressType = 4;
// endpoints is a list of unique endpoints in this slice. Each slice may

View File

@@ -50,6 +50,7 @@ type EndpointSlice struct {
// the "FQDN" type.
// +required
// +k8s:required
// +k8s:immutable
AddressType AddressType `json:"addressType" protobuf:"bytes,4,rep,name=addressType"`
// endpoints is a list of unique endpoints in this slice. Each slice may

View File

@@ -172,6 +172,7 @@ message EndpointSlice {
// * FQDN: Represents a Fully Qualified Domain Name.
// +required
// +k8s:required
// +k8s:immutable
optional string addressType = 4;
// endpoints is a list of unique endpoints in this slice. Each slice may

View File

@@ -47,6 +47,7 @@ type EndpointSlice struct {
// * FQDN: Represents a Fully Qualified Domain Name.
// +required
// +k8s:required
// +k8s:immutable
AddressType AddressType `json:"addressType" protobuf:"bytes,4,rep,name=addressType"`
// endpoints is a list of unique endpoints in this slice. Each slice may