diff --git a/pkg/kubelet/cm/cpumanager/cpu_manager.go b/pkg/kubelet/cm/cpumanager/cpu_manager.go index ab19c2fa80d..616a620f8ce 100644 --- a/pkg/kubelet/cm/cpumanager/cpu_manager.go +++ b/pkg/kubelet/cm/cpumanager/cpu_manager.go @@ -215,22 +215,6 @@ func (m *manager) Allocate(p *v1.Pod, c *v1.Container) error { m.Lock() defer m.Unlock() - // TODO: Reuse CPUs allocated to init containers to mimic functionality - // from previous implementation below. Logic should probably be pushed into - // the policy now instead of doing it at this level. - - //// Proactively remove CPUs from init containers that have already run. - //// They are guaranteed to have run to completion before any other - //// container is run. - //for _, initContainer := range p.Spec.InitContainers { - // if c.Name != initContainer.Name { - // err := m.policyRemoveContainerByRef(string(p.UID), initContainer.Name) - // if err != nil { - // klog.Warningf("[cpumanager] unable to remove init container (pod: %s, container: %s, error: %v)", string(p.UID), initContainer.Name, err) - // } - // } - //} - // Call down into the policy to assign this container CPUs if required. err := m.policy.Allocate(m.state, p, c) if err != nil { diff --git a/pkg/kubelet/cm/cpumanager/policy_static.go b/pkg/kubelet/cm/cpumanager/policy_static.go index ba80c0145bd..da68ed808bd 100644 --- a/pkg/kubelet/cm/cpumanager/policy_static.go +++ b/pkg/kubelet/cm/cpumanager/policy_static.go @@ -209,6 +209,17 @@ func (p *staticPolicy) Allocate(s state.State, pod *v1.Pod, container *v1.Contai return err } s.SetCPUSet(string(pod.UID), container.Name, cpuset) + + // Check if the container that has just been allocated resources is an init container. + // If so, release its CPUs back into the shared pool so they can be reallocated. + for _, initContainer := range pod.Spec.InitContainers { + if container.Name == initContainer.Name { + if toRelease, ok := s.GetCPUSet(string(pod.UID), container.Name); ok { + // Mutate the shared pool, adding released cpus. + s.SetDefaultCPUSet(s.GetDefaultCPUSet().Union(toRelease)) + } + } + } } // container belongs in the shared pool (nothing to do; use default cpuset) return nil