Skip to content

Commit d09f8b9

Browse files
authored
Merge pull request kubernetes#79409 from takmatsu/add-phase
Modify Kubelet Pod Resources API to get only active pods
2 parents 415b3ed + 785fac6 commit d09f8b9

File tree

10 files changed

+36
-6
lines changed

10 files changed

+36
-6
lines changed

pkg/kubelet/apis/podresources/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
// DevicesProvider knows how to provide the devices used by the given container
2727
type DevicesProvider interface {
2828
GetDevices(podUID, containerName string) []*v1alpha1.ContainerDevices
29+
UpdateAllocatedDevices()
2930
}
3031

3132
// PodsProvider knows how to provide the pods admitted by the node
@@ -52,6 +53,7 @@ func NewPodResourcesServer(podsProvider PodsProvider, devicesProvider DevicesPro
5253
func (p *podResourcesServer) List(ctx context.Context, req *v1alpha1.ListPodResourcesRequest) (*v1alpha1.ListPodResourcesResponse, error) {
5354
pods := p.podsProvider.GetPods()
5455
podResources := make([]*v1alpha1.PodResources, len(pods))
56+
p.devicesProvider.UpdateAllocatedDevices()
5557

5658
for i, pod := range pods {
5759
pRes := v1alpha1.PodResources{

pkg/kubelet/apis/podresources/server_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func (m *mockProvider) GetDevices(podUID, containerName string) []*v1alpha1.Cont
4242
return args.Get(0).([]*v1alpha1.ContainerDevices)
4343
}
4444

45+
func (m *mockProvider) UpdateAllocatedDevices() {
46+
m.Called()
47+
}
48+
4549
func TestListPodResources(t *testing.T) {
4650
podName := "pod-name"
4751
podNamespace := "pod-namespace"
@@ -140,6 +144,7 @@ func TestListPodResources(t *testing.T) {
140144
m := new(mockProvider)
141145
m.On("GetPods").Return(tc.pods)
142146
m.On("GetDevices", string(podUID), containerName).Return(tc.devices)
147+
m.On("UpdateAllocatedDevices").Return()
143148
server := NewPodResourcesServer(m, m)
144149
resp, err := server.List(context.TODO(), &v1alpha1.ListPodResourcesRequest{})
145150
if err != nil {

pkg/kubelet/cm/container_manager.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ type ContainerManager interface {
113113

114114
// GetTopologyPodAdmitHandler returns an instance of the TopologyManager for Pod Admission
115115
GetTopologyPodAdmitHandler() topologymanager.Manager
116+
117+
// UpdateAllocatedDevices frees any Devices that are bound to terminated pods.
118+
UpdateAllocatedDevices()
116119
}
117120

118121
type NodeConfig struct {

pkg/kubelet/cm/container_manager_linux.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,3 +946,7 @@ func (cm *containerManagerImpl) GetDevices(podUID, containerName string) []*podr
946946
func (cm *containerManagerImpl) ShouldResetExtendedResourceCapacity() bool {
947947
return cm.deviceManager.ShouldResetExtendedResourceCapacity()
948948
}
949+
950+
func (cm *containerManagerImpl) UpdateAllocatedDevices() {
951+
cm.deviceManager.UpdateAllocatedDevices()
952+
}

pkg/kubelet/cm/container_manager_stub.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ func (cm *containerManagerStub) GetTopologyPodAdmitHandler() topologymanager.Man
121121
return nil
122122
}
123123

124+
func (cm *containerManagerStub) UpdateAllocatedDevices() {
125+
return
126+
}
127+
124128
func NewStubContainerManager() ContainerManager {
125129
return &containerManagerStub{shouldResetExtendedResourceCapacity: false}
126130
}

pkg/kubelet/cm/container_manager_windows.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,7 @@ func (cm *containerManagerImpl) ShouldResetExtendedResourceCapacity() bool {
181181
func (cm *containerManagerImpl) GetTopologyPodAdmitHandler() topologymanager.Manager {
182182
return nil
183183
}
184+
185+
func (cm *containerManagerImpl) UpdateAllocatedDevices() {
186+
return
187+
}

pkg/kubelet/cm/devicemanager/manager.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,9 @@ func (m *ManagerImpl) readCheckpoint() error {
597597
return nil
598598
}
599599

600-
// updateAllocatedDevices gets a list of active pods and then frees any Devices that are bound to
601-
// terminated pods. Returns error on failure.
602-
func (m *ManagerImpl) updateAllocatedDevices(activePods []*v1.Pod) {
600+
// UpdateAllocatedDevices frees any Devices that are bound to terminated pods.
601+
func (m *ManagerImpl) UpdateAllocatedDevices() {
602+
activePods := m.activePods()
603603
if !m.sourcesReady.AllReady() {
604604
return
605605
}
@@ -773,7 +773,7 @@ func (m *ManagerImpl) allocateContainerResources(pod *v1.Pod, container *v1.Cont
773773
// Updates allocatedDevices to garbage collect any stranded resources
774774
// before doing the device plugin allocation.
775775
if !allocatedDevicesUpdated {
776-
m.updateAllocatedDevices(m.activePods())
776+
m.UpdateAllocatedDevices()
777777
allocatedDevicesUpdated = true
778778
}
779779
allocDevices, err := m.devicesToAllocate(podUID, contName, resource, needed, devicesToReuse[resource])
@@ -788,7 +788,7 @@ func (m *ManagerImpl) allocateContainerResources(pod *v1.Pod, container *v1.Cont
788788
// Manager.Allocate involves RPC calls to device plugin, which
789789
// could be heavy-weight. Therefore we want to perform this operation outside
790790
// mutex lock. Note if Allocate call fails, we may leave container resources
791-
// partially allocated for the failed container. We rely on updateAllocatedDevices()
791+
// partially allocated for the failed container. We rely on UpdateAllocatedDevices()
792792
// to garbage collect these resources later. Another side effect is that if
793793
// we have X resource A and Y resource B in total, and two containers, container1
794794
// and container2 both require X resource A and Y resource B. Both allocation

pkg/kubelet/cm/devicemanager/manager_stub.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,8 @@ func (h *ManagerStub) GetDevices(_, _ string) []*podresourcesapi.ContainerDevice
7878
func (h *ManagerStub) ShouldResetExtendedResourceCapacity() bool {
7979
return false
8080
}
81+
82+
// UpdateAllocatedDevices returns nothing
83+
func (h *ManagerStub) UpdateAllocatedDevices() {
84+
return
85+
}

pkg/kubelet/cm/devicemanager/topology_hints.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// container are created.
3030
func (m *ManagerImpl) GetTopologyHints(pod *v1.Pod, container *v1.Container) map[string][]topologymanager.TopologyHint {
3131
// Garbage collect any stranded device resources before providing TopologyHints
32-
m.updateAllocatedDevices(m.activePods())
32+
m.UpdateAllocatedDevices()
3333

3434
// Loop through all device resources and generate TopologyHints for them..
3535
deviceHints := make(map[string][]topologymanager.TopologyHint)

pkg/kubelet/cm/devicemanager/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ type Manager interface {
6868
// TopologyManager HintProvider provider indicates the Device Manager implements the Topology Manager Interface
6969
// and is consulted to make Topology aware resource alignments
7070
GetTopologyHints(pod *v1.Pod, container *v1.Container) map[string][]topologymanager.TopologyHint
71+
72+
// UpdateAllocatedDevices frees any Devices that are bound to terminated pods.
73+
UpdateAllocatedDevices()
7174
}
7275

7376
// DeviceRunContainerOptions contains the combined container runtime settings to consume its allocated devices.

0 commit comments

Comments
 (0)