mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-10-31 02:08:13 +00:00 
			
		
		
		
	Merge pull request #108832 from waynepeking348/fix_bugs_of_container_cpu_shares
fix bugs of container cpu shares when cpu request set to zero
This commit is contained in:
		| @@ -70,7 +70,11 @@ func (m *kubeGenericRuntimeManager) generateLinuxContainerConfig(container *v1.C | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// set linux container resources | 	// set linux container resources | ||||||
| 	lc.Resources = m.calculateLinuxResources(container.Resources.Requests.Cpu(), container.Resources.Limits.Cpu(), container.Resources.Limits.Memory()) | 	var cpuRequest *resource.Quantity | ||||||
|  | 	if _, cpuRequestExists := container.Resources.Requests[v1.ResourceCPU]; cpuRequestExists { | ||||||
|  | 		cpuRequest = container.Resources.Requests.Cpu() | ||||||
|  | 	} | ||||||
|  | 	lc.Resources = m.calculateLinuxResources(cpuRequest, container.Resources.Limits.Cpu(), container.Resources.Limits.Memory()) | ||||||
|  |  | ||||||
| 	lc.Resources.OomScoreAdj = int64(qos.GetContainerOOMScoreAdjust(pod, container, | 	lc.Resources.OomScoreAdj = int64(qos.GetContainerOOMScoreAdjust(pod, container, | ||||||
| 		int64(m.machineInfo.MemoryCapacity))) | 		int64(m.machineInfo.MemoryCapacity))) | ||||||
| @@ -145,7 +149,7 @@ func (m *kubeGenericRuntimeManager) calculateLinuxResources(cpuRequest, cpuLimit | |||||||
| 	// If request is not specified, but limit is, we want request to default to limit. | 	// If request is not specified, but limit is, we want request to default to limit. | ||||||
| 	// API server does this for new containers, but we repeat this logic in Kubelet | 	// API server does this for new containers, but we repeat this logic in Kubelet | ||||||
| 	// for containers running on existing Kubernetes clusters. | 	// for containers running on existing Kubernetes clusters. | ||||||
| 	if cpuRequest.IsZero() && !cpuLimit.IsZero() { | 	if cpuRequest == nil && cpuLimit != nil { | ||||||
| 		cpuShares = int64(cm.MilliCPUToShares(cpuLimit.MilliValue())) | 		cpuShares = int64(cm.MilliCPUToShares(cpuLimit.MilliValue())) | ||||||
| 	} else { | 	} else { | ||||||
| 		// if cpuRequest.Amount is nil, then MilliCPUToShares will return the minimal number | 		// if cpuRequest.Amount is nil, then MilliCPUToShares will return the minimal number | ||||||
|   | |||||||
| @@ -232,18 +232,23 @@ func TestCalculateLinuxResources(t *testing.T) { | |||||||
|  |  | ||||||
| 	assert.NoError(t, err) | 	assert.NoError(t, err) | ||||||
|  |  | ||||||
|  | 	generateResourceQuantity := func(str string) *resource.Quantity { | ||||||
|  | 		quantity := resource.MustParse(str) | ||||||
|  | 		return &quantity | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	tests := []struct { | 	tests := []struct { | ||||||
| 		name     string | 		name     string | ||||||
| 		cpuReq   resource.Quantity | 		cpuReq   *resource.Quantity | ||||||
| 		cpuLim   resource.Quantity | 		cpuLim   *resource.Quantity | ||||||
| 		memLim   resource.Quantity | 		memLim   *resource.Quantity | ||||||
| 		expected *runtimeapi.LinuxContainerResources | 		expected *runtimeapi.LinuxContainerResources | ||||||
| 	}{ | 	}{ | ||||||
| 		{ | 		{ | ||||||
| 			name:   "Request128MBLimit256MB", | 			name:   "Request128MBLimit256MB", | ||||||
| 			cpuReq: resource.MustParse("1"), | 			cpuReq: generateResourceQuantity("1"), | ||||||
| 			cpuLim: resource.MustParse("2"), | 			cpuLim: generateResourceQuantity("2"), | ||||||
| 			memLim: resource.MustParse("128Mi"), | 			memLim: generateResourceQuantity("128Mi"), | ||||||
| 			expected: &runtimeapi.LinuxContainerResources{ | 			expected: &runtimeapi.LinuxContainerResources{ | ||||||
| 				CpuPeriod:          100000, | 				CpuPeriod:          100000, | ||||||
| 				CpuQuota:           200000, | 				CpuQuota:           200000, | ||||||
| @@ -253,9 +258,9 @@ func TestCalculateLinuxResources(t *testing.T) { | |||||||
| 		}, | 		}, | ||||||
| 		{ | 		{ | ||||||
| 			name:   "RequestNoMemory", | 			name:   "RequestNoMemory", | ||||||
| 			cpuReq: resource.MustParse("2"), | 			cpuReq: generateResourceQuantity("2"), | ||||||
| 			cpuLim: resource.MustParse("8"), | 			cpuLim: generateResourceQuantity("8"), | ||||||
| 			memLim: resource.MustParse("0"), | 			memLim: generateResourceQuantity("0"), | ||||||
| 			expected: &runtimeapi.LinuxContainerResources{ | 			expected: &runtimeapi.LinuxContainerResources{ | ||||||
| 				CpuPeriod:          100000, | 				CpuPeriod:          100000, | ||||||
| 				CpuQuota:           800000, | 				CpuQuota:           800000, | ||||||
| @@ -263,9 +268,32 @@ func TestCalculateLinuxResources(t *testing.T) { | |||||||
| 				MemoryLimitInBytes: 0, | 				MemoryLimitInBytes: 0, | ||||||
| 			}, | 			}, | ||||||
| 		}, | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name:   "RequestNilCPU", | ||||||
|  | 			cpuLim: generateResourceQuantity("2"), | ||||||
|  | 			memLim: generateResourceQuantity("0"), | ||||||
|  | 			expected: &runtimeapi.LinuxContainerResources{ | ||||||
|  | 				CpuPeriod:          100000, | ||||||
|  | 				CpuQuota:           200000, | ||||||
|  | 				CpuShares:          2048, | ||||||
|  | 				MemoryLimitInBytes: 0, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name:   "RequestZeroCPU", | ||||||
|  | 			cpuReq: generateResourceQuantity("0"), | ||||||
|  | 			cpuLim: generateResourceQuantity("2"), | ||||||
|  | 			memLim: generateResourceQuantity("0"), | ||||||
|  | 			expected: &runtimeapi.LinuxContainerResources{ | ||||||
|  | 				CpuPeriod:          100000, | ||||||
|  | 				CpuQuota:           200000, | ||||||
|  | 				CpuShares:          2, | ||||||
|  | 				MemoryLimitInBytes: 0, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
| 	} | 	} | ||||||
| 	for _, test := range tests { | 	for _, test := range tests { | ||||||
| 		linuxContainerResources := m.calculateLinuxResources(&test.cpuReq, &test.cpuLim, &test.memLim) | 		linuxContainerResources := m.calculateLinuxResources(test.cpuReq, test.cpuLim, test.memLim) | ||||||
| 		assert.Equal(t, test.expected, linuxContainerResources) | 		assert.Equal(t, test.expected, linuxContainerResources) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
| @@ -21,6 +21,7 @@ package kuberuntime | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	v1 "k8s.io/api/core/v1" | 	v1 "k8s.io/api/core/v1" | ||||||
|  | 	"k8s.io/apimachinery/pkg/api/resource" | ||||||
| 	runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" | 	runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1" | ||||||
| 	resourcehelper "k8s.io/kubernetes/pkg/api/v1/resource" | 	resourcehelper "k8s.io/kubernetes/pkg/api/v1/resource" | ||||||
| ) | ) | ||||||
| @@ -41,7 +42,11 @@ func (m *kubeGenericRuntimeManager) convertOverheadToLinuxResources(pod *v1.Pod) | |||||||
|  |  | ||||||
| func (m *kubeGenericRuntimeManager) calculateSandboxResources(pod *v1.Pod) *runtimeapi.LinuxContainerResources { | func (m *kubeGenericRuntimeManager) calculateSandboxResources(pod *v1.Pod) *runtimeapi.LinuxContainerResources { | ||||||
| 	req, lim := resourcehelper.PodRequestsAndLimitsWithoutOverhead(pod) | 	req, lim := resourcehelper.PodRequestsAndLimitsWithoutOverhead(pod) | ||||||
| 	return m.calculateLinuxResources(req.Cpu(), lim.Cpu(), lim.Memory()) | 	var cpuRequest *resource.Quantity | ||||||
|  | 	if _, cpuRequestExists := req[v1.ResourceCPU]; cpuRequestExists { | ||||||
|  | 		cpuRequest = req.Cpu() | ||||||
|  | 	} | ||||||
|  | 	return m.calculateLinuxResources(cpuRequest, lim.Cpu(), lim.Memory()) | ||||||
| } | } | ||||||
|  |  | ||||||
| func (m *kubeGenericRuntimeManager) applySandboxResources(pod *v1.Pod, config *runtimeapi.PodSandboxConfig) error { | func (m *kubeGenericRuntimeManager) applySandboxResources(pod *v1.Pod, config *runtimeapi.PodSandboxConfig) error { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Kubernetes Prow Robot
					Kubernetes Prow Robot