From ea0c6bcbfdc47528e713de2779abea4b3e54c49b Mon Sep 17 00:00:00 2001 From: Claire Li Date: Wed, 5 Nov 2014 22:38:06 -0800 Subject: [PATCH] Refactor findPort in service pkg --- pkg/service/endpoints_controller.go | 33 ++++++++++++++++++----------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/pkg/service/endpoints_controller.go b/pkg/service/endpoints_controller.go index 93eddb6f89..8b943591df 100644 --- a/pkg/service/endpoints_controller.go +++ b/pkg/service/endpoints_controller.go @@ -138,21 +138,30 @@ func endpointsEqual(e *api.Endpoints, endpoints []string) bool { // findPort locates the container port for the given manifest and portName. func findPort(manifest *api.ContainerManifest, portName util.IntOrString) (int, error) { - if ((portName.Kind == util.IntstrString && len(portName.StrVal) == 0) || - (portName.Kind == util.IntstrInt && portName.IntVal == 0)) && - len(manifest.Containers[0].Ports) > 0 { - return manifest.Containers[0].Ports[0].ContainerPort, nil + firstContianerPort := -1 + if len(manifest.Containers[0].Ports) > 0 { + firstContianerPort = manifest.Containers[0].Ports[0].ContainerPort } - if portName.Kind == util.IntstrInt { - return portName.IntVal, nil - } - name := portName.StrVal - for _, container := range manifest.Containers { - for _, port := range container.Ports { - if port.Name == name { - return port.ContainerPort, nil + + switch portName.Kind { + case util.IntstrString: + if len(portName.StrVal) == 0 && firstContianerPort != -1 { + return firstContianerPort, nil + } + name := portName.StrVal + for _, container := range manifest.Containers { + for _, port := range container.Ports { + if port.Name == name { + return port.ContainerPort, nil + } } } + case util.IntstrInt: + if portName.IntVal == 0 && firstContianerPort != -1 { + return firstContianerPort, nil + } + return portName.IntVal, nil } + return -1, fmt.Errorf("no suitable port for manifest: %s", manifest.ID) }