Commit aac20ed6 authored by k8s-merge-robot's avatar k8s-merge-robot

Merge pull request #20600 from freehan/servicevalidation

Auto commit by PR queue bot
parents 70946a57 ec7366fc
......@@ -1653,12 +1653,18 @@ func ValidateService(service *api.Service) field.ErrorList {
if service.Spec.Type == api.ServiceTypeLoadBalancer {
portsPath := specPath.Child("ports")
includeProtocols := sets.NewString()
for i := range service.Spec.Ports {
portPath := portsPath.Index(i)
if !supportedPortProtocols.Has(string(service.Spec.Ports[i].Protocol)) {
allErrs = append(allErrs, field.Invalid(portPath.Child("protocol"), service.Spec.Ports[i].Protocol, "cannot create an external load balancer with non-TCP/UDP ports"))
} else {
includeProtocols.Insert(string(service.Spec.Ports[i].Protocol))
}
}
if includeProtocols.Len() > 1 {
allErrs = append(allErrs, field.Invalid(portsPath, service.Spec.Ports, "cannot create an external load balancer with mix protocols"))
}
}
if service.Spec.Type == api.ServiceTypeClusterIP {
......
......@@ -2580,11 +2580,19 @@ func TestValidateService(t *testing.T) {
name: "valid load balancer protocol UDP 2",
tweakSvc: func(s *api.Service) {
s.Spec.Type = api.ServiceTypeLoadBalancer
s.Spec.Ports = append(s.Spec.Ports, api.ServicePort{Name: "q", Port: 12345, Protocol: "UDP", TargetPort: intstr.FromInt(12345)})
s.Spec.Ports[0] = api.ServicePort{Name: "q", Port: 12345, Protocol: "UDP", TargetPort: intstr.FromInt(12345)}
},
numErrs: 0,
},
{
name: "invalid load balancer with mix protocol",
tweakSvc: func(s *api.Service) {
s.Spec.Type = api.ServiceTypeLoadBalancer
s.Spec.Ports = append(s.Spec.Ports, api.ServicePort{Name: "q", Port: 12345, Protocol: "UDP", TargetPort: intstr.FromInt(12345)})
},
numErrs: 1,
},
{
name: "valid 1",
tweakSvc: func(s *api.Service) {
// do nothing
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment