Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing isExclusive field as part of ContainerResources in PodResource API #102989

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions pkg/kubelet/apis/podresources/server_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
kubefeatures "k8s.io/kubernetes/pkg/features"
"k8s.io/kubernetes/pkg/kubelet/metrics"

"k8s.io/kubelet/pkg/apis/podresources/v1"
v1 "k8s.io/kubelet/pkg/apis/podresources/v1"
)

// podResourcesServerV1alpha1 implements PodResourcesListerServer
Expand Down Expand Up @@ -63,11 +63,13 @@ func (p *v1PodResourcesServer) List(ctx context.Context, req *v1.ListPodResource
}

for j, container := range pod.Spec.Containers {
cpus, isExclusive := p.cpusProvider.GetCPUs(string(pod.UID), container.Name)
pRes.Containers[j] = &v1.ContainerResources{
Name: container.Name,
Devices: p.devicesProvider.GetDevices(string(pod.UID), container.Name),
CpuIds: p.cpusProvider.GetCPUs(string(pod.UID), container.Name),
Memory: p.memoryProvider.GetMemory(string(pod.UID), container.Name),
Name: container.Name,
Devices: p.devicesProvider.GetDevices(string(pod.UID), container.Name),
CpuIds: cpus,
Memory: p.memoryProvider.GetMemory(string(pod.UID), container.Name),
IsExclusive: isExclusive,
}
}
podResources[i] = &pRes
Expand Down
36 changes: 21 additions & 15 deletions pkg/kubelet/apis/podresources/server_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"sort"
"testing"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilfeature "k8s.io/apiserver/pkg/util/feature"
Expand Down Expand Up @@ -70,6 +70,7 @@ func TestListPodResourcesV1(t *testing.T) {
devices []*podresourcesapi.ContainerDevices
cpus []int64
memory []*podresourcesapi.ContainerMemory
isExclusive bool
expectedResponse *podresourcesapi.ListPodResourcesResponse
}{
{
Expand All @@ -78,6 +79,7 @@ func TestListPodResourcesV1(t *testing.T) {
devices: []*podresourcesapi.ContainerDevices{},
cpus: []int64{},
memory: []*podresourcesapi.ContainerMemory{},
isExclusive: false,
expectedResponse: &podresourcesapi.ListPodResourcesResponse{},
},
{
Expand All @@ -98,18 +100,20 @@ func TestListPodResourcesV1(t *testing.T) {
},
},
},
devices: []*podresourcesapi.ContainerDevices{},
cpus: []int64{},
memory: []*podresourcesapi.ContainerMemory{},
devices: []*podresourcesapi.ContainerDevices{},
cpus: []int64{},
memory: []*podresourcesapi.ContainerMemory{},
isExclusive: false,
expectedResponse: &podresourcesapi.ListPodResourcesResponse{
PodResources: []*podresourcesapi.PodResources{
{
Name: podName,
Namespace: podNamespace,
Containers: []*podresourcesapi.ContainerResources{
{
Name: containerName,
Devices: []*podresourcesapi.ContainerDevices{},
Name: containerName,
Devices: []*podresourcesapi.ContainerDevices{},
IsExclusive: false,
},
},
},
Expand All @@ -134,20 +138,22 @@ func TestListPodResourcesV1(t *testing.T) {
},
},
},
devices: devs,
cpus: cpus,
memory: memory,
devices: devs,
cpus: cpus,
memory: memory,
isExclusive: true,
expectedResponse: &podresourcesapi.ListPodResourcesResponse{
PodResources: []*podresourcesapi.PodResources{
{
Name: podName,
Namespace: podNamespace,
Containers: []*podresourcesapi.ContainerResources{
{
Name: containerName,
Devices: devs,
CpuIds: cpus,
Memory: memory,
Name: containerName,
Devices: devs,
CpuIds: cpus,
Memory: memory,
IsExclusive: true,
},
},
},
Expand All @@ -159,7 +165,7 @@ func TestListPodResourcesV1(t *testing.T) {
m := new(mockProvider)
m.On("GetPods").Return(tc.pods)
m.On("GetDevices", string(podUID), containerName).Return(tc.devices)
m.On("GetCPUs", string(podUID), containerName).Return(tc.cpus)
m.On("GetCPUs", string(podUID), containerName).Return(tc.cpus, tc.isExclusive)
m.On("GetMemory", string(podUID), containerName).Return(tc.memory)
m.On("UpdateAllocatedDevices").Return()
m.On("GetAllocatableCPUs").Return(cpuset.CPUSet{})
Expand Down Expand Up @@ -438,7 +444,7 @@ func TestAllocatableResources(t *testing.T) {
t.Run(tc.desc, func(t *testing.T) {
m := new(mockProvider)
m.On("GetDevices", "", "").Return([]*podresourcesapi.ContainerDevices{})
m.On("GetCPUs", "", "").Return([]int64{})
m.On("GetCPUs", "", "").Return([]int64{}, false)
m.On("GetMemory", "", "").Return([]*podresourcesapi.ContainerMemory{})
m.On("UpdateAllocatedDevices").Return()
m.On("GetAllocatableDevices").Return(tc.allDevices)
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/apis/podresources/server_v1alpha1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func (m *mockProvider) GetDevices(podUID, containerName string) []*podresourcesv
return args.Get(0).([]*podresourcesv1.ContainerDevices)
}

func (m *mockProvider) GetCPUs(podUID, containerName string) []int64 {
func (m *mockProvider) GetCPUs(podUID, containerName string) ([]int64, bool) {
args := m.Called(podUID, containerName)
return args.Get(0).([]int64)
return args.Get(0).([]int64), false
}

func (m *mockProvider) GetMemory(podUID, containerName string) []*podresourcesv1.ContainerMemory {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/apis/podresources/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type PodsProvider interface {
// CPUsProvider knows how to provide the cpus used by the given container
type CPUsProvider interface {
// GetCPUs returns information about the cpus assigned to pods and containers
GetCPUs(podUID, containerName string) []int64
GetCPUs(podUID, containerName string) ([]int64, bool)
// GetAllocatableCPUs returns the allocatable (not allocated) CPUs
GetAllocatableCPUs() []int64
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/kubelet/cm/container_manager_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1073,11 +1073,12 @@ func (cm *containerManagerImpl) GetAllocatableDevices() []*podresourcesapi.Conta
return containerDevicesFromResourceDeviceInstances(cm.deviceManager.GetAllocatableDevices())
}

func (cm *containerManagerImpl) GetCPUs(podUID, containerName string) []int64 {
func (cm *containerManagerImpl) GetCPUs(podUID, containerName string) ([]int64, bool) {
if cm.cpuManager != nil {
return cm.cpuManager.GetCPUs(podUID, containerName).ToSliceNoSortInt64()
cpus, isExclusive := cm.cpuManager.GetCPUs(podUID, containerName)
return cpus.ToSliceNoSortInt64(), isExclusive
}
return []int64{}
return []int64{}, false
}

func (cm *containerManagerImpl) GetAllocatableCPUs() []int64 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/cm/container_manager_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ func (cm *containerManagerStub) UpdateAllocatedDevices() {
return
}

func (cm *containerManagerStub) GetCPUs(_, _ string) []int64 {
return nil
func (cm *containerManagerStub) GetCPUs(_, _ string) ([]int64, bool) {
return nil, false
}

func (cm *containerManagerStub) GetAllocatableCPUs() []int64 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/cm/container_manager_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ func (cm *containerManagerImpl) UpdateAllocatedDevices() {
return
}

func (cm *containerManagerImpl) GetCPUs(_, _ string) []int64 {
return nil
func (cm *containerManagerImpl) GetCPUs(_, _ string) ([]int64, bool) {
return nil, false
}

func (cm *containerManagerImpl) GetAllocatableCPUs() []int64 {
Expand Down
12 changes: 8 additions & 4 deletions pkg/kubelet/cm/cpumanager/cpu_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ type Manager interface {
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
// cpus for the container and also returns a boolean indicative of whether the CPUs
// are exclusively allocated or not
GetCPUs(podUID, containerName string) (cpuset.CPUSet, bool)

// GetPodTopologyHints implements the topologymanager.HintProvider Interface
// and is consulted to achieve NUMA aware resource alignment per Pod
Expand Down Expand Up @@ -487,6 +488,9 @@ func (m *manager) updateContainerCPUSet(containerID string, cpus cpuset.CPUSet)
})
}

func (m *manager) GetCPUs(podUID, containerName string) cpuset.CPUSet {
return m.state.GetCPUSetOrDefault(podUID, containerName)
func (m *manager) GetCPUs(podUID, containerName string) (cpuset.CPUSet, bool) {
if res, ok := m.state.GetCPUSet(podUID, containerName); ok {
return res, true
}
return m.state.GetDefaultCPUSet(), false
}
4 changes: 2 additions & 2 deletions pkg/kubelet/cm/cpumanager/fake_cpu_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ func (m *fakeManager) State() state.Reader {
return m.state
}

func (m *fakeManager) GetCPUs(podUID, containerName string) cpuset.CPUSet {
func (m *fakeManager) GetCPUs(podUID, containerName string) (cpuset.CPUSet, bool) {
klog.InfoS("GetCPUs", "podUID", podUID, "containerName", containerName)
return cpuset.CPUSet{}
return cpuset.CPUSet{}, false
}

func (m *fakeManager) GetAllocatableCPUs() cpuset.CPUSet {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kubelet/cm/fake_container_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ func (cm *FakeContainerManager) UpdateAllocatedDevices() {
return
}

func (cm *FakeContainerManager) GetCPUs(_, _ string) []int64 {
func (cm *FakeContainerManager) GetCPUs(_, _ string) ([]int64, bool) {
cm.Lock()
defer cm.Unlock()
cm.CalledFunctions = append(cm.CalledFunctions, "GetCPUs")
return nil
return nil, false
}

func (cm *FakeContainerManager) GetAllocatableCPUs() []int64 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/cm/internal_container_lifecycle_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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.GetCPUs(string(pod.UID), container.Name)
if !allocatedCPUs.IsEmpty() {
containerConfig.Linux.Resources.CpusetCpus = allocatedCPUs.String()
}
Expand Down