@@ -101,17 +101,21 @@ func (r *sessionAffinityRouter) setSessionHeader(ctx *types.RoutingContext, addr
101101// It also sets the session ID in the response so the client can stick to this pod next time.
102102func (r * sessionAffinityRouter ) fallbackRoute (ctx * types.RoutingContext , readyPodList types.PodList ) (string , error ) {
103103 pods := readyPodList .All ()
104-
105- selected := pods [rand .Intn (len (pods ))]
106- port := utils .GetModelPortForPod (ctx .RequestID , selected )
107- if port == 0 || selected .Status .PodIP == "" {
108- return "" , fmt .Errorf ("selected pod has no valid network address" )
104+ rand .Shuffle (len (pods ), func (i , j int ) { pods [i ], pods [j ] = pods [j ], pods [i ] })
105+
106+ for _ , selected := range pods {
107+ port := utils .GetModelPortForPod (ctx .RequestID , selected )
108+ // A routable pod must have a valid IP and port.
109+ if port == 0 || selected .Status .PodIP == "" {
110+ klog .V (4 ).Infof ("Fallback skipping pod %s with invalid " +
111+ "network address (IP: %s, Port: %d)" , selected .Name , selected .Status .PodIP , port )
112+ continue
113+ }
114+ addr := net .JoinHostPort (selected .Status .PodIP , strconv .Itoa (int (port )))
115+ ctx .SetTargetPod (selected )
116+ r .setSessionHeader (ctx , addr )
117+ klog .V (5 ).Infof ("Fallback to random pod: %s (%s)" , selected .Name , addr )
118+ return ctx .TargetAddress (), nil
109119 }
110- addr := net .JoinHostPort (selected .Status .PodIP , strconv .Itoa (int (port )))
111-
112- ctx .SetTargetPod (selected )
113- r .setSessionHeader (ctx , addr )
114- klog .V (5 ).Infof ("Fallback to random pod: %s (%s)" , selected .Name , addr )
115-
116- return ctx .TargetAddress (), nil
120+ return "" , fmt .Errorf ("no fallback pod found with a valid network address" )
117121}
0 commit comments