Skip to content

Commit f748bd8

Browse files
author
珩轩
committed
fix the panic when the active endpoints of gateway is nil/empty
1 parent 214aa3a commit f748bd8

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

pkg/proxyengine/proxyserver/interceptor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@ func serveRequest(conn net.Conn, w http.ResponseWriter, r *http.Request) {
189189
ctx := r.Context()
190190
select {
191191
case <-stopCh:
192-
klog.Info("chunked request(%s) normally exit", r.URL.String())
192+
klog.Infof("chunked request(%s) normally exit", r.URL.String())
193193
case <-ctx.Done():
194-
klog.Info("chunked request(%s) to agent(%s) closed by cloud client, %v", r.URL.String(),
194+
klog.Infof("chunked request(%s) to agent(%s) closed by cloud client, %v", r.URL.String(),
195195
r.Header.Get(utils.RavenProxyHostHeaderKey), ctx.Err())
196196
conn.Close()
197197
}

pkg/proxyengine/proxyserver/manageheader.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,7 @@ func (h *headerManger) getAPIServerRequestDestAddress(r *http.Request) (name, ip
145145
}
146146
name, err = h.getGatewayNodeName(&node)
147147
if err != nil {
148-
return "", "", "", fmt.Errorf("gateway include node %s, has no active endpoints, error %s",
149-
node.Name, err.Error())
148+
return "", "", "", fmt.Errorf("can not find gateway node for node %s, error %s", node.Name, err.Error())
150149
}
151150
ip = getNodeIP(&node)
152151
if ip == "" {
@@ -180,8 +179,7 @@ func (h *headerManger) getNormalRequestDestAddress(r *http.Request) (name, ip, p
180179
}
181180
name, err = h.getGatewayNodeName(&node)
182181
if err != nil {
183-
return "", "", "", fmt.Errorf("gateway include node %s, has no active endpoints, error %s",
184-
node.Name, err.Error())
182+
return "", "", "", fmt.Errorf("can not find gateway node for node %s, error %s", node.Name, err.Error())
185183
}
186184
ip = getNodeIP(&node)
187185
if ip == "" {
@@ -241,6 +239,15 @@ func (h *headerManger) getGatewayNodeName(node *v1.Node) (string, error) {
241239
}
242240
return "", err
243241
}
244-
rand.Seed(time.Now().Unix())
245-
return gw.Status.ActiveEndpoints[rand.Intn(len(gw.Status.ActiveEndpoints))].NodeName, nil
242+
if gw.Status.ActiveEndpoints == nil || len(gw.Status.ActiveEndpoints) == 0 {
243+
return "", fmt.Errorf("no active endpoints for gateway %s", gwName)
244+
}
245+
names := make([]string, 0)
246+
for _, ep := range gw.Status.ActiveEndpoints {
247+
if ep.Type == v1beta1.Proxy {
248+
names = append(names, ep.NodeName)
249+
}
250+
}
251+
rand.New(rand.NewSource(time.Now().Unix()))
252+
return names[rand.Intn(len(names))], nil
246253
}

0 commit comments

Comments
 (0)