Svc REST: Don't call validation directly

The validation is called soon after anyway.
This commit is contained in:
Tim Hockin 2020-11-17 22:10:19 -08:00
parent b76a8c3c40
commit 14d0571a5f
3 changed files with 5 additions and 10 deletions

View File

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

View File

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

View File

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