github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/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  	"k8s.io/kubernetes/pkg/api/unversioned"
    22  )
    23  
    24  // Returns string version of ResourceName.
    25  func (self ResourceName) String() string {
    26  	return string(self)
    27  }
    28  
    29  // Returns the CPU limit if specified.
    30  func (self *ResourceList) Cpu() *resource.Quantity {
    31  	if val, ok := (*self)[ResourceCPU]; ok {
    32  		return &val
    33  	}
    34  	return &resource.Quantity{Format: resource.DecimalSI}
    35  }
    36  
    37  // Returns the Memory limit if specified.
    38  func (self *ResourceList) Memory() *resource.Quantity {
    39  	if val, ok := (*self)[ResourceMemory]; ok {
    40  		return &val
    41  	}
    42  	return &resource.Quantity{Format: resource.BinarySI}
    43  }
    44  
    45  func (self *ResourceList) Pods() *resource.Quantity {
    46  	if val, ok := (*self)[ResourcePods]; ok {
    47  		return &val
    48  	}
    49  	return &resource.Quantity{}
    50  }
    51  
    52  func (self *ResourceList) NvidiaGPU() *resource.Quantity {
    53  	if val, ok := (*self)[ResourceNvidiaGPU]; ok {
    54  		return &val
    55  	}
    56  	return &resource.Quantity{}
    57  }
    58  
    59  func GetContainerStatus(statuses []ContainerStatus, name string) (ContainerStatus, bool) {
    60  	for i := range statuses {
    61  		if statuses[i].Name == name {
    62  			return statuses[i], true
    63  		}
    64  	}
    65  	return ContainerStatus{}, false
    66  }
    67  
    68  func GetExistingContainerStatus(statuses []ContainerStatus, name string) ContainerStatus {
    69  	for i := range statuses {
    70  		if statuses[i].Name == name {
    71  			return statuses[i]
    72  		}
    73  	}
    74  	return ContainerStatus{}
    75  }
    76  
    77  // IsPodReady returns true if a pod is ready; false otherwise.
    78  func IsPodReady(pod *Pod) bool {
    79  	return IsPodReadyConditionTrue(pod.Status)
    80  }
    81  
    82  // IsPodReady retruns true if a pod is ready; false otherwise.
    83  func IsPodReadyConditionTrue(status PodStatus) bool {
    84  	condition := GetPodReadyCondition(status)
    85  	return condition != nil && condition.Status == ConditionTrue
    86  }
    87  
    88  // Extracts the pod ready condition from the given status and returns that.
    89  // Returns nil if the condition is not present.
    90  func GetPodReadyCondition(status PodStatus) *PodCondition {
    91  	_, condition := GetPodCondition(&status, PodReady)
    92  	return condition
    93  }
    94  
    95  func GetPodCondition(status *PodStatus, conditionType PodConditionType) (int, *PodCondition) {
    96  	for i, c := range status.Conditions {
    97  		if c.Type == conditionType {
    98  			return i, &status.Conditions[i]
    99  		}
   100  	}
   101  	return -1, nil
   102  }
   103  
   104  // Updates existing pod condition or creates a new one. Sets LastTransitionTime to now if the
   105  // status has changed.
   106  // Returns true if pod condition has changed or has been added.
   107  func UpdatePodCondition(status *PodStatus, condition *PodCondition) bool {
   108  	condition.LastTransitionTime = unversioned.Now()
   109  	// Try to find this pod condition.
   110  	conditionIndex, oldCondition := GetPodCondition(status, condition.Type)
   111  
   112  	if oldCondition == nil {
   113  		// We are adding new pod condition.
   114  		status.Conditions = append(status.Conditions, *condition)
   115  		return true
   116  	} else {
   117  		// We are updating an existing condition, so we need to check if it has changed.
   118  		if condition.Status == oldCondition.Status {
   119  			condition.LastTransitionTime = oldCondition.LastTransitionTime
   120  		}
   121  		status.Conditions[conditionIndex] = *condition
   122  		// Return true if one of the fields have changed.
   123  		return condition.Status != oldCondition.Status ||
   124  			condition.Reason != oldCondition.Reason ||
   125  			condition.Message != oldCondition.Message ||
   126  			!condition.LastProbeTime.Equal(oldCondition.LastProbeTime) ||
   127  			!condition.LastTransitionTime.Equal(oldCondition.LastTransitionTime)
   128  	}
   129  }
   130  
   131  // IsNodeReady returns true if a node is ready; false otherwise.
   132  func IsNodeReady(node *Node) bool {
   133  	for _, c := range node.Status.Conditions {
   134  		if c.Type == NodeReady {
   135  			return c.Status == ConditionTrue
   136  		}
   137  	}
   138  	return false
   139  }
   140  
   141  // PodRequestsAndLimits returns a dictionary of all defined resources summed up for all
   142  // containers of the pod.
   143  func PodRequestsAndLimits(pod *Pod) (reqs map[ResourceName]resource.Quantity, limits map[ResourceName]resource.Quantity, err error) {
   144  	reqs, limits = map[ResourceName]resource.Quantity{}, map[ResourceName]resource.Quantity{}
   145  	for _, container := range pod.Spec.Containers {
   146  		for name, quantity := range container.Resources.Requests {
   147  			if value, ok := reqs[name]; !ok {
   148  				reqs[name] = *quantity.Copy()
   149  			} else if err = value.Add(quantity); err != nil {
   150  				return nil, nil, err
   151  			}
   152  		}
   153  		for name, quantity := range container.Resources.Limits {
   154  			if value, ok := limits[name]; !ok {
   155  				limits[name] = *quantity.Copy()
   156  			} else if err = value.Add(quantity); err != nil {
   157  				return nil, nil, err
   158  			}
   159  		}
   160  	}
   161  	return
   162  }