Merge pull request #6097 from hazim1093/unique-addresses
Return unique addresses from service
This commit is contained in:
commit
2d44f62e1b
2 changed files with 38 additions and 9 deletions
|
@ -25,6 +25,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
pool "gopkg.in/go-playground/pool.v3"
|
pool "gopkg.in/go-playground/pool.v3"
|
||||||
|
@ -339,26 +340,26 @@ func statusAddressFromService(service string, kubeClient clientset.Interface) ([
|
||||||
case apiv1.ServiceTypeClusterIP:
|
case apiv1.ServiceTypeClusterIP:
|
||||||
return []string{svc.Spec.ClusterIP}, nil
|
return []string{svc.Spec.ClusterIP}, nil
|
||||||
case apiv1.ServiceTypeNodePort:
|
case apiv1.ServiceTypeNodePort:
|
||||||
addresses := []string{}
|
addresses := sets.NewString()
|
||||||
if svc.Spec.ExternalIPs != nil {
|
if svc.Spec.ExternalIPs != nil {
|
||||||
addresses = append(addresses, svc.Spec.ExternalIPs...)
|
addresses.Insert(svc.Spec.ExternalIPs...)
|
||||||
} else {
|
} else {
|
||||||
addresses = append(addresses, svc.Spec.ClusterIP)
|
addresses.Insert(svc.Spec.ClusterIP)
|
||||||
}
|
}
|
||||||
return addresses, nil
|
return addresses.List(), nil
|
||||||
case apiv1.ServiceTypeLoadBalancer:
|
case apiv1.ServiceTypeLoadBalancer:
|
||||||
addresses := []string{}
|
addresses := sets.NewString()
|
||||||
for _, ip := range svc.Status.LoadBalancer.Ingress {
|
for _, ip := range svc.Status.LoadBalancer.Ingress {
|
||||||
if ip.IP == "" {
|
if ip.IP == "" {
|
||||||
addresses = append(addresses, ip.Hostname)
|
addresses.Insert(ip.Hostname)
|
||||||
} else {
|
} else {
|
||||||
addresses = append(addresses, ip.IP)
|
addresses.Insert(ip.IP)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addresses = append(addresses, svc.Spec.ExternalIPs...)
|
addresses.Insert(svc.Spec.ExternalIPs...)
|
||||||
|
|
||||||
return addresses, nil
|
return addresses.List(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("unable to extract IP address/es from service %v", service)
|
return nil, fmt.Errorf("unable to extract IP address/es from service %v", service)
|
||||||
|
|
|
@ -483,6 +483,34 @@ func TestRunningAddresessWithPublishService(t *testing.T) {
|
||||||
[]string{"10.0.0.1", "foo"},
|
[]string{"10.0.0.1", "foo"},
|
||||||
false,
|
false,
|
||||||
},
|
},
|
||||||
|
"service type LoadBalancer with same externalIP and ingress IP": {
|
||||||
|
testclient.NewSimpleClientset(
|
||||||
|
&apiv1.ServiceList{Items: []apiv1.Service{
|
||||||
|
{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "foo",
|
||||||
|
Namespace: apiv1.NamespaceDefault,
|
||||||
|
},
|
||||||
|
Spec: apiv1.ServiceSpec{
|
||||||
|
Type: apiv1.ServiceTypeLoadBalancer,
|
||||||
|
ExternalIPs: []string{"10.0.0.1"},
|
||||||
|
},
|
||||||
|
Status: apiv1.ServiceStatus{
|
||||||
|
LoadBalancer: apiv1.LoadBalancerStatus{
|
||||||
|
Ingress: []apiv1.LoadBalancerIngress{
|
||||||
|
{
|
||||||
|
IP: "10.0.0.1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
[]string{"10.0.0.1"},
|
||||||
|
false,
|
||||||
|
},
|
||||||
"invalid service type": {
|
"invalid service type": {
|
||||||
testclient.NewSimpleClientset(
|
testclient.NewSimpleClientset(
|
||||||
&apiv1.ServiceList{Items: []apiv1.Service{
|
&apiv1.ServiceList{Items: []apiv1.Service{
|
||||||
|
|
Loading…
Reference in a new issue