mirror of https://github.com/k3s-io/k3s
add service validation for mix protocol
parent
210bac10c9
commit
ec7366fc2a
|
@ -1509,12 +1509,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 {
|
||||
|
|
|
@ -2269,10 +2269,18 @@ 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) {
|
||||
|
|
Loading…
Reference in New Issue