Skip to content

Commit 67db88c

Browse files
committed
fix: r
Signed-off-by: ashing <[email protected]>
1 parent e6b5752 commit 67db88c

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

internal/controller/ingress_controller.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"reflect"
7-
"strings"
87

98
"github.com/api7/api7-ingress-controller/internal/controller/config"
109
"github.com/api7/api7-ingress-controller/internal/controller/indexer"
@@ -380,29 +379,36 @@ func (r *IngressReconciler) updateStatus(ctx context.Context, ingress *networkin
380379
publishService := config.GetIngressPublishService()
381380
if publishService != "" {
382381
// 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 {
385384
return fmt.Errorf("invalid ingress-publish-service format: %s, expected format: namespace/name", publishService)
386385
}
386+
if namespace == "" {
387+
namespace = ingress.Namespace
388+
}
387389

388390
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 {
390392
return fmt.Errorf("failed to get publish service %s: %w", publishService, err)
391393
}
392394

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+
}
404409
}
405410
}
411+
406412
} else {
407413
// 3. if the PublishService is not configured, try to use the Gateway's Addresses
408414
cfg := config.GetFirstGatewayConfig()

internal/controller/utils.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,3 +743,19 @@ func getListenerStatus(
743743

744744
return statusArray, nil
745745
}
746+
747+
// SplitMetaNamespaceKey returns the namespace and name that
748+
// MetaNamespaceKeyFunc encoded into key.
749+
func SplitMetaNamespaceKey(key string) (namespace, name string, err error) {
750+
parts := strings.Split(key, "/")
751+
switch len(parts) {
752+
case 1:
753+
// name only, no namespace
754+
return "", parts[0], nil
755+
case 2:
756+
// namespace and name
757+
return parts[0], parts[1], nil
758+
}
759+
760+
return "", "", fmt.Errorf("unexpected key format: %q", key)
761+
}

0 commit comments

Comments
 (0)