Skip to content

Commit

Permalink
Merge pull request kubernetes#108259 from carlosdamazio/refactor/roun…
Browse files Browse the repository at this point in the history
…drobin

pkg/proxy/userspace/roundrobin: Make `lb.services` nil check standardized
  • Loading branch information
k8s-ci-robot committed May 26, 2022
2 parents fa73094 + fd00b04 commit 4a2391c
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions pkg/proxy/userspace/roundrobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (lb *LoadBalancerRR) newServiceInternal(svcPort proxy.ServicePortName, affi
ttlSeconds = int(v1.DefaultClientIPServiceAffinitySeconds) //default to 3 hours if not specified. Should 0 be unlimited instead????
}

if _, exists := lb.services[svcPort]; !exists {
if state, exists := lb.services[svcPort]; !exists || state == nil {
lb.services[svcPort] = &balancerState{affinity: *newAffinityPolicy(affinityType, ttlSeconds)}
klog.V(4).InfoS("LoadBalancerRR service does not exist, created", "servicePortName", svcPort)
} else if affinityType != "" {
Expand Down Expand Up @@ -126,9 +126,10 @@ func (lb *LoadBalancerRR) ServiceHasEndpoints(svcPort proxy.ServicePortName) boo
lb.lock.RLock()
defer lb.lock.RUnlock()
state, exists := lb.services[svcPort]
// TODO: while nothing ever assigns nil to the map, *some* of the code using the map
// checks for it. The code should all follow the same convention.
return exists && state != nil && len(state.endpoints) > 0
if !exists || state == nil {
return false
}
return len(state.endpoints) > 0
}

// NextEndpoint returns a service endpoint.
Expand Down Expand Up @@ -209,7 +210,7 @@ func (lb *LoadBalancerRR) removeStaleAffinity(svcPort proxy.ServicePortName, new
}

state, exists := lb.services[svcPort]
if !exists {
if !exists || state == nil {
return
}
for _, existingEndpoint := range state.endpoints {
Expand Down Expand Up @@ -290,7 +291,7 @@ func (lb *LoadBalancerRR) OnEndpointsUpdate(oldEndpoints, endpoints *v1.Endpoint

func (lb *LoadBalancerRR) resetService(svcPort proxy.ServicePortName) {
// If the service is still around, reset but don't delete.
if state, ok := lb.services[svcPort]; ok {
if state, ok := lb.services[svcPort]; ok && state != nil {
if len(state.endpoints) > 0 {
klog.V(2).InfoS("LoadBalancerRR: Removing endpoints service", "servicePortName", svcPort)
state.endpoints = []string{}
Expand Down Expand Up @@ -330,7 +331,7 @@ func (lb *LoadBalancerRR) CleanupStaleStickySessions(svcPort proxy.ServicePortNa
defer lb.lock.Unlock()

state, exists := lb.services[svcPort]
if !exists {
if !exists || state == nil {
return
}
for ip, affinity := range state.affinity.affinityMap {
Expand Down

0 comments on commit 4a2391c

Please sign in to comment.