From 6d69ee09379ef380f2eed61b6f1c0dcfe3776132 Mon Sep 17 00:00:00 2001 From: CLBRITTON2 Date: Fri, 9 Jan 2026 08:53:41 -0500 Subject: [PATCH] Add declarative validation tests for EndpointSlice addressType supported values --- pkg/apis/discovery/v1/zz_generated.validations.go | 13 +++++++++++++ .../discovery/v1beta1/zz_generated.validations.go | 13 +++++++++++++ pkg/apis/discovery/validation/validation.go | 2 +- pkg/generated/openapi/zz_generated.openapi.go | 3 ++- .../endpointslice/declarative_validation_test.go | 6 ++++++ staging/src/k8s.io/api/discovery/v1/types.go | 1 + staging/src/k8s.io/api/discovery/v1beta1/types.go | 2 ++ 7 files changed, 38 insertions(+), 2 deletions(-) diff --git a/pkg/apis/discovery/v1/zz_generated.validations.go b/pkg/apis/discovery/v1/zz_generated.validations.go index e5fed4b47f8..00458b52b3d 100644 --- a/pkg/apis/discovery/v1/zz_generated.validations.go +++ b/pkg/apis/discovery/v1/zz_generated.validations.go @@ -31,6 +31,7 @@ import ( safe "k8s.io/apimachinery/pkg/api/safe" validate "k8s.io/apimachinery/pkg/api/validate" runtime "k8s.io/apimachinery/pkg/runtime" + sets "k8s.io/apimachinery/pkg/util/sets" field "k8s.io/apimachinery/pkg/util/validation/field" ) @@ -50,6 +51,16 @@ func RegisterValidations(scheme *runtime.Scheme) error { return nil } +var symbolsForAddressType = sets.New(discoveryv1.AddressTypeFQDN, discoveryv1.AddressTypeIPv4, discoveryv1.AddressTypeIPv6) + +// Validate_AddressType validates an instance of AddressType according +// to declarative validation rules in the API schema. +func Validate_AddressType(ctx context.Context, op operation.Operation, fldPath *field.Path, obj, oldObj *discoveryv1.AddressType) (errs field.ErrorList) { + errs = append(errs, validate.Enum(ctx, op, fldPath, obj, oldObj, symbolsForAddressType, nil)...) + + return errs +} + // Validate_Endpoint validates an instance of Endpoint according // to declarative validation rules in the API schema. func Validate_Endpoint(ctx context.Context, op operation.Operation, fldPath *field.Path, obj, oldObj *discoveryv1.Endpoint) (errs field.ErrorList) { @@ -112,6 +123,8 @@ func Validate_EndpointSlice(ctx context.Context, op operation.Operation, fldPath if earlyReturn { return // do not proceed } + // call the type's validation function + errs = append(errs, Validate_AddressType(ctx, op, fldPath, obj, oldObj)...) return }(fldPath.Child("addressType"), &obj.AddressType, safe.Field(oldObj, func(oldObj *discoveryv1.EndpointSlice) *discoveryv1.AddressType { return &oldObj.AddressType }), oldObj != nil)...) diff --git a/pkg/apis/discovery/v1beta1/zz_generated.validations.go b/pkg/apis/discovery/v1beta1/zz_generated.validations.go index c41ef7de08b..2bf2d20f6fe 100644 --- a/pkg/apis/discovery/v1beta1/zz_generated.validations.go +++ b/pkg/apis/discovery/v1beta1/zz_generated.validations.go @@ -31,6 +31,7 @@ import ( safe "k8s.io/apimachinery/pkg/api/safe" validate "k8s.io/apimachinery/pkg/api/validate" runtime "k8s.io/apimachinery/pkg/runtime" + sets "k8s.io/apimachinery/pkg/util/sets" field "k8s.io/apimachinery/pkg/util/validation/field" ) @@ -50,6 +51,16 @@ func RegisterValidations(scheme *runtime.Scheme) error { return nil } +var symbolsForAddressType = sets.New(discoveryv1beta1.AddressTypeFQDN, discoveryv1beta1.AddressTypeIPv4, discoveryv1beta1.AddressTypeIPv6) + +// Validate_AddressType validates an instance of AddressType according +// to declarative validation rules in the API schema. +func Validate_AddressType(ctx context.Context, op operation.Operation, fldPath *field.Path, obj, oldObj *discoveryv1beta1.AddressType) (errs field.ErrorList) { + errs = append(errs, validate.Enum(ctx, op, fldPath, obj, oldObj, symbolsForAddressType, nil)...) + + return errs +} + // Validate_Endpoint validates an instance of Endpoint according // to declarative validation rules in the API schema. func Validate_Endpoint(ctx context.Context, op operation.Operation, fldPath *field.Path, obj, oldObj *discoveryv1beta1.Endpoint) (errs field.ErrorList) { @@ -111,6 +122,8 @@ func Validate_EndpointSlice(ctx context.Context, op operation.Operation, fldPath if earlyReturn { return // do not proceed } + // call the type's validation function + errs = append(errs, Validate_AddressType(ctx, op, fldPath, obj, oldObj)...) return }(fldPath.Child("addressType"), &obj.AddressType, safe.Field(oldObj, func(oldObj *discoveryv1beta1.EndpointSlice) *discoveryv1beta1.AddressType { return &oldObj.AddressType }), oldObj != nil)...) diff --git a/pkg/apis/discovery/validation/validation.go b/pkg/apis/discovery/validation/validation.go index 89a6b584dfd..10bdc69a1d4 100644 --- a/pkg/apis/discovery/validation/validation.go +++ b/pkg/apis/discovery/validation/validation.go @@ -209,7 +209,7 @@ func validateAddressType(addressType discovery.AddressType) field.ErrorList { if addressType == "" { allErrs = append(allErrs, field.Required(field.NewPath("addressType"), "").MarkCoveredByDeclarative()) } else if !supportedAddressTypes.Has(addressType) { - allErrs = append(allErrs, field.NotSupported(field.NewPath("addressType"), addressType, sets.List(supportedAddressTypes))) + allErrs = append(allErrs, field.NotSupported(field.NewPath("addressType"), addressType, sets.List(supportedAddressTypes)).MarkCoveredByDeclarative()) } return allErrs diff --git a/pkg/generated/openapi/zz_generated.openapi.go b/pkg/generated/openapi/zz_generated.openapi.go index abfc6af67c8..68b1c0dfd2f 100644 --- a/pkg/generated/openapi/zz_generated.openapi.go +++ b/pkg/generated/openapi/zz_generated.openapi.go @@ -33603,10 +33603,11 @@ func schema_k8sio_api_discovery_v1beta1_EndpointSlice(ref common.ReferenceCallba }, "addressType": { SchemaProps: spec.SchemaProps{ - Description: "addressType specifies the type of address carried by this EndpointSlice. All addresses in this slice must be the same type. This field is immutable after creation. The following address types are currently supported: * IPv4: Represents an IPv4 Address. * IPv6: Represents an IPv6 Address. * FQDN: Represents a Fully Qualified Domain Name.", + Description: "addressType specifies the type of address carried by this EndpointSlice. All addresses in this slice must be the same type. This field is immutable after creation. The following address types are currently supported: * IPv4: Represents an IPv4 Address. * IPv6: Represents an IPv6 Address. * FQDN: Represents a Fully Qualified Domain Name.\n\nPossible enum values:\n - `\"FQDN\"` represents a FQDN.\n - `\"IPv4\"` represents an IPv4 Address.\n - `\"IPv6\"` represents an IPv6 Address.", Default: "", Type: []string{"string"}, Format: "", + Enum: []interface{}{"FQDN", "IPv4", "IPv6"}, }, }, "endpoints": { diff --git a/pkg/registry/discovery/endpointslice/declarative_validation_test.go b/pkg/registry/discovery/endpointslice/declarative_validation_test.go index 496a4b042ae..9f17498b3c3 100644 --- a/pkg/registry/discovery/endpointslice/declarative_validation_test.go +++ b/pkg/registry/discovery/endpointslice/declarative_validation_test.go @@ -76,6 +76,12 @@ func testDeclarativeValidate(t *testing.T, apiVersion string) { field.Required(field.NewPath("addressType"), ""), }, }, + "invalid addressType not supported": { + input: mkValidEndpointSlice(tweakAddressType("invalid")), + expectedErrs: field.ErrorList{ + field.NotSupported(field.NewPath("addressType"), discovery.AddressType("invalid"), []string{string(discovery.AddressTypeIPv4), string(discovery.AddressTypeIPv6), string(discovery.AddressTypeFQDN)}), + }, + }, } for name, tc := range testCases { t.Run(name, func(t *testing.T) { diff --git a/staging/src/k8s.io/api/discovery/v1/types.go b/staging/src/k8s.io/api/discovery/v1/types.go index de83652ac0c..b38104553cd 100644 --- a/staging/src/k8s.io/api/discovery/v1/types.go +++ b/staging/src/k8s.io/api/discovery/v1/types.go @@ -72,6 +72,7 @@ type EndpointSlice struct { // AddressType represents the type of address referred to by an endpoint. // +enum +// +k8s:enum type AddressType string const ( diff --git a/staging/src/k8s.io/api/discovery/v1beta1/types.go b/staging/src/k8s.io/api/discovery/v1beta1/types.go index a04d98d01c1..9d882019336 100644 --- a/staging/src/k8s.io/api/discovery/v1beta1/types.go +++ b/staging/src/k8s.io/api/discovery/v1beta1/types.go @@ -66,6 +66,8 @@ type EndpointSlice struct { } // AddressType represents the type of address referred to by an endpoint. +// +enum +// +k8s:enum type AddressType string const (