diff --git a/pkg/apis/core/validation/validation.go b/pkg/apis/core/validation/validation.go index a072d7cee28..d5c66ce84c4 100644 --- a/pkg/apis/core/validation/validation.go +++ b/pkg/apis/core/validation/validation.go @@ -4455,6 +4455,7 @@ func ValidateService(service *core.Service) field.ErrorList { // external traffic fields allErrs = append(allErrs, validateServiceExternalTrafficFieldsValue(service)...) + allErrs = append(allErrs, validateServiceExternalTrafficFieldsCombination(service)...) // internal traffic policy field allErrs = append(allErrs, validateServiceInternalTrafficFieldsValue(service)...) @@ -4545,11 +4546,11 @@ func validateServiceInternalTrafficFieldsValue(service *core.Service) field.Erro return allErrs } -// ValidateServiceExternalTrafficFieldsCombination validates if ExternalTrafficPolicy, +// validateServiceExternalTrafficFieldsCombination validates if ExternalTrafficPolicy, // HealthCheckNodePort and Type combination are legal. For update, it should be called // after clearing externalTraffic related fields for the ease of transitioning between // different service types. -func ValidateServiceExternalTrafficFieldsCombination(service *core.Service) field.ErrorList { +func validateServiceExternalTrafficFieldsCombination(service *core.Service) field.ErrorList { allErrs := field.ErrorList{} if service.Spec.Type != core.ServiceTypeLoadBalancer && diff --git a/pkg/apis/core/validation/validation_test.go b/pkg/apis/core/validation/validation_test.go index 2ec590aef68..64182ef6ef4 100644 --- a/pkg/apis/core/validation/validation_test.go +++ b/pkg/apis/core/validation/validation_test.go @@ -12019,7 +12019,8 @@ func TestValidateServiceExternalTrafficFieldsCombination(t *testing.T) { for _, tc := range testCases { svc := makeValidService() tc.tweakSvc(&svc) - errs := ValidateServiceExternalTrafficFieldsCombination(&svc) + // TODO: This test is probably insufficient for such a big function under test. + errs := validateServiceExternalTrafficFieldsCombination(&svc) if len(errs) != tc.numErrs { t.Errorf("Unexpected error list for case %q: %v", tc.name, errs.ToAggregate()) } diff --git a/pkg/registry/core/service/storage/rest.go b/pkg/registry/core/service/storage/rest.go index 093232c8692..57d2573b2c2 100644 --- a/pkg/registry/core/service/storage/rest.go +++ b/pkg/registry/core/service/storage/rest.go @@ -40,7 +40,6 @@ import ( "k8s.io/klog/v2" apiservice "k8s.io/kubernetes/pkg/api/service" api "k8s.io/kubernetes/pkg/apis/core" - "k8s.io/kubernetes/pkg/apis/core/validation" "k8s.io/kubernetes/pkg/features" registry "k8s.io/kubernetes/pkg/registry/core/service" "k8s.io/kubernetes/pkg/registry/core/service/ipallocator" @@ -230,9 +229,6 @@ func (rs *REST) Create(ctx context.Context, obj runtime.Object, createValidation return nil, errors.NewInternalError(err) } } - if errs := validation.ValidateServiceExternalTrafficFieldsCombination(service); len(errs) > 0 { - return nil, errors.NewInvalid(api.Kind("Service"), service.Name, errs) - } out, err := rs.services.Create(ctx, service, createValidation, options) if err != nil { @@ -468,9 +464,6 @@ func (rs *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObj if !success || err != nil { return nil, false, err } - if errs := validation.ValidateServiceExternalTrafficFieldsCombination(service); len(errs) > 0 { - return nil, false, errors.NewInvalid(api.Kind("Service"), service.Name, errs) - } out, created, err := rs.services.Update(ctx, service.Name, rest.DefaultUpdatedObjectInfo(service), createValidation, updateValidation, forceAllowCreate, options) if err == nil {