Skip to content

Commit

Permalink
Merge pull request #4449 from aledbf/fix-en
Browse files Browse the repository at this point in the history
Fix service type external name using the name
  • Loading branch information
k8s-ci-robot authored Aug 15, 2019
2 parents b5fecd0 + 816f4b0 commit f4da014
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
22 changes: 17 additions & 5 deletions internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,9 +835,9 @@ func (n *NGINXController) getServiceClusterEndpoint(svcKey string, backend *netw

// serviceEndpoints returns the upstream servers (Endpoints) associated with a Service.
func (n *NGINXController) serviceEndpoints(svcKey, backendPort string) ([]ingress.Endpoint, error) {
svc, err := n.store.GetService(svcKey)

var upstreams []ingress.Endpoint

svc, err := n.store.GetService(svcKey)
if err != nil {
return upstreams, err
}
Expand All @@ -848,14 +848,26 @@ func (n *NGINXController) serviceEndpoints(svcKey, backendPort string) ([]ingres
if svc.Spec.Type == apiv1.ServiceTypeExternalName {
externalPort, err := strconv.Atoi(backendPort)
if err != nil {
klog.Warningf("Only numeric ports are allowed in ExternalName Services: %q is not a valid port number.", backendPort)
return upstreams, nil
// check if the service ports have one with backendPort as name
found := false
for _, port := range svc.Spec.Ports {
if port.Name == backendPort {
externalPort = int(port.Port)
found = true
break
}
}

if !found {
klog.Errorf("service %v/%v does not have a port with name %v", svc.Namespace, svc.Namespace, backendPort)
return upstreams, nil
}
}

servicePort := apiv1.ServicePort{
Protocol: "TCP",
Port: int32(externalPort),
TargetPort: intstr.FromString(backendPort),
TargetPort: intstr.FromInt(externalPort),
}
endps := getEndpoints(svc, &servicePort, apiv1.ProtocolTCP, n.store.GetServiceEndpoints)
if len(endps) == 0 {
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/servicebackend/service_externalname.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,45 @@ var _ = framework.IngressNginxDescribe("Service Type ExternalName", func() {
Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(503))
})

It("should return 200 for service type=ExternalName using a port name", func() {
host := "echo"

svc := &core.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "httpbin",
Namespace: f.Namespace,
},
Spec: corev1.ServiceSpec{
ExternalName: "httpbin.org",
Type: corev1.ServiceTypeExternalName,
Ports: []corev1.ServicePort{
{
Name: host,
Port: 80,
TargetPort: intstr.FromInt(80),
Protocol: "TCP",
},
},
},
}
f.EnsureService(svc)

ing := framework.NewSingleIngress(host, "/", host, f.Namespace, "httpbin", 80, nil)
ing.Spec.Rules[0].HTTP.Paths[0].Backend.ServicePort = intstr.FromString(host)
f.EnsureIngress(ing)

f.WaitForNginxServer(host,
func(server string) bool {
return strings.Contains(server, "proxy_pass http://upstream_balancer;")
})

resp, _, errs := gorequest.New().
Get(f.GetURL(framework.HTTP)+"/get").
Set("Host", host).
End()
Expect(errs).Should(BeEmpty())
Expect(resp.StatusCode).Should(Equal(200))
})

})

0 comments on commit f4da014

Please sign in to comment.