mirror of
https://github.com/optim-enterprises-bv/kubernetes.git
synced 2025-11-02 03:08:15 +00:00
dedupe pod resource request calculation
This commit is contained in:
@@ -24,10 +24,10 @@ import (
|
||||
"strconv"
|
||||
|
||||
libcontainercgroups "github.com/opencontainers/runc/libcontainer/cgroups"
|
||||
|
||||
v1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
|
||||
podutil "k8s.io/kubernetes/pkg/api/v1/pod"
|
||||
"k8s.io/kubernetes/pkg/api/v1/resource"
|
||||
v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
|
||||
@@ -119,8 +119,28 @@ func HugePageLimits(resourceList v1.ResourceList) map[int64]int64 {
|
||||
|
||||
// ResourceConfigForPod takes the input pod and outputs the cgroup resource config.
|
||||
func ResourceConfigForPod(pod *v1.Pod, enforceCPULimits bool, cpuPeriod uint64, enforceMemoryQoS bool) *ResourceConfig {
|
||||
inPlacePodVerticalScalingEnabled := utilfeature.DefaultFeatureGate.Enabled(kubefeatures.InPlacePodVerticalScaling)
|
||||
// sum requests and limits.
|
||||
reqs, limits := resource.PodRequestsAndLimits(pod)
|
||||
reqs := resource.PodRequests(pod, resource.PodResourcesOptions{
|
||||
InPlacePodVerticalScalingEnabled: inPlacePodVerticalScalingEnabled,
|
||||
})
|
||||
// track if limits were applied for each resource.
|
||||
memoryLimitsDeclared := true
|
||||
cpuLimitsDeclared := true
|
||||
|
||||
limits := resource.PodLimits(pod, resource.PodResourcesOptions{
|
||||
InPlacePodVerticalScalingEnabled: inPlacePodVerticalScalingEnabled,
|
||||
ContainerFn: func(res v1.ResourceList, containerType podutil.ContainerType) {
|
||||
if res.Cpu().IsZero() {
|
||||
cpuLimitsDeclared = false
|
||||
}
|
||||
if res.Memory().IsZero() {
|
||||
memoryLimitsDeclared = false
|
||||
}
|
||||
},
|
||||
})
|
||||
// map hugepage pagesize (bytes) to limits (bytes)
|
||||
hugePageLimits := HugePageLimits(reqs)
|
||||
|
||||
cpuRequests := int64(0)
|
||||
cpuLimits := int64(0)
|
||||
@@ -139,48 +159,6 @@ func ResourceConfigForPod(pod *v1.Pod, enforceCPULimits bool, cpuPeriod uint64,
|
||||
cpuShares := MilliCPUToShares(cpuRequests)
|
||||
cpuQuota := MilliCPUToQuota(cpuLimits, int64(cpuPeriod))
|
||||
|
||||
// track if limits were applied for each resource.
|
||||
memoryLimitsDeclared := true
|
||||
cpuLimitsDeclared := true
|
||||
// map hugepage pagesize (bytes) to limits (bytes)
|
||||
hugePageLimits := map[int64]int64{}
|
||||
for _, container := range pod.Spec.Containers {
|
||||
if container.Resources.Limits.Cpu().IsZero() {
|
||||
cpuLimitsDeclared = false
|
||||
}
|
||||
if container.Resources.Limits.Memory().IsZero() {
|
||||
memoryLimitsDeclared = false
|
||||
}
|
||||
containerHugePageLimits := HugePageLimits(container.Resources.Requests)
|
||||
if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.InPlacePodVerticalScaling) {
|
||||
if cs, ok := podutil.GetContainerStatus(pod.Status.ContainerStatuses, container.Name); ok {
|
||||
containerHugePageLimits = HugePageLimits(cs.ResourcesAllocated)
|
||||
}
|
||||
}
|
||||
for k, v := range containerHugePageLimits {
|
||||
if value, exists := hugePageLimits[k]; exists {
|
||||
hugePageLimits[k] = value + v
|
||||
} else {
|
||||
hugePageLimits[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, container := range pod.Spec.InitContainers {
|
||||
if container.Resources.Limits.Cpu().IsZero() {
|
||||
cpuLimitsDeclared = false
|
||||
}
|
||||
if container.Resources.Limits.Memory().IsZero() {
|
||||
memoryLimitsDeclared = false
|
||||
}
|
||||
containerHugePageLimits := HugePageLimits(container.Resources.Requests)
|
||||
for k, v := range containerHugePageLimits {
|
||||
if value, exists := hugePageLimits[k]; !exists || v > value {
|
||||
hugePageLimits[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// quota is not capped when cfs quota is disabled
|
||||
if !enforceCPULimits {
|
||||
cpuQuota = int64(-1)
|
||||
|
||||
Reference in New Issue
Block a user