提交 3ecce3ff 编写于 作者: P Piotr Bryk

Merge pull request #369 from maciaszczykm/localhost-endpoint

Add external endpoint for cluster IP services
......@@ -330,9 +330,7 @@ func getExternalEndpoints(replicationController api.ReplicationController, pods
if service.Spec.Type == api.ServiceTypeNodePort {
externalEndpoints = getNodePortEndpoints(replicationControllerPods, service, getNodeFn)
}
if service.Spec.Type == api.ServiceTypeLoadBalancer {
} else if service.Spec.Type == api.ServiceTypeLoadBalancer {
for _, ingress := range service.Status.LoadBalancer.Ingress {
externalEndpoints = append(externalEndpoints, getExternalEndpoint(ingress,
service.Spec.Ports))
......@@ -343,6 +341,28 @@ func getExternalEndpoints(replicationController api.ReplicationController, pods
}
}
if len(externalEndpoints) == 0 && (service.Spec.Type == api.ServiceTypeNodePort ||
service.Spec.Type == api.ServiceTypeLoadBalancer) {
externalEndpoints = getLocalhostEndpoints(service)
}
return externalEndpoints
}
// Returns localhost endpoints for specified node port or load balancer service.
func getLocalhostEndpoints(service api.Service) []Endpoint {
var externalEndpoints []Endpoint
for _, port := range service.Spec.Ports {
externalEndpoints = append(externalEndpoints, Endpoint{
Host: "localhost",
Ports: []ServicePort{
{
Protocol: port.Protocol,
Port: port.NodePort,
},
},
})
}
return externalEndpoints
}
......
......@@ -493,6 +493,57 @@ func TestGetNodePortEndpoints(t *testing.T) {
}
}
func TestGetLocalhostEndpoints(t *testing.T) {
cases := []struct {
service api.Service
expected []Endpoint
}{
{
api.Service{
Spec: api.ServiceSpec{
Ports: []api.ServicePort{
{
Protocol: "TCP",
NodePort: 30100,
},
{
Protocol: "TCP",
NodePort: 30101,
},
},
},
},
[]Endpoint{
{
Host: "localhost",
Ports: []ServicePort{
{
Port: 30100,
Protocol: "TCP",
},
},
},
{
Host: "localhost",
Ports: []ServicePort{
{
Port: 30101,
Protocol: "TCP",
},
},
},
},
},
}
for _, c := range cases {
actual := getLocalhostEndpoints(c.service)
if !reflect.DeepEqual(actual, c.expected) {
t.Errorf("getLocalhostEndpoints(%+v) == %+v, expected %+v", c.service, actual,
c.expected)
}
}
}
func TestGetInternalEndpoint(t *testing.T) {
cases := []struct {
serviceName, namespace string
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册