Mixed protocol support for Services with type=LoadBalancer (#94028)

* Mixed protocol support for Services with type=LoadBalancer

KEP: https://github.com/kubernetes/enhancements/blob/master/keps/sig-network/20200103-mixed-protocol-lb.md
Add new feature gate to control the support of mixed protocols in Services with type=LoadBalancer
Add new fields to the ServiceStatus
  Add Ports to the LoadBalancerIngress, so cloud provider implementations can report the status of the requested load balanc
er ports
  Add ServiceCondition to the ServiceStatus so Service controllers can indicate the conditions of the Service

* regenerate conflicting stuff
This commit is contained in:
Laszlo Janosi
2020-11-13 22:21:04 +01:00
committed by GitHub
parent 0081e0ebf5
commit c970a46bc1
30 changed files with 2219 additions and 970 deletions

View File

@@ -7,6 +7,7 @@ load(
go_library(
name = "go_default_library",
srcs = [
"conditional_validation.go",
"doc.go",
"events.go",
"validation.go",
@@ -48,6 +49,7 @@ go_library(
go_test(
name = "go_default_test",
srcs = [
"conditional_validation_test.go",
"events_test.go",
"validation_test.go",
],

View File

@@ -0,0 +1,61 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package validation
import (
"k8s.io/apimachinery/pkg/util/validation/field"
utilfeature "k8s.io/apiserver/pkg/util/feature"
api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/features"
)
// ValidateConditionalService validates conditionally valid fields.
func ValidateConditionalService(service, oldService *api.Service) field.ErrorList {
var errs field.ErrorList
errs = append(errs, validateMixedProtocolLBService(service, oldService)...)
return errs
}
// validateMixedProtocolLBService checks if the old Service has type=LoadBalancer and whether the Service has different Protocols
// on its ports. If the MixedProtocolLBService feature flag is disabled the usage of different Protocols in the new Service is
// valid only if the old Service has different Protocols, too.
func validateMixedProtocolLBService(service, oldService *api.Service) (errs field.ErrorList) {
if service.Spec.Type != api.ServiceTypeLoadBalancer {
return
}
if utilfeature.DefaultFeatureGate.Enabled(features.MixedProtocolLBService) {
return
}
if serviceHasMixedProtocols(service) && !serviceHasMixedProtocols(oldService) {
errs = append(errs, field.Invalid(field.NewPath("spec", "ports"), service.Spec.Ports, "may not contain more than 1 protocol when type is 'LoadBalancer'"))
}
return
}
func serviceHasMixedProtocols(service *api.Service) bool {
if service == nil {
return false
}
protos := map[string]bool{}
for _, port := range service.Spec.Ports {
protos[string(port.Protocol)] = true
}
return len(protos) > 1
}

View File

@@ -0,0 +1,277 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package validation
import (
"strings"
"testing"
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
api "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/features"
)
func TestValidateMixedProtocolLBService(t *testing.T) {
newLBServiceDifferentProtocols := &api.Service{
Spec: api.ServiceSpec{
Ports: []api.ServicePort{
{
Protocol: api.ProtocolTCP,
},
{
Protocol: api.ProtocolUDP,
},
},
Type: api.ServiceTypeLoadBalancer,
},
}
newLBServiceSameProtocols := &api.Service{
Spec: api.ServiceSpec{
Ports: []api.ServicePort{
{
Protocol: api.ProtocolTCP,
},
{
Protocol: api.ProtocolTCP,
},
},
Type: api.ServiceTypeLoadBalancer,
},
}
newNonLBServiceDifferentProtocols := &api.Service{
Spec: api.ServiceSpec{
Ports: []api.ServicePort{
{
Protocol: api.ProtocolTCP,
},
{
Protocol: api.ProtocolUDP,
},
},
Type: api.ServiceTypeNodePort,
},
}
newNonLBServiceSameProtocols := &api.Service{
Spec: api.ServiceSpec{
Ports: []api.ServicePort{
{
Protocol: api.ProtocolUDP,
},
{
Protocol: api.ProtocolUDP,
},
},
Type: api.ServiceTypeNodePort,
},
}
oldLBServiceDifferentProtocols := &api.Service{
Spec: api.ServiceSpec{
Ports: []api.ServicePort{
{
Protocol: api.ProtocolTCP,
},
{
Protocol: api.ProtocolUDP,
},
},
Type: api.ServiceTypeLoadBalancer,
},
}
oldLBServiceSameProtocols := &api.Service{
Spec: api.ServiceSpec{
Ports: []api.ServicePort{
{
Protocol: api.ProtocolTCP,
},
{
Protocol: api.ProtocolTCP,
},
},
Type: api.ServiceTypeLoadBalancer,
},
}
oldNonLBServiceDifferentProtocols := &api.Service{
Spec: api.ServiceSpec{
Ports: []api.ServicePort{
{
Protocol: api.ProtocolTCP,
},
{
Protocol: api.ProtocolUDP,
},
},
Type: api.ServiceTypeNodePort,
},
}
oldNonLBServiceSameProtocols := &api.Service{
Spec: api.ServiceSpec{
Ports: []api.ServicePort{
{
Protocol: api.ProtocolUDP,
},
{
Protocol: api.ProtocolUDP,
},
},
Type: api.ServiceTypeNodePort,
},
}
cases := map[string]struct {
oldService *api.Service
newService *api.Service
fgEnabled bool
expectedError []string
}{
"Old service is nil, new service has different protocols, feature gate false": {
oldService: nil,
newService: newLBServiceDifferentProtocols,
fgEnabled: false,
expectedError: []string{`spec.ports: Invalid value: []core.ServicePort{core.ServicePort{Name:"", Protocol:"TCP", AppProtocol:(*string)(nil), Port:0, TargetPort:intstr.IntOrString{Type:0, IntVal:0, StrVal:""}, NodePort:0}, core.ServicePort{Name:"", Protocol:"UDP", AppProtocol:(*string)(nil), Port:0, TargetPort:intstr.IntOrString{Type:0, IntVal:0, StrVal:""}, NodePort:0}}: may not contain more than 1 protocol when type is 'LoadBalancer'`},
},
"Old service is nil, new service has different protocols, feature gate true": {
oldService: nil,
newService: newLBServiceDifferentProtocols,
fgEnabled: true,
},
"Old service is nil, new service does not have different protocols, feature gate false": {
oldService: nil,
newService: newLBServiceSameProtocols,
fgEnabled: false,
},
"Old service is nil, new service does not have different protocols, feature gate true": {
oldService: nil,
newService: newLBServiceSameProtocols,
fgEnabled: true,
},
"Old service is nil, new non-LB service has different protocols, feature gate false": {
oldService: nil,
newService: newNonLBServiceDifferentProtocols,
fgEnabled: false,
},
"Old service is nil, new non-LB service has different protocols, feature gate true": {
oldService: nil,
newService: newNonLBServiceDifferentProtocols,
fgEnabled: true,
},
"Old service is nil, new non-LB service does not have different protocols, feature gate false": {
oldService: nil,
newService: newNonLBServiceSameProtocols,
fgEnabled: false,
},
"Old service is nil, new non-LB service does not have different protocols, feature gate true": {
oldService: nil,
newService: newNonLBServiceSameProtocols,
fgEnabled: true,
},
"Non-LB services, both services have different protocols, feature gate false": {
oldService: oldNonLBServiceDifferentProtocols,
newService: newNonLBServiceDifferentProtocols,
fgEnabled: false,
},
"Non-LB services, old service has same protocols, new service has different protocols, feature gate false": {
oldService: oldNonLBServiceSameProtocols,
newService: newNonLBServiceDifferentProtocols,
fgEnabled: false,
},
"Non-LB services, old service has different protocols, new service has identical protocols, feature gate false": {
oldService: oldNonLBServiceDifferentProtocols,
newService: newNonLBServiceSameProtocols,
fgEnabled: false,
},
"Non-LB services, both services have same protocols, feature gate false": {
oldService: oldNonLBServiceSameProtocols,
newService: newNonLBServiceSameProtocols,
fgEnabled: false,
},
"Non-LB services, both services have different protocols, feature gate true": {
oldService: oldNonLBServiceDifferentProtocols,
newService: newNonLBServiceDifferentProtocols,
fgEnabled: true,
},
"Non-LB services, old service has same protocols, new service has different protocols, feature gate true": {
oldService: oldNonLBServiceSameProtocols,
newService: newNonLBServiceDifferentProtocols,
fgEnabled: true,
},
"Non-LB services, old service has different protocols, new service has identical protocols, feature gate true": {
oldService: oldNonLBServiceDifferentProtocols,
newService: newNonLBServiceSameProtocols,
fgEnabled: true,
},
"Non-LB services, both services have same protocols, feature gate true": {
oldService: oldNonLBServiceSameProtocols,
newService: newNonLBServiceSameProtocols,
fgEnabled: true,
},
"LB service, neither service has different protocols, feature gate false": {
oldService: oldLBServiceSameProtocols,
newService: newLBServiceSameProtocols,
fgEnabled: false,
},
"LB service, old service does not have different protocols, new service has different protocols, feature gate false": {
oldService: oldLBServiceSameProtocols,
newService: newLBServiceDifferentProtocols,
fgEnabled: false,
expectedError: []string{`spec.ports: Invalid value: []core.ServicePort{core.ServicePort{Name:"", Protocol:"TCP", AppProtocol:(*string)(nil), Port:0, TargetPort:intstr.IntOrString{Type:0, IntVal:0, StrVal:""}, NodePort:0}, core.ServicePort{Name:"", Protocol:"UDP", AppProtocol:(*string)(nil), Port:0, TargetPort:intstr.IntOrString{Type:0, IntVal:0, StrVal:""}, NodePort:0}}: may not contain more than 1 protocol when type is 'LoadBalancer'`},
},
"LB service, old service has different protocols, new service does not have different protocols, feature gate false": {
oldService: oldLBServiceDifferentProtocols,
newService: newLBServiceSameProtocols,
fgEnabled: false,
},
"LB service, both services have different protocols, feature gate false": {
oldService: oldLBServiceDifferentProtocols,
newService: newLBServiceDifferentProtocols,
fgEnabled: false,
},
"LB service, neither service has different protocols, feature gate true": {
oldService: oldLBServiceSameProtocols,
newService: newLBServiceSameProtocols,
fgEnabled: true,
},
"LB service, old service has different protocols, new service does not have different protocols, feature gate true": {
oldService: oldLBServiceDifferentProtocols,
newService: newLBServiceSameProtocols,
fgEnabled: true,
},
"LB service, old service does not have different protocols, new service has different protocols, feature gate true": {
oldService: oldLBServiceSameProtocols,
newService: newLBServiceDifferentProtocols,
fgEnabled: true,
},
"LB service, both services have different protocols, feature gate true": {
oldService: oldLBServiceDifferentProtocols,
newService: newLBServiceDifferentProtocols,
fgEnabled: true,
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.MixedProtocolLBService, tc.fgEnabled)()
errs := validateMixedProtocolLBService(tc.newService, tc.oldService)
if len(errs) != len(tc.expectedError) {
t.Fatalf("unexpected number of errors: %v", errs)
}
for i := range errs {
if !strings.Contains(errs[i].Error(), tc.expectedError[i]) {
t.Errorf("unexpected error %d: %v", i, errs[i])
}
}
})
}
}

View File

@@ -4249,22 +4249,6 @@ func ValidateService(service *core.Service) field.ErrorList {
allErrs = append(allErrs, field.NotSupported(specPath.Child("type"), service.Spec.Type, supportedServiceType.List()))
}
if service.Spec.Type == core.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/SCTP 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 == core.ServiceTypeClusterIP {
portsPath := specPath.Child("ports")
for i := range service.Spec.Ports {

View File

@@ -10288,12 +10288,12 @@ func TestValidateServiceCreate(t *testing.T) {
numErrs: 0,
},
{
name: "invalid load balancer with mix protocol",
name: "load balancer with mix protocol",
tweakSvc: func(s *core.Service) {
s.Spec.Type = core.ServiceTypeLoadBalancer
s.Spec.Ports = append(s.Spec.Ports, core.ServicePort{Name: "q", Port: 12345, Protocol: "UDP", TargetPort: intstr.FromInt(12345)})
},
numErrs: 1,
numErrs: 0,
},
{
name: "valid 1",