github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/pod/util.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package pod
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/kubernetes/pkg/api"
    23  	"k8s.io/kubernetes/pkg/util/intstr"
    24  )
    25  
    26  const (
    27  	// TODO: to be de!eted after v1.3 is released. PodSpec has a dedicated Hostname field.
    28  	// The annotation value is a string specifying the hostname to be used for the pod e.g 'my-webserver-1'
    29  	PodHostnameAnnotation = "pod.beta.kubernetes.io/hostname"
    30  
    31  	// TODO: to be de!eted after v1.3 is released. PodSpec has a dedicated Subdomain field.
    32  	// The annotation value is a string specifying the subdomain e.g. "my-web-service"
    33  	// If specified, on the the pod itself, "<hostname>.my-web-service.<namespace>.svc.<cluster domain>" would resolve to
    34  	// the pod's IP.
    35  	// If there is a headless service named "my-web-service" in the same namespace as the pod, then,
    36  	// <hostname>.my-web-service.<namespace>.svc.<cluster domain>" would be resolved by the cluster DNS Server.
    37  	PodSubdomainAnnotation = "pod.beta.kubernetes.io/subdomain"
    38  )
    39  
    40  // FindPort locates the container port for the given pod and portName.  If the
    41  // targetPort is a number, use that.  If the targetPort is a string, look that
    42  // string up in all named ports in all containers in the target pod.  If no
    43  // match is found, fail.
    44  func FindPort(pod *api.Pod, svcPort *api.ServicePort) (int, error) {
    45  	portName := svcPort.TargetPort
    46  	switch portName.Type {
    47  	case intstr.String:
    48  		name := portName.StrVal
    49  		for _, container := range pod.Spec.Containers {
    50  			for _, port := range container.Ports {
    51  				if port.Name == name && port.Protocol == svcPort.Protocol {
    52  					return int(port.ContainerPort), nil
    53  				}
    54  			}
    55  		}
    56  	case intstr.Int:
    57  		return portName.IntValue(), nil
    58  	}
    59  
    60  	return 0, fmt.Errorf("no suitable port for manifest: %s", pod.UID)
    61  }