From 5d9032007a3d1b0a0c1fa113d2800b75d7c41e7e Mon Sep 17 00:00:00 2001 From: Alexey Perevalov Date: Mon, 21 Dec 2020 04:21:25 -0500 Subject: [PATCH] Return only isolated cpus in podresources interface Co-Authored-by: Swati Sehgal Signed-off-by: Alexey Perevalov --- pkg/kubelet/cm/container_manager_linux.go | 2 +- pkg/kubelet/cm/cpumanager/cpu_manager.go | 20 +++++++++++++++---- pkg/kubelet/cm/cpumanager/fake_cpu_manager.go | 9 +++++++-- .../cm/internal_container_lifecycle_linux.go | 2 +- 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/pkg/kubelet/cm/container_manager_linux.go b/pkg/kubelet/cm/container_manager_linux.go index 1c39e3d7d07..09bd752d556 100644 --- a/pkg/kubelet/cm/container_manager_linux.go +++ b/pkg/kubelet/cm/container_manager_linux.go @@ -1064,7 +1064,7 @@ func (cm *containerManagerImpl) GetAllocatableDevices() []*podresourcesapi.Conta func (cm *containerManagerImpl) GetCPUs(podUID, containerName string) []int64 { if cm.cpuManager != nil { - return cm.cpuManager.GetCPUs(podUID, containerName).ToSliceNoSortInt64() + return cm.cpuManager.GetExclusiveCPUs(podUID, containerName).ToSliceNoSortInt64() } return []int64{} } diff --git a/pkg/kubelet/cm/cpumanager/cpu_manager.go b/pkg/kubelet/cm/cpumanager/cpu_manager.go index 4777c132e81..e95f77e32b0 100644 --- a/pkg/kubelet/cm/cpumanager/cpu_manager.go +++ b/pkg/kubelet/cm/cpumanager/cpu_manager.go @@ -77,9 +77,9 @@ type Manager interface { // and other resource controllers. GetTopologyHints(*v1.Pod, *v1.Container) map[string][]topologymanager.TopologyHint - // GetCPUs implements the podresources.CPUsProvider interface to provide allocated - // cpus for the container - GetCPUs(podUID, containerName string) cpuset.CPUSet + // GetExclusiveCPUs implements the podresources.CPUsProvider interface to provide + // exclusively allocated cpus for the container + GetExclusiveCPUs(podUID, containerName string) cpuset.CPUSet // GetPodTopologyHints implements the topologymanager.HintProvider Interface // and is consulted to achieve NUMA aware resource alignment per Pod @@ -88,6 +88,10 @@ type Manager interface { // GetAllocatableCPUs returns the assignable (not allocated) CPUs GetAllocatableCPUs() cpuset.CPUSet + + // GetCPUAffinity returns cpuset which includes cpus from shared pools + // as well as exclusively allocated cpus + GetCPUAffinity(podUID, containerName string) cpuset.CPUSet } type manager struct { @@ -506,7 +510,15 @@ func (m *manager) updateContainerCPUSet(containerID string, cpus cpuset.CPUSet) }) } -func (m *manager) GetCPUs(podUID, containerName string) cpuset.CPUSet { +func (m *manager) GetExclusiveCPUs(podUID, containerName string) cpuset.CPUSet { + if result, ok := m.state.GetCPUSet(string(podUID), containerName); ok { + return result + } + + return cpuset.CPUSet{} +} + +func (m *manager) GetCPUAffinity(podUID, containerName string) cpuset.CPUSet { return m.state.GetCPUSetOrDefault(podUID, containerName) } diff --git a/pkg/kubelet/cm/cpumanager/fake_cpu_manager.go b/pkg/kubelet/cm/cpumanager/fake_cpu_manager.go index 28578e6415d..93369705135 100644 --- a/pkg/kubelet/cm/cpumanager/fake_cpu_manager.go +++ b/pkg/kubelet/cm/cpumanager/fake_cpu_manager.go @@ -70,8 +70,8 @@ func (m *fakeManager) State() state.Reader { return m.state } -func (m *fakeManager) GetCPUs(podUID, containerName string) cpuset.CPUSet { - klog.InfoS("GetCPUs", "podUID", podUID, "containerName", containerName) +func (m *fakeManager) GetExclusiveCPUs(podUID, containerName string) cpuset.CPUSet { + klog.InfoS("GetExclusiveCPUs", "podUID", podUID, "containerName", containerName) return cpuset.CPUSet{} } @@ -80,6 +80,11 @@ func (m *fakeManager) GetAllocatableCPUs() cpuset.CPUSet { return cpuset.CPUSet{} } +func (m *fakeManager) GetCPUAffinity(podUID, containerName string) cpuset.CPUSet { + klog.InfoS("GetCPUAffinity", "podUID", podUID, "containerName", containerName) + return cpuset.CPUSet{} +} + // NewFakeManager creates empty/fake cpu manager func NewFakeManager() Manager { return &fakeManager{ diff --git a/pkg/kubelet/cm/internal_container_lifecycle_linux.go b/pkg/kubelet/cm/internal_container_lifecycle_linux.go index e731a4e5872..2d4ff55f6d6 100644 --- a/pkg/kubelet/cm/internal_container_lifecycle_linux.go +++ b/pkg/kubelet/cm/internal_container_lifecycle_linux.go @@ -29,7 +29,7 @@ import ( func (i *internalContainerLifecycleImpl) PreCreateContainer(pod *v1.Pod, container *v1.Container, containerConfig *runtimeapi.ContainerConfig) error { if i.cpuManager != nil { - allocatedCPUs := i.cpuManager.GetCPUs(string(pod.UID), container.Name) + allocatedCPUs := i.cpuManager.GetCPUAffinity(string(pod.UID), container.Name) if !allocatedCPUs.IsEmpty() { containerConfig.Linux.Resources.CpusetCpus = allocatedCPUs.String() }