github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/resource_helpers.go (about)

     1  /*
     2  Copyright 2014 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 api
    18  
    19  import (
    20  	"k8s.io/kubernetes/pkg/api/resource"
    21  )
    22  
    23  // Returns string version of ResourceName.
    24  func (self ResourceName) String() string {
    25  	return string(self)
    26  }
    27  
    28  // Returns the CPU limit if specified.
    29  func (self *ResourceList) Cpu() *resource.Quantity {
    30  	if val, ok := (*self)[ResourceCPU]; ok {
    31  		return &val
    32  	}
    33  	return &resource.Quantity{}
    34  }
    35  
    36  // Returns the Memory limit if specified.
    37  func (self *ResourceList) Memory() *resource.Quantity {
    38  	if val, ok := (*self)[ResourceMemory]; ok {
    39  		return &val
    40  	}
    41  	return &resource.Quantity{}
    42  }
    43  
    44  func (self *ResourceList) Pods() *resource.Quantity {
    45  	if val, ok := (*self)[ResourcePods]; ok {
    46  		return &val
    47  	}
    48  	return &resource.Quantity{}
    49  }
    50  
    51  func GetContainerStatus(statuses []ContainerStatus, name string) (ContainerStatus, bool) {
    52  	for i := range statuses {
    53  		if statuses[i].Name == name {
    54  			return statuses[i], true
    55  		}
    56  	}
    57  	return ContainerStatus{}, false
    58  }
    59  
    60  func GetExistingContainerStatus(statuses []ContainerStatus, name string) ContainerStatus {
    61  	for i := range statuses {
    62  		if statuses[i].Name == name {
    63  			return statuses[i]
    64  		}
    65  	}
    66  	return ContainerStatus{}
    67  }
    68  
    69  // IsPodReady returns true if a pod is ready; false otherwise.
    70  func IsPodReady(pod *Pod) bool {
    71  	return IsPodReadyConditionTrue(pod.Status)
    72  }
    73  
    74  // IsPodReady retruns true if a pod is ready; false otherwise.
    75  func IsPodReadyConditionTrue(status PodStatus) bool {
    76  	condition := GetPodReadyCondition(status)
    77  	return condition != nil && condition.Status == ConditionTrue
    78  }
    79  
    80  // Extracts the pod ready condition from the given status and returns that.
    81  // Returns nil if the condition is not present.
    82  func GetPodReadyCondition(status PodStatus) *PodCondition {
    83  	for i, c := range status.Conditions {
    84  		if c.Type == PodReady {
    85  			return &status.Conditions[i]
    86  		}
    87  	}
    88  	return nil
    89  }
    90  
    91  // IsNodeReady returns true if a node is ready; false otherwise.
    92  func IsNodeReady(node *Node) bool {
    93  	for _, c := range node.Status.Conditions {
    94  		if c.Type == NodeReady {
    95  			return c.Status == ConditionTrue
    96  		}
    97  	}
    98  	return false
    99  }