Merge pull request #185 from bprashanth/named_port

Match named port between container and probe
This commit is contained in:
Tim Hockin 2017-01-27 17:25:39 -08:00 committed by GitHub
commit df6f1ab5c6
2 changed files with 38 additions and 13 deletions

View file

@ -109,6 +109,29 @@ func TestProbeGetter(t *testing.T) {
} }
} }
func TestProbeGetterNamedPort(t *testing.T) {
cm := NewFakeClusterManager(DefaultClusterUID)
lbc := newLoadBalancerController(t, cm, "")
nodePortToHealthCheck := map[int64]string{
3001: "/healthz",
}
addPods(lbc, nodePortToHealthCheck, api.NamespaceDefault)
for _, p := range lbc.podLister.Indexer.List() {
pod := p.(*api.Pod)
pod.Spec.Containers[0].Ports[0].Name = "test"
pod.Spec.Containers[0].ReadinessProbe.Handler.HTTPGet.Port = intstr.IntOrString{Type: intstr.String, StrVal: "test"}
}
for p, exp := range nodePortToHealthCheck {
got, err := lbc.tr.HealthCheck(p)
if err != nil {
t.Errorf("Failed to get health check for node port %v: %v", p, err)
} else if got.RequestPath != exp {
t.Errorf("Wrong health check for node port %v, got %v expected %v", p, got.RequestPath, exp)
}
}
}
func TestProbeGetterCrossNamespace(t *testing.T) { func TestProbeGetterCrossNamespace(t *testing.T) {
cm := NewFakeClusterManager(DefaultClusterUID) cm := NewFakeClusterManager(DefaultClusterUID)
lbc := newLoadBalancerController(t, cm, "") lbc := newLoadBalancerController(t, cm, "")
@ -191,7 +214,7 @@ func addPods(lbc *LoadBalancerController, nodePortToHealthCheck map[int64]string
Spec: api.PodSpec{ Spec: api.PodSpec{
Containers: []api.Container{ Containers: []api.Container{
{ {
Ports: []api.ContainerPort{{ContainerPort: 80}}, Ports: []api.ContainerPort{{Name: "test", ContainerPort: 80}},
ReadinessProbe: &api.Probe{ ReadinessProbe: &api.Probe{
Handler: api.Handler{ Handler: api.Handler{
HTTPGet: &api.HTTPGetAction{ HTTPGet: &api.HTTPGetAction{

View file

@ -410,14 +410,6 @@ func (t *GCETranslator) ListZones() ([]string, error) {
return zones.List(), nil return zones.List(), nil
} }
// isPortEqual compares the given IntOrString ports
func isPortEqual(port, targetPort intstr.IntOrString) bool {
if targetPort.Type == intstr.Int {
return port.IntVal == targetPort.IntVal
}
return port.StrVal == targetPort.StrVal
}
// geHTTPProbe returns the http readiness probe from the first container // geHTTPProbe returns the http readiness probe from the first container
// that matches targetPort, from the set of pods matching the given labels. // that matches targetPort, from the set of pods matching the given labels.
func (t *GCETranslator) getHTTPProbe(svc api.Service, targetPort intstr.IntOrString) (*api.Probe, error) { func (t *GCETranslator) getHTTPProbe(svc api.Service, targetPort intstr.IntOrString) (*api.Probe, error) {
@ -443,11 +435,21 @@ func (t *GCETranslator) getHTTPProbe(svc api.Service, targetPort intstr.IntOrStr
continue continue
} }
for _, p := range c.Ports { for _, p := range c.Ports {
cPort := intstr.IntOrString{IntVal: p.ContainerPort, StrVal: p.Name} if targetPort.Type == intstr.Int && targetPort.IntVal == p.ContainerPort ||
if isPortEqual(cPort, targetPort) { targetPort.Type == intstr.String && targetPort.StrVal == p.Name {
if isPortEqual(c.ReadinessProbe.Handler.HTTPGet.Port, targetPort) {
readinessProbePort := c.ReadinessProbe.Handler.HTTPGet.Port
switch readinessProbePort.Type {
case intstr.Int:
if readinessProbePort.IntVal == p.ContainerPort {
return c.ReadinessProbe, nil return c.ReadinessProbe, nil
} }
case intstr.String:
if readinessProbePort.StrVal == p.Name {
return c.ReadinessProbe, nil
}
}
glog.Infof("%v: found matching targetPort on container %v, but not on readinessProbe (%+v)", glog.Infof("%v: found matching targetPort on container %v, but not on readinessProbe (%+v)",
logStr, c.Name, c.ReadinessProbe.Handler.HTTPGet.Port) logStr, c.Name, c.ReadinessProbe.Handler.HTTPGet.Port)
} }