// PodsRequests returns sum of each resource request for each pod in list // If a given pod in the list does not have a request for the named resource, we log the error // but still attempt to get the most representative count func PodsRequests(pods []*api.Pod, resourceName api.ResourceName) *resource.Quantity { var sum *resource.Quantity for i := range pods { pod := pods[i] podQuantity, err := PodRequests(pod, resourceName) if err != nil { // log the error, but try to keep the most accurate count possible in log // rationale here is that you may have had pods in a namespace that did not have // explicit requests prior to adding the quota glog.Infof("No explicit request for resource, pod %s/%s, %s", pod.Namespace, pod.Name, resourceName) } else { if sum == nil { sum = podQuantity } else { sum.Add(*podQuantity) } } } // if list is empty if sum == nil { q := resource.MustParse("0") sum = &q } return sum }
// PodRequests returns sum of each resource request across all containers in pod func PodRequests(pod *api.Pod, resourceName api.ResourceName) (*resource.Quantity, error) { if !PodHasRequests(pod, resourceName) { return nil, fmt.Errorf("Each container in pod %s/%s does not have an explicit request for resource %s.", pod.Namespace, pod.Name, resourceName) } var sum *resource.Quantity for j := range pod.Spec.Containers { value, _ := pod.Spec.Containers[j].Resources.Requests[resourceName] if sum == nil { sum = value.Copy() } else { err := sum.Add(value) if err != nil { return sum, err } } } // if list is empty if sum == nil { q := resource.MustParse("0") sum = &q } return sum, nil }