Add declarative validation tests for EndpointSlice addressType supported values

This commit is contained in:
CLBRITTON2
2026-01-09 08:53:41 -05:00
parent 5ee4b49ebd
commit 6d69ee0937
7 changed files with 38 additions and 2 deletions

View File

@@ -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)...)

View File

@@ -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)...)

View File

@@ -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

View File

@@ -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": {

View File

@@ -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) {

View File

@@ -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 (

View File

@@ -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 (