Skip to content

Commit 0b168f0

Browse files
klueskanolancon
authored andcommitted
Change devicemanager to implement HintProvider.Allocate()
This change will not work on its own. Higher level code needs to make sure and call Allocate() before AddContainer is called. This is already being done in cases when the TopologyManager feature gate is enabled (in the PodAdmitHandler of the TopologyManager). However, we need to make sure we add proper logic to call it in cases when the TopologyManager feature gate is disabled.
1 parent 91f9185 commit 0b168f0

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

pkg/kubelet/cm/devicemanager/manager.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,30 @@ func (m *ManagerImpl) allocatePodResources(pod *v1.Pod) error {
369369

370370
// Allocate is the call that you can use to allocate a set of devices
371371
// from the registered device plugins.
372-
func (m *ManagerImpl) Allocate(pod *v1.Pod) error {
372+
func (m *ManagerImpl) Allocate(pod *v1.Pod, container *v1.Container) error {
373+
// TODO: This function does not yet do what it is supposed to. The call to
374+
// allocatePodResources() below is still allocating devices to a pod all at
375+
// once. We need to unroll this logic to allow it to allocate devices on a
376+
// container-by-container basis instead. The main challenge will be
377+
// ensuring that we "reuse" devices from init containers when allocating
378+
// devices to app containers (just as the logic inside
379+
// allocatePodResources() currently does). The hard part being that we will
380+
// need to maintain the 'devicesToReuse' present in allocatePodResources()
381+
// across invocations of Allocate().
382+
//
383+
// My initial inclination to solve this with the least coode churn is:
384+
// 1) Create a new type called PodReusableDevices, defined as:
385+
// type PodReusableDevices map[string]map[string]sets.String
386+
// 2) Instantiate a PodReusableDevices map as a new field of the
387+
// devicemanager called devicesToReuse (similar to the local
388+
// devicesToReuse variable currently in allocatePodResources)
389+
// 3) Use devicesToReuse[string(pod.UID) just as devicesToReuse is used
390+
// today, being careful to create / destroy the nested maps where
391+
// appropriate.
392+
373393
err := m.allocatePodResources(pod)
374394
if err != nil {
375-
klog.Errorf("Failed to allocate device plugin resource for pod %s: %v", string(pod.UID), err)
395+
klog.Errorf("Failed to allocate device plugin resource for pod %s, container %s: %v", string(pod.UID), container.Name, err)
376396
return err
377397
}
378398
return nil
@@ -864,8 +884,8 @@ func (m *ManagerImpl) GetDeviceRunContainerOptions(pod *v1.Pod, container *v1.Co
864884
}
865885
}
866886
if needsReAllocate {
867-
klog.V(2).Infof("needs re-allocate device plugin resources for pod %s", podUID)
868-
if err := m.allocatePodResources(pod); err != nil {
887+
klog.V(2).Infof("needs re-allocate device plugin resources for pod %s, container %s", podUID, container.Name)
888+
if err := m.Allocate(pod, container); err != nil {
869889
return nil, err
870890
}
871891
}

pkg/kubelet/cm/devicemanager/manager_stub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (h *ManagerStub) Stop() error {
4545
}
4646

4747
// Allocate simply returns nil.
48-
func (h *ManagerStub) Allocate(pod *v1.Pod) error {
48+
func (h *ManagerStub) Allocate(pod *v1.Pod, container *v1.Container) error {
4949
return nil
5050
}
5151

pkg/kubelet/cm/devicemanager/types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ type Manager interface {
3434
// Start starts device plugin registration service.
3535
Start(activePods ActivePodsFunc, sourcesReady config.SourcesReady) error
3636

37-
// Allocate configures and assigns devices to a pod. From the requested
38-
// device resources, Allocate will communicate with the owning device
39-
// plugin to allow setup procedures to take place, and for the device
40-
// plugin to provide runtime settings to use the device (environment
41-
// variables, mount points and device files).
42-
Allocate(pod *v1.Pod) error
37+
// Allocate configures and assigns devices to a container in a pod. From
38+
// the requested device resources, Allocate will communicate with the
39+
// owning device plugin to allow setup procedures to take place, and for
40+
// the device plugin to provide runtime settings to use the device
41+
// (environment variables, mount points and device files).
42+
Allocate(pod *v1.Pod, container *v1.Container) error
4343

4444
// UpdatePluginResources updates node resources based on devices already
4545
// allocated to pods. The node object is provided for the device manager to

0 commit comments

Comments
 (0)