Skip to content

Commit 452d8bb

Browse files
committed
fix panic due to empty DestinationRule LoadBalancerPolicy
Signed-off-by: Abirdcfly <fp544037857@gmail.com>
1 parent 739b29a commit 452d8bb

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

pkg/loadbalancer/loadbalancer.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,9 @@ func (lb *LoadBalancer) OnDestinationRuleUpdate(oldDr, dr *istioapi.DestinationR
775775
delete(lb.policyMap, svcPort)
776776
}
777777
lb.setLoadBalancerPolicy(dr, policyName, svcPort, state.endpoints)
778-
lb.policyMap[svcPort].Update(oldDr, dr)
778+
if _, exists := lb.policyMap[svcPort]; exists {
779+
lb.policyMap[svcPort].Update(oldDr, dr)
780+
}
779781
lb.policyMutex.Unlock()
780782
}
781783
}
@@ -815,8 +817,10 @@ func (lb *LoadBalancer) setLoadBalancerPolicy(dr *istioapi.DestinationRule, poli
815817
}
816818

817819
// TryPickEndpoint try to pick a service endpoint from load-balance strategy.
818-
func (lb *LoadBalancer) tryPickEndpoint(svcPort proxy.ServicePortName, sessionAffinityEnabled bool, endpoints []string,
819-
srcAddr net.Addr, netConn net.Conn, cliReq *http.Request) (string, *http.Request, bool) {
820+
func (lb *LoadBalancer) tryPickEndpoint(
821+
svcPort proxy.ServicePortName, sessionAffinityEnabled bool, endpoints []string,
822+
srcAddr net.Addr, netConn net.Conn, cliReq *http.Request,
823+
) (string, *http.Request, bool) {
820824
lb.policyMutex.Lock()
821825
defer lb.policyMutex.Unlock()
822826

@@ -835,8 +839,10 @@ func (lb *LoadBalancer) tryPickEndpoint(svcPort proxy.ServicePortName, sessionAf
835839
return endpoint, req, true
836840
}
837841

838-
func (lb *LoadBalancer) nextEndpointWithConn(svcPort proxy.ServicePortName, srcAddr net.Addr, sessionAffinityReset bool,
839-
netConn net.Conn, cliReq *http.Request) (string, *http.Request, error) {
842+
func (lb *LoadBalancer) nextEndpointWithConn(
843+
svcPort proxy.ServicePortName, srcAddr net.Addr, sessionAffinityReset bool,
844+
netConn net.Conn, cliReq *http.Request,
845+
) (string, *http.Request, error) {
840846
// Coarse locking is simple. We can get more fine-grained if/when we
841847
// can prove it matters.
842848
lb.lock.Lock()
@@ -901,8 +907,10 @@ func (lb *LoadBalancer) nextEndpointWithConn(svcPort proxy.ServicePortName, srcA
901907

902908
// TryConnectEndpoints attempts to connect to the next available endpoint for the given service, cycling
903909
// through until it is able to successfully connect, or it has tried with all timeouts in EndpointDialTimeouts.
904-
func (lb *LoadBalancer) TryConnectEndpoints(service proxy.ServicePortName, srcAddr net.Addr, protocol string,
905-
netConn net.Conn, cliReq *http.Request) (out net.Conn, err error) {
910+
func (lb *LoadBalancer) TryConnectEndpoints(
911+
service proxy.ServicePortName, srcAddr net.Addr, protocol string,
912+
netConn net.Conn, cliReq *http.Request,
913+
) (out net.Conn, err error) {
906914
sessionAffinityReset := false
907915
for _, dialTimeout := range userspace.EndpointDialTimeouts {
908916
endpoint, req, err := lb.nextEndpointWithConn(service, srcAddr, sessionAffinityReset, netConn, cliReq)

0 commit comments

Comments
 (0)