```
diff --git a/pkg/registry/core/service/storage/rest.go
b/pkg/registry/core/service/storage/rest.go
index f24c79b2d26..c696f7d91a6 100644
--- a/pkg/registry/core/service/storage/rest.go
+++ b/pkg/registry/core/service/storage/rest.go
@@ -279,10 +279,16 @@ func (al *RESTAllocStuff)
releaseAllocatedResources(svc *api.Service) {
}
func shouldAllocateNodePorts(service *api.Service) bool {
- if utilfeature.DefaultFeatureGate.Enabled(features.ServiceLBNodePortControl) {
- return *service.Spec.AllocateLoadBalancerNodePorts
+ if service.Spec.Type == api.ServiceTypeNodePort {
+ return true
}
- return true
+ if service.Spec.Type == api.ServiceTypeLoadBalancer {
+ if utilfeature.DefaultFeatureGate.Enabled(features.ServiceLBNodePortControl) {
+ return *service.Spec.AllocateLoadBalancerNodePorts
+ }
+ return true
+ }
+ return false
}
// externalTrafficPolicyUpdate adjusts ExternalTrafficPolicy during
service update if needed.
@@ -424,8 +430,7 @@ func (rs *REST) Update(ctx context.Context, name
string, objInfo rest.UpdatedObj
releaseNodePorts(oldService, nodePortOp)
}
// Update service from any type to NodePort or LoadBalancer, should
update NodePort.
- if service.Spec.Type == api.ServiceTypeNodePort ||
- (service.Spec.Type == api.ServiceTypeLoadBalancer &&
shouldAllocateNodePorts(service)) {
+ if service.Spec.Type == api.ServiceTypeNodePort || service.Spec.Type
== api.ServiceTypeLoadBalancer {
if err := updateNodePorts(oldService, service, nodePortOp); err != nil {
return nil, false, err
}
@@ -1163,8 +1168,7 @@ func (al *RESTAllocStuff)
allocServiceNodePortsNew(service *api.Service, dryRun
// TODO: This creates nodePorts if needed. In the future nodePorts
may be cleared if *never* used.
// But for now we stick to the KEP "don't allocate new node ports
but do not deallocate existing node ports if set"
- if service.Spec.Type == api.ServiceTypeNodePort ||
- (service.Spec.Type == api.ServiceTypeLoadBalancer &&
shouldAllocateNodePorts(service)) {
+ if service.Spec.Type == api.ServiceTypeNodePort || service.Spec.Type
== api.ServiceTypeLoadBalancer {
if err := initNodePorts(service, nodePortOp); err != nil {
txn.Revert()
return nil, err
@@ -1186,6 +1190,9 @@ func initNodePorts(service *api.Service,
nodePortOp *portallocator.PortAllocatio
svcPortToNodePort := map[int]int{}
for i := range service.Spec.Ports {
servicePort := &service.Spec.Ports[i]
+ if servicePort.NodePort == 0 && !shouldAllocateNodePorts(service) {
+ continue
+ }
allocatedNodePort := svcPortToNodePort[int(servicePort.Port)]
if allocatedNodePort == 0 {
// This will only scan forward in the service.Spec.Ports list
because any matches
@@ -1238,6 +1245,9 @@ func updateNodePorts(oldService, newService
*api.Service, nodePortOp *portalloca
for i := range newService.Spec.Ports {
servicePort := &newService.Spec.Ports[i]
+ if servicePort.NodePort == 0 && !shouldAllocateNodePorts(newService) {
+ continue
+ }
nodePort := ServiceNodePort{Protocol: servicePort.Protocol,
NodePort: servicePort.NodePort}
if nodePort.NodePort != 0 {
if !containsNumber(oldNodePortsNumbers, int(nodePort.NodePort)) &&
!portAllocated[int(nodePort.NodePort)] {
```