Merge pull request #137621 from Karthik-K-N/consolidate-functions

kubelet: deduplicate CPU conversion helper functions
This commit is contained in:
Kubernetes Prow Robot
2026-03-14 15:05:34 +05:30
committed by GitHub
4 changed files with 11 additions and 33 deletions

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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)
}