@@ -72,14 +72,24 @@ func getNodeIPs(k8sComponent *k8s.Component) ([]string, error) {
7272}
7373
7474// generateAccessURLs 生成访问地址列表
75- func generateAccessURLs (nodeIPs []string , ports []model.LoadBalancerPort ) []string {
75+ func generateAccessURLs (ips []string , ports []model.LoadBalancerPort , useServicePort bool ) []string {
7676 var accessURLs []string
77- for _ , nodeIP := range nodeIPs {
77+ for _ , ip := range ips {
7878 for _ , port := range ports {
79- if port .NodePort > 0 {
80- url := fmt .Sprintf ("%s:%d" , nodeIP , port .NodePort )
81- accessURLs = append (accessURLs , url )
79+ var portNum int
80+ if useServicePort {
81+ // 使用 LoadBalancer 的服务端口
82+ portNum = port .Port
83+ } else {
84+ // 使用 NodePort
85+ if port .NodePort > 0 {
86+ portNum = int (port .NodePort )
87+ } else {
88+ continue
89+ }
8290 }
91+ url := fmt .Sprintf ("%s:%d" , ip , portNum )
92+ accessURLs = append (accessURLs , url )
8393 }
8494 }
8595 return accessURLs
@@ -208,17 +218,33 @@ func (g Struct) CreateLoadBalancer(w http.ResponseWriter, r *http.Request) {
208218 })
209219 }
210220
211- // 获取节点IP列表
212- nodeIPs , err := getNodeIPs (k8s .Default ())
213- if err != nil {
214- logrus .Warnf ("get node IPs error %s" , err .Error ())
215- // 不影响主要功能,继续执行
221+ // 生成访问地址 - 优先使用 LoadBalancer Ingress IP
222+ var accessURLs []string
223+ var ingressIPs []string
224+
225+ // 检查是否已有 LoadBalancer Ingress IP
226+ if len (createdService .Status .LoadBalancer .Ingress ) > 0 {
227+ for _ , ingress := range createdService .Status .LoadBalancer .Ingress {
228+ if ingress .IP != "" {
229+ ingressIPs = append (ingressIPs , ingress .IP )
230+ }
231+ if ingress .Hostname != "" {
232+ ingressIPs = append (ingressIPs , ingress .Hostname )
233+ }
234+ }
216235 }
217236
218- // 生成访问地址
219- var accessURLs []string
220- if len (nodeIPs ) > 0 {
221- accessURLs = generateAccessURLs (nodeIPs , responsePorts )
237+ if len (ingressIPs ) > 0 {
238+ // 使用 LoadBalancer Ingress IP 和服务端口
239+ accessURLs = generateAccessURLs (ingressIPs , responsePorts , true )
240+ } else {
241+ // 回退到使用节点IP和NodePort
242+ nodeIPs , err := getNodeIPs (k8s .Default ())
243+ if err != nil {
244+ logrus .Warnf ("get node IPs error %s" , err .Error ())
245+ } else if len (nodeIPs ) > 0 {
246+ accessURLs = generateAccessURLs (nodeIPs , responsePorts , false )
247+ }
222248 }
223249
224250 // 构造响应
@@ -305,10 +331,28 @@ func (g Struct) GetLoadBalancer(w http.ResponseWriter, r *http.Request) {
305331 })
306332 }
307333
308- // 生成访问地址
334+ // 生成访问地址 - 优先使用 LoadBalancer Ingress IP
309335 var accessURLs []string
310- if len (nodeIPs ) > 0 {
311- accessURLs = generateAccessURLs (nodeIPs , servicePorts )
336+ var ingressIPs []string
337+
338+ // 检查是否已有 LoadBalancer Ingress IP
339+ if len (service .Status .LoadBalancer .Ingress ) > 0 {
340+ for _ , ingress := range service .Status .LoadBalancer .Ingress {
341+ if ingress .IP != "" {
342+ ingressIPs = append (ingressIPs , ingress .IP )
343+ }
344+ if ingress .Hostname != "" {
345+ ingressIPs = append (ingressIPs , ingress .Hostname )
346+ }
347+ }
348+ }
349+
350+ if len (ingressIPs ) > 0 {
351+ // 使用 LoadBalancer Ingress IP 和服务端口
352+ accessURLs = generateAccessURLs (ingressIPs , servicePorts , true )
353+ } else if len (nodeIPs ) > 0 {
354+ // 回退到使用节点IP和NodePort
355+ accessURLs = generateAccessURLs (nodeIPs , servicePorts , false )
312356 }
313357
314358 response := & model.LoadBalancerResponse {
@@ -509,16 +553,33 @@ func (g Struct) UpdateLoadBalancer(w http.ResponseWriter, r *http.Request) {
509553 })
510554 }
511555
512- // 获取节点IP列表
513- nodeIPs , err := getNodeIPs (k8s .Default ())
514- if err != nil {
515- logrus .Warnf ("get node IPs error %s" , err .Error ())
556+ // 生成访问地址 - 优先使用 LoadBalancer Ingress IP
557+ var accessURLs []string
558+ var ingressIPs []string
559+
560+ // 检查是否已有 LoadBalancer Ingress IP
561+ if len (updatedService .Status .LoadBalancer .Ingress ) > 0 {
562+ for _ , ingress := range updatedService .Status .LoadBalancer .Ingress {
563+ if ingress .IP != "" {
564+ ingressIPs = append (ingressIPs , ingress .IP )
565+ }
566+ if ingress .Hostname != "" {
567+ ingressIPs = append (ingressIPs , ingress .Hostname )
568+ }
569+ }
516570 }
517571
518- // 生成访问地址
519- var accessURLs []string
520- if len (nodeIPs ) > 0 {
521- accessURLs = generateAccessURLs (nodeIPs , updatedPorts )
572+ if len (ingressIPs ) > 0 {
573+ // 使用 LoadBalancer Ingress IP 和服务端口
574+ accessURLs = generateAccessURLs (ingressIPs , updatedPorts , true )
575+ } else {
576+ // 回退到使用节点IP和NodePort
577+ nodeIPs , err := getNodeIPs (k8s .Default ())
578+ if err != nil {
579+ logrus .Warnf ("get node IPs error %s" , err .Error ())
580+ } else if len (nodeIPs ) > 0 {
581+ accessURLs = generateAccessURLs (nodeIPs , updatedPorts , false )
582+ }
522583 }
523584
524585 // 构造响应
0 commit comments