Fix default resource limits (node capacities) for downward api volumes

This commit is contained in:
Avesh Agarwal
2016-07-26 18:04:03 -04:00
committed by Avesh
parent 431e7ce1ab
commit 52a60fe3be
11 changed files with 127 additions and 31 deletions

View File

@@ -20,15 +20,16 @@ import (
"fmt"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/fieldpath"
)
// defaultPodLimitsForDownwardApi copies the input pod, and optional container,
// and applies default resource limits. it returns a copy of the input pod,
// and a copy of the input container (if specified) with default limits
// applied. if a container has no limit specified, it will default the limit to
// the node capacity.
// the node allocatable.
// TODO: if/when we have pod level resources, we need to update this function
// to use those limits instead of node capacity.
// to use those limits instead of node allocatable.
func (kl *Kubelet) defaultPodLimitsForDownwardApi(pod *api.Pod, container *api.Container) (*api.Pod, *api.Container, error) {
if pod == nil {
return nil, nil, fmt.Errorf("invalid input, pod cannot be nil")
@@ -38,7 +39,7 @@ func (kl *Kubelet) defaultPodLimitsForDownwardApi(pod *api.Pod, container *api.C
if err != nil {
return nil, nil, fmt.Errorf("failed to find node object, expected a node")
}
capacity := node.Status.Capacity
allocatable := node.Status.Allocatable
podCopy, err := api.Scheme.Copy(pod)
if err != nil {
@@ -49,7 +50,7 @@ func (kl *Kubelet) defaultPodLimitsForDownwardApi(pod *api.Pod, container *api.C
return nil, nil, fmt.Errorf("unexpected type returned from deep copy of pod object")
}
for idx := range outputPod.Spec.Containers {
mergeContainerResourceLimitsWithCapacity(&outputPod.Spec.Containers[idx], capacity)
fieldpath.MergeContainerResourceLimits(&outputPod.Spec.Containers[idx], allocatable)
}
var outputContainer *api.Container
@@ -62,23 +63,7 @@ func (kl *Kubelet) defaultPodLimitsForDownwardApi(pod *api.Pod, container *api.C
if !ok {
return nil, nil, fmt.Errorf("unexpected type returned from deep copy of container object")
}
mergeContainerResourceLimitsWithCapacity(outputContainer, capacity)
fieldpath.MergeContainerResourceLimits(outputContainer, allocatable)
}
return outputPod, outputContainer, nil
}
// mergeContainerResourceLimitsWithCapacity checks if a limit is applied for
// the container, and if not, it sets the limit based on the capacity.
func mergeContainerResourceLimitsWithCapacity(container *api.Container,
capacity api.ResourceList) {
if container.Resources.Limits == nil {
container.Resources.Limits = make(api.ResourceList)
}
for _, resource := range []api.ResourceName{api.ResourceCPU, api.ResourceMemory} {
if quantity, exists := container.Resources.Limits[resource]; !exists || quantity.IsZero() {
if cap, exists := capacity[resource]; exists {
container.Resources.Limits[resource] = *cap.Copy()
}
}
}
}