diff --git a/pkg/service/endpoints_controller.go b/pkg/service/endpoints_controller.go index 75b597a4ad5..6fd0d963467 100644 --- a/pkg/service/endpoints_controller.go +++ b/pkg/service/endpoints_controller.go @@ -145,8 +145,11 @@ func findPort(manifest *api.ContainerManifest, portName util.IntOrString) (int, switch portName.Kind { case util.IntstrString: - if len(portName.StrVal) == 0 && firstContainerPort != -1 { - return firstContainerPort, nil + if len(portName.StrVal) == 0 { + if firstContainerPort != 0 { + return firstContainerPort, nil + } + break } name := portName.StrVal for _, container := range manifest.Containers { @@ -157,8 +160,11 @@ func findPort(manifest *api.ContainerManifest, portName util.IntOrString) (int, } } case util.IntstrInt: - if portName.IntVal == 0 && firstContainerPort != -1 { - return firstContainerPort, nil + if portName.IntVal == 0 { + if firstContainerPort != 0 { + return firstContainerPort, nil + } + break } return portName.IntVal, nil } diff --git a/pkg/service/endpoints_controller_test.go b/pkg/service/endpoints_controller_test.go index 0850a38c495..e456bd3c97a 100644 --- a/pkg/service/endpoints_controller_test.go +++ b/pkg/service/endpoints_controller_test.go @@ -79,50 +79,77 @@ func TestFindPort(t *testing.T) { }, }, } + emptyPortsManifest := api.ContainerManifest{ + Containers: []api.Container{ + { + Ports: []api.Port{}, + }, + }, + } tests := []struct { + manifest api.ContainerManifest portName util.IntOrString wport int werr bool }{ { + manifest, util.IntOrString{Kind: util.IntstrString, StrVal: "foo"}, 8080, false, }, { + manifest, util.IntOrString{Kind: util.IntstrString, StrVal: "bar"}, 8000, false, }, { + manifest, util.IntOrString{Kind: util.IntstrInt, IntVal: 8000}, 8000, false, }, { + manifest, util.IntOrString{Kind: util.IntstrInt, IntVal: 7000}, 7000, false, }, { + manifest, util.IntOrString{Kind: util.IntstrString, StrVal: "baz"}, 0, true, }, { + manifest, util.IntOrString{Kind: util.IntstrString, StrVal: ""}, 8080, false, }, { - util.IntOrString{}, + manifest, + util.IntOrString{Kind: util.IntstrInt, IntVal: 0}, 8080, false, }, + { + emptyPortsManifest, + util.IntOrString{Kind: util.IntstrString, StrVal: ""}, + 0, + true, + }, + { + emptyPortsManifest, + util.IntOrString{Kind: util.IntstrInt, IntVal: 0}, + 0, + true, + }, } for _, test := range tests { - port, err := findPort(&manifest, test.portName) + port, err := findPort(&test.manifest, test.portName) if port != test.wport { t.Errorf("Expected port %d, Got %d", test.wport, port) }