|
4 | 4 | "context" |
5 | 5 | "fmt" |
6 | 6 | "reflect" |
7 | | - "strings" |
8 | 7 |
|
9 | 8 | "github.com/api7/api7-ingress-controller/internal/controller/config" |
10 | 9 | "github.com/api7/api7-ingress-controller/internal/controller/indexer" |
@@ -380,29 +379,36 @@ func (r *IngressReconciler) updateStatus(ctx context.Context, ingress *networkin |
380 | 379 | publishService := config.GetIngressPublishService() |
381 | 380 | if publishService != "" { |
382 | 381 | // parse the namespace/name format |
383 | | - parts := strings.Split(publishService, "/") |
384 | | - if len(parts) != 2 { |
| 382 | + namespace, name, err := SplitMetaNamespaceKey(publishService) |
| 383 | + if err != nil { |
385 | 384 | return fmt.Errorf("invalid ingress-publish-service format: %s, expected format: namespace/name", publishService) |
386 | 385 | } |
| 386 | + if namespace == "" { |
| 387 | + namespace = ingress.Namespace |
| 388 | + } |
387 | 389 |
|
388 | 390 | svc := &corev1.Service{} |
389 | | - if err := r.Get(ctx, client.ObjectKey{Namespace: parts[0], Name: parts[1]}, svc); err != nil { |
| 391 | + if err := r.Get(ctx, client.ObjectKey{Namespace: namespace, Name: name}, svc); err != nil { |
390 | 392 | return fmt.Errorf("failed to get publish service %s: %w", publishService, err) |
391 | 393 | } |
392 | 394 |
|
393 | | - // get the LoadBalancer IP and Hostname of the service |
394 | | - for _, ip := range svc.Status.LoadBalancer.Ingress { |
395 | | - if ip.IP != "" { |
396 | | - loadBalancerStatus.Ingress = append(loadBalancerStatus.Ingress, networkingv1.IngressLoadBalancerIngress{ |
397 | | - IP: ip.IP, |
398 | | - }) |
399 | | - } |
400 | | - if ip.Hostname != "" { |
401 | | - loadBalancerStatus.Ingress = append(loadBalancerStatus.Ingress, networkingv1.IngressLoadBalancerIngress{ |
402 | | - Hostname: ip.Hostname, |
403 | | - }) |
| 395 | + if svc.Spec.Type != corev1.ServiceTypeLoadBalancer { |
| 396 | + r.Log.Error(fmt.Errorf("publish service %s is not a load balancer service", publishService), "publish service is not a load balancer service") |
| 397 | + // get the LoadBalancer IP and Hostname of the service |
| 398 | + for _, ip := range svc.Status.LoadBalancer.Ingress { |
| 399 | + if ip.IP != "" { |
| 400 | + loadBalancerStatus.Ingress = append(loadBalancerStatus.Ingress, networkingv1.IngressLoadBalancerIngress{ |
| 401 | + IP: ip.IP, |
| 402 | + }) |
| 403 | + } |
| 404 | + if ip.Hostname != "" { |
| 405 | + loadBalancerStatus.Ingress = append(loadBalancerStatus.Ingress, networkingv1.IngressLoadBalancerIngress{ |
| 406 | + Hostname: ip.Hostname, |
| 407 | + }) |
| 408 | + } |
404 | 409 | } |
405 | 410 | } |
| 411 | + |
406 | 412 | } else { |
407 | 413 | // 3. if the PublishService is not configured, try to use the Gateway's Addresses |
408 | 414 | cfg := config.GetFirstGatewayConfig() |
|
0 commit comments