From 1d76d229ab556b26e9e529154ae5a1fdfdaa188d Mon Sep 17 00:00:00 2001 From: Karthik Bhat Date: Wed, 11 Mar 2026 11:57:34 +0530 Subject: [PATCH] kubelet: deduplicate CPU conversion helper functions --- pkg/kubelet/cm/helpers_linux.go | 15 ++++++------- pkg/kubelet/kuberuntime/helpers_linux.go | 21 +------------------ pkg/kubelet/kuberuntime/helpers_linux_test.go | 4 ++-- .../kuberuntime_container_linux.go | 4 ++-- 4 files changed, 11 insertions(+), 33 deletions(-) diff --git a/pkg/kubelet/cm/helpers_linux.go b/pkg/kubelet/cm/helpers_linux.go index b73ff774f8d..02a4ce0f675 100644 --- a/pkg/kubelet/cm/helpers_linux.go +++ b/pkg/kubelet/cm/helpers_linux.go @@ -347,7 +347,7 @@ func GetKubeletContainer(logger klog.Logger, kubeletCgroups string) (string, err func CPURequestsFromConfig(podConfig *ResourceConfig) *resource.Quantity { var cpuRequest *resource.Quantity if podConfig != nil && *podConfig.CPUShares > 0 { - milliCPU := sharesToMilliCPU(int64(*podConfig.CPUShares)) + milliCPU := SharesToMilliCPU(int64(*podConfig.CPUShares)) if milliCPU > 0 { cpuRequest = resource.NewMilliQuantity(milliCPU, resource.DecimalSI) } @@ -360,7 +360,7 @@ func CPULimitsFromConfig(podConfig *ResourceConfig) *resource.Quantity { var cpuLimit *resource.Quantity if podConfig != nil && *podConfig.CPUPeriod > 0 { - milliCPU := quotaToMilliCPU(*podConfig.CPUQuota, int64(*podConfig.CPUPeriod)) + milliCPU := QuotaToMilliCPU(*podConfig.CPUQuota, int64(*podConfig.CPUPeriod)) if milliCPU > 0 { cpuLimit = resource.NewMilliQuantity(milliCPU, resource.DecimalSI) } @@ -378,9 +378,8 @@ func MemoryLimitsFromConfig(podConfig *ResourceConfig) *resource.Quantity { return memLimit } -// sharesToMilliCPU converts CpuShares (cpu.shares) to milli-CPU value -// TODO - dedup sharesToMilliCPU with sharesToMilliCPU in pkg/kubelet/kuberuntime/helpers_linux.go -func sharesToMilliCPU(shares int64) int64 { +// SharesToMilliCPU converts CpuShares (cpu.shares) to milli-CPU value +func SharesToMilliCPU(shares int64) int64 { milliCPU := int64(0) if shares >= int64(MinShares) { milliCPU = int64(math.Ceil(float64(shares*MilliCPUToCPU) / float64(SharesPerCPU))) @@ -388,10 +387,8 @@ func sharesToMilliCPU(shares int64) int64 { return milliCPU } -// quotaToMilliCPU converts cpu.cfs_quota_us and cpu.cfs_period_us to milli-CPU -// value -// TODO - dedup quotaToMilliCPU with sharesToMilliCPU in pkg/kubelet/kuberuntime/helpers_linux.go -func quotaToMilliCPU(quota int64, period int64) int64 { +// QuotaToMilliCPU converts cpu.cfs_quota_us and cpu.cfs_period_us to milli-CPU value +func QuotaToMilliCPU(quota int64, period int64) int64 { if quota == -1 { return int64(0) } diff --git a/pkg/kubelet/kuberuntime/helpers_linux.go b/pkg/kubelet/kuberuntime/helpers_linux.go index b5933ee0148..9de005cdbf3 100644 --- a/pkg/kubelet/kuberuntime/helpers_linux.go +++ b/pkg/kubelet/kuberuntime/helpers_linux.go @@ -19,29 +19,10 @@ limitations under the License. package kuberuntime import ( - "math" - v1 "k8s.io/api/core/v1" "k8s.io/kubernetes/pkg/kubelet/cm" ) -// sharesToMilliCPU converts CpuShares (cpu.shares) to milli-CPU value -func sharesToMilliCPU(shares int64) int64 { - milliCPU := int64(0) - if shares >= int64(cm.MinShares) { - milliCPU = int64(math.Ceil(float64(shares*cm.MilliCPUToCPU) / float64(cm.SharesPerCPU))) - } - return milliCPU -} - -// quotaToMilliCPU converts cpu.cfs_quota_us and cpu.cfs_period_us to milli-CPU value -func quotaToMilliCPU(quota int64, period int64) int64 { - if quota == -1 { - return int64(0) - } - return (quota * cm.MilliCPUToCPU) / period -} - func subtractOverheadFromResourceConfig(resCfg *cm.ResourceConfig, pod *v1.Pod) *cm.ResourceConfig { if resCfg == nil { return nil @@ -58,7 +39,7 @@ func subtractOverheadFromResourceConfig(resCfg *cm.ResourceConfig, pod *v1.Pod) } if rc.CPUShares != nil { - totalCPUMilli := sharesToMilliCPU(int64(*rc.CPUShares)) + totalCPUMilli := cm.SharesToMilliCPU(int64(*rc.CPUShares)) cpuShares := cm.MilliCPUToShares(totalCPUMilli - cpu.MilliValue()) rc.CPUShares = &cpuShares } diff --git a/pkg/kubelet/kuberuntime/helpers_linux_test.go b/pkg/kubelet/kuberuntime/helpers_linux_test.go index ca68180bb26..b230eb00eaf 100644 --- a/pkg/kubelet/kuberuntime/helpers_linux_test.go +++ b/pkg/kubelet/kuberuntime/helpers_linux_test.go @@ -267,7 +267,7 @@ func TestSharesToMilliCPU(t *testing.T) { if testMilliCPU < 2 { expectedMilliCPU = 2 } - milliCPU := sharesToMilliCPU(shares) + milliCPU := cm.SharesToMilliCPU(shares) if milliCPU != expectedMilliCPU { t.Errorf("Test sharesToMilliCPU: Input shares %v, expected milliCPU %v, but got %v", shares, expectedMilliCPU, milliCPU) } @@ -307,7 +307,7 @@ func TestQuotaToMilliCPU(t *testing.T) { expected: int64(1500), }} { t.Run(tc.name, func(t *testing.T) { - milliCPU := quotaToMilliCPU(tc.quota, tc.period) + milliCPU := cm.QuotaToMilliCPU(tc.quota, tc.period) if milliCPU != tc.expected { t.Errorf("Test %s: Input quota %v and period %v, expected milliCPU %v, but got %v", tc.name, tc.quota, tc.period, tc.expected, milliCPU) } diff --git a/pkg/kubelet/kuberuntime/kuberuntime_container_linux.go b/pkg/kubelet/kuberuntime/kuberuntime_container_linux.go index 84294bde68c..9b9cb8c1148 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_container_linux.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_container_linux.go @@ -386,13 +386,13 @@ func toKubeContainerResources(statusResources *runtimeapi.ContainerResources) *k if runtimeStatusResources != nil { var cpuLimit, memLimit, cpuRequest *resource.Quantity if runtimeStatusResources.CpuPeriod > 0 { - milliCPU := quotaToMilliCPU(runtimeStatusResources.CpuQuota, runtimeStatusResources.CpuPeriod) + milliCPU := cm.QuotaToMilliCPU(runtimeStatusResources.CpuQuota, runtimeStatusResources.CpuPeriod) if milliCPU > 0 { cpuLimit = resource.NewMilliQuantity(milliCPU, resource.DecimalSI) } } if runtimeStatusResources.CpuShares > 0 { - milliCPU := sharesToMilliCPU(runtimeStatusResources.CpuShares) + milliCPU := cm.SharesToMilliCPU(runtimeStatusResources.CpuShares) if milliCPU > 0 { cpuRequest = resource.NewMilliQuantity(milliCPU, resource.DecimalSI) }