diff --git a/pkg/kubelet/cm/devicemanager/manager.go b/pkg/kubelet/cm/devicemanager/manager.go index 3925a3fd6e6..424aacfc224 100644 --- a/pkg/kubelet/cm/devicemanager/manager.go +++ b/pkg/kubelet/cm/devicemanager/manager.go @@ -108,6 +108,9 @@ type ManagerImpl struct { // devicesToReuse contains devices that can be reused as they have been allocated to // init containers. devicesToReuse PodReusableDevices + + // pendingAdmissionPod contain the pod during the admission phase + pendingAdmissionPod *v1.Pod } type endpointInfo struct { @@ -367,6 +370,10 @@ func (m *ManagerImpl) isVersionCompatibleWithPlugin(versions []string) bool { // Allocate is the call that you can use to allocate a set of devices // from the registered device plugins. func (m *ManagerImpl) Allocate(pod *v1.Pod, container *v1.Container) error { + // The pod is during the admission phase. We need to save the pod to avoid it + // being cleaned before the admission ended + m.setPodPendingAdmission(pod) + if _, ok := m.devicesToReuse[string(pod.UID)]; !ok { m.devicesToReuse[string(pod.UID)] = make(map[string]sets.String) } @@ -619,14 +626,20 @@ func (m *ManagerImpl) readCheckpoint() error { // UpdateAllocatedDevices frees any Devices that are bound to terminated pods. func (m *ManagerImpl) UpdateAllocatedDevices() { - activePods := m.activePods() if !m.sourcesReady.AllReady() { return } + m.mutex.Lock() defer m.mutex.Unlock() + + activeAndAdmittedPods := m.activePods() + if m.pendingAdmissionPod != nil { + activeAndAdmittedPods = append(activeAndAdmittedPods, m.pendingAdmissionPod) + } + podsToBeRemoved := m.podDevices.pods() - for _, pod := range activePods { + for _, pod := range activeAndAdmittedPods { podsToBeRemoved.Delete(string(pod.UID)) } if len(podsToBeRemoved) <= 0 { @@ -1117,3 +1130,10 @@ func (m *ManagerImpl) ShouldResetExtendedResourceCapacity() bool { } return false } + +func (m *ManagerImpl) setPodPendingAdmission(pod *v1.Pod) { + m.mutex.Lock() + defer m.mutex.Unlock() + + m.pendingAdmissionPod = pod +} diff --git a/pkg/kubelet/cm/devicemanager/topology_hints.go b/pkg/kubelet/cm/devicemanager/topology_hints.go index 32f26f139a8..4b094c7adbf 100644 --- a/pkg/kubelet/cm/devicemanager/topology_hints.go +++ b/pkg/kubelet/cm/devicemanager/topology_hints.go @@ -29,6 +29,10 @@ import ( // ensures the Device Manager is consulted when Topology Aware Hints for each // container are created. func (m *ManagerImpl) GetTopologyHints(pod *v1.Pod, container *v1.Container) map[string][]topologymanager.TopologyHint { + // The pod is during the admission phase. We need to save the pod to avoid it + // being cleaned before the admission ended + m.setPodPendingAdmission(pod) + // Garbage collect any stranded device resources before providing TopologyHints m.UpdateAllocatedDevices() @@ -83,6 +87,10 @@ func (m *ManagerImpl) GetTopologyHints(pod *v1.Pod, container *v1.Container) map // GetPodTopologyHints implements the topologymanager.HintProvider Interface which // ensures the Device Manager is consulted when Topology Aware Hints for Pod are created. func (m *ManagerImpl) GetPodTopologyHints(pod *v1.Pod) map[string][]topologymanager.TopologyHint { + // The pod is during the admission phase. We need to save the pod to avoid it + // being cleaned before the admission ended + m.setPodPendingAdmission(pod) + // Garbage collect any stranded device resources before providing TopologyHints m.UpdateAllocatedDevices()