// resolvePort attempts to turn a IntOrString port reference into a concrete port number. // If portReference has an int value, it is treated as a literal, and simply returns that value. // If portReference is a string, an attempt is first made to parse it as an integer. If that fails, // an attempt is made to find a port with the same name in the container spec. // If a port with the same name is found, it's ContainerPort value is returned. If no matching // port is found, an error is returned. func resolvePort(portReference intstr.IntOrString, container *api.Container) (int, error) { if portReference.Type == intstr.Int { return portReference.IntValue(), nil } portName := portReference.StrVal port, err := strconv.Atoi(portName) if err == nil { return port, nil } for _, portSpec := range container.Ports { if portSpec.Name == portName { return int(portSpec.ContainerPort), nil } } return -1, fmt.Errorf("couldn't find port: %v in %v", portReference, container) }
func extractPort(param intstr.IntOrString, container api.Container) (int, error) { port := -1 var err error switch param.Type { case intstr.Int: port = param.IntValue() case intstr.String: if port, err = findPortByName(container, param.StrVal); err != nil { // Last ditch effort - maybe it was an int stored as string? if port, err = strconv.Atoi(param.StrVal); err != nil { return port, err } } default: return port, fmt.Errorf("IntOrString had no kind: %+v", param) } if port > 0 && port < 65536 { return port, nil } return port, fmt.Errorf("invalid port number: %v", port) }