Skip to content

Commit

Permalink
Merge pull request #846 from justinsb/serviceport
Browse files Browse the repository at this point in the history
Match ServicePort to Endpoints by Name
  • Loading branch information
aledbf authored Jun 13, 2017
2 parents cdbf2aa + cacc7bc commit e258ee1
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions core/pkg/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func (ic *GenericController) getStreamServices(configmapName string, proto api.P
glog.V(3).Infof("searching service %v/%v endpoints using the name '%v'", svcNs, svcName, svcPort)
for _, sp := range svc.Spec.Ports {
if sp.Name == svcPort {
endps = ic.getEndpoints(svc, sp.TargetPort, proto, &healthcheck.Upstream{})
endps = ic.getEndpoints(svc, &sp, proto, &healthcheck.Upstream{})
break
}
}
Expand All @@ -498,7 +498,7 @@ func (ic *GenericController) getStreamServices(configmapName string, proto api.P
glog.V(3).Infof("searching service %v/%v endpoints using the target port '%v'", svcNs, svcName, targetPort)
for _, sp := range svc.Spec.Ports {
if sp.Port == int32(targetPort) {
endps = ic.getEndpoints(svc, sp.TargetPort, proto, &healthcheck.Upstream{})
endps = ic.getEndpoints(svc, &sp, proto, &healthcheck.Upstream{})
break
}
}
Expand Down Expand Up @@ -548,7 +548,7 @@ func (ic *GenericController) getDefaultUpstream() *ingress.Backend {
}

svc := svcObj.(*api.Service)
endps := ic.getEndpoints(svc, svc.Spec.Ports[0].TargetPort, api.ProtocolTCP, &healthcheck.Upstream{})
endps := ic.getEndpoints(svc, &svc.Spec.Ports[0], api.ProtocolTCP, &healthcheck.Upstream{})
if len(endps) == 0 {
glog.Warningf("service %v does not have any active endpoints", svcKey)
endps = []ingress.Endpoint{newDefaultServer()}
Expand Down Expand Up @@ -841,7 +841,7 @@ func (ic *GenericController) serviceEndpoints(svcKey, backendPort string,
servicePort.TargetPort.String() == backendPort ||
servicePort.Name == backendPort {

endps := ic.getEndpoints(svc, servicePort.TargetPort, api.ProtocolTCP, hz)
endps := ic.getEndpoints(svc, &servicePort, api.ProtocolTCP, hz)
if len(endps) == 0 {
glog.Warningf("service %v does not have any active endpoints", svcKey)
}
Expand Down Expand Up @@ -1026,7 +1026,7 @@ func (ic *GenericController) createServers(data []interface{},
// getEndpoints returns a list of <endpoint ip>:<port> for a given service/target port combination.
func (ic *GenericController) getEndpoints(
s *api.Service,
servicePort intstr.IntOrString,
servicePort *api.ServicePort,
proto api.Protocol,
hz *healthcheck.Upstream) []ingress.Endpoint {

Expand All @@ -1039,7 +1039,7 @@ func (ic *GenericController) getEndpoints(

// ExternalName services
if s.Spec.Type == api.ServiceTypeExternalName {
targetPort := servicePort.IntValue()
targetPort := servicePort.TargetPort.IntValue()
// check for invalid port value
if targetPort <= 0 {
return upsServers
Expand Down Expand Up @@ -1069,15 +1069,11 @@ func (ic *GenericController) getEndpoints(

var targetPort int32

switch servicePort.Type {
case intstr.Int:
if int(epPort.Port) == servicePort.IntValue() {
targetPort = epPort.Port
}
case intstr.String:
if epPort.Name == servicePort.StrVal {
targetPort = epPort.Port
}
if servicePort.Name == "" {
// ServicePort.Name is optional if there is only one port
targetPort = epPort.Port
} else if servicePort.Name == epPort.Name {
targetPort = epPort.Port
}

// check for invalid port value
Expand Down

0 comments on commit e258ee1

Please sign in to comment.