Skip to content

Commit 3ecce3f

Browse files
committed
Merge pull request #369 from maciaszczykm/localhost-endpoint
Add external endpoint for cluster IP services
2 parents 127183e + c024188 commit 3ecce3f

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/app/backend/replicationcontrollerdetail.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,7 @@ func getExternalEndpoints(replicationController api.ReplicationController, pods
330330

331331
if service.Spec.Type == api.ServiceTypeNodePort {
332332
externalEndpoints = getNodePortEndpoints(replicationControllerPods, service, getNodeFn)
333-
}
334-
335-
if service.Spec.Type == api.ServiceTypeLoadBalancer {
333+
} else if service.Spec.Type == api.ServiceTypeLoadBalancer {
336334
for _, ingress := range service.Status.LoadBalancer.Ingress {
337335
externalEndpoints = append(externalEndpoints, getExternalEndpoint(ingress,
338336
service.Spec.Ports))
@@ -343,6 +341,28 @@ func getExternalEndpoints(replicationController api.ReplicationController, pods
343341
}
344342
}
345343

344+
if len(externalEndpoints) == 0 && (service.Spec.Type == api.ServiceTypeNodePort ||
345+
service.Spec.Type == api.ServiceTypeLoadBalancer) {
346+
externalEndpoints = getLocalhostEndpoints(service)
347+
}
348+
349+
return externalEndpoints
350+
}
351+
352+
// Returns localhost endpoints for specified node port or load balancer service.
353+
func getLocalhostEndpoints(service api.Service) []Endpoint {
354+
var externalEndpoints []Endpoint
355+
for _, port := range service.Spec.Ports {
356+
externalEndpoints = append(externalEndpoints, Endpoint{
357+
Host: "localhost",
358+
Ports: []ServicePort{
359+
{
360+
Protocol: port.Protocol,
361+
Port: port.NodePort,
362+
},
363+
},
364+
})
365+
}
346366
return externalEndpoints
347367
}
348368

src/test/backend/replicationcontrollerdetail_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,57 @@ func TestGetNodePortEndpoints(t *testing.T) {
493493
}
494494
}
495495

496+
func TestGetLocalhostEndpoints(t *testing.T) {
497+
cases := []struct {
498+
service api.Service
499+
expected []Endpoint
500+
}{
501+
{
502+
api.Service{
503+
Spec: api.ServiceSpec{
504+
Ports: []api.ServicePort{
505+
{
506+
Protocol: "TCP",
507+
NodePort: 30100,
508+
},
509+
{
510+
Protocol: "TCP",
511+
NodePort: 30101,
512+
},
513+
},
514+
},
515+
},
516+
[]Endpoint{
517+
{
518+
Host: "localhost",
519+
Ports: []ServicePort{
520+
{
521+
Port: 30100,
522+
Protocol: "TCP",
523+
},
524+
},
525+
},
526+
{
527+
Host: "localhost",
528+
Ports: []ServicePort{
529+
{
530+
Port: 30101,
531+
Protocol: "TCP",
532+
},
533+
},
534+
},
535+
},
536+
},
537+
}
538+
for _, c := range cases {
539+
actual := getLocalhostEndpoints(c.service)
540+
if !reflect.DeepEqual(actual, c.expected) {
541+
t.Errorf("getLocalhostEndpoints(%+v) == %+v, expected %+v", c.service, actual,
542+
c.expected)
543+
}
544+
}
545+
}
546+
496547
func TestGetInternalEndpoint(t *testing.T) {
497548
cases := []struct {
498549
serviceName, namespace string

0 commit comments

Comments
 (0)