Skip to content

Commit 32b8a3a

Browse files
committed
Return unique addresses from service
1 parent 12150e3 commit 32b8a3a

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

internal/ingress/status/status.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/pkg/errors"
28+
"k8s.io/apimachinery/pkg/util/sets"
2829
"k8s.io/klog/v2"
2930

3031
pool "gopkg.in/go-playground/pool.v3"
@@ -339,26 +340,26 @@ func statusAddressFromService(service string, kubeClient clientset.Interface) ([
339340
case apiv1.ServiceTypeClusterIP:
340341
return []string{svc.Spec.ClusterIP}, nil
341342
case apiv1.ServiceTypeNodePort:
342-
addresses := []string{}
343+
addresses := sets.NewString()
343344
if svc.Spec.ExternalIPs != nil {
344-
addresses = append(addresses, svc.Spec.ExternalIPs...)
345+
addresses.Insert(svc.Spec.ExternalIPs...)
345346
} else {
346-
addresses = append(addresses, svc.Spec.ClusterIP)
347+
addresses.Insert(svc.Spec.ClusterIP)
347348
}
348-
return addresses, nil
349+
return addresses.List(), nil
349350
case apiv1.ServiceTypeLoadBalancer:
350-
addresses := []string{}
351+
addresses := sets.NewString()
351352
for _, ip := range svc.Status.LoadBalancer.Ingress {
352353
if ip.IP == "" {
353-
addresses = append(addresses, ip.Hostname)
354+
addresses.Insert(ip.Hostname)
354355
} else {
355-
addresses = append(addresses, ip.IP)
356+
addresses.Insert(ip.IP)
356357
}
357358
}
358359

359-
addresses = append(addresses, svc.Spec.ExternalIPs...)
360+
addresses.Insert(svc.Spec.ExternalIPs...)
360361

361-
return addresses, nil
362+
return addresses.List(), nil
362363
}
363364

364365
return nil, fmt.Errorf("unable to extract IP address/es from service %v", service)

internal/ingress/status/status_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,34 @@ func TestRunningAddresessWithPublishService(t *testing.T) {
483483
[]string{"10.0.0.1", "foo"},
484484
false,
485485
},
486+
"service type LoadBalancer with same externalIP and ingress IP": {
487+
testclient.NewSimpleClientset(
488+
&apiv1.ServiceList{Items: []apiv1.Service{
489+
{
490+
ObjectMeta: metav1.ObjectMeta{
491+
Name: "foo",
492+
Namespace: apiv1.NamespaceDefault,
493+
},
494+
Spec: apiv1.ServiceSpec{
495+
Type: apiv1.ServiceTypeLoadBalancer,
496+
ExternalIPs: []string{"10.0.0.1"},
497+
},
498+
Status: apiv1.ServiceStatus{
499+
LoadBalancer: apiv1.LoadBalancerStatus{
500+
Ingress: []apiv1.LoadBalancerIngress{
501+
{
502+
IP: "10.0.0.1",
503+
},
504+
},
505+
},
506+
},
507+
},
508+
},
509+
},
510+
),
511+
[]string{"10.0.0.1"},
512+
false,
513+
},
486514
"invalid service type": {
487515
testclient.NewSimpleClientset(
488516
&apiv1.ServiceList{Items: []apiv1.Service{

0 commit comments

Comments
 (0)