Skip to content

Commit 467f665

Browse files
author
nolancon
committed
CPU Manager - Add check to policy.Allocate() for init conatiners
If container allocated CPUs is an init container, release those CPUs back into the shared pool for re-allocation to next container.
1 parent 709989e commit 467f665

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

pkg/kubelet/cm/cpumanager/cpu_manager.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -215,22 +215,6 @@ func (m *manager) Allocate(p *v1.Pod, c *v1.Container) error {
215215
m.Lock()
216216
defer m.Unlock()
217217

218-
// TODO: Reuse CPUs allocated to init containers to mimic functionality
219-
// from previous implementation below. Logic should probably be pushed into
220-
// the policy now instead of doing it at this level.
221-
222-
//// Proactively remove CPUs from init containers that have already run.
223-
//// They are guaranteed to have run to completion before any other
224-
//// container is run.
225-
//for _, initContainer := range p.Spec.InitContainers {
226-
// if c.Name != initContainer.Name {
227-
// err := m.policyRemoveContainerByRef(string(p.UID), initContainer.Name)
228-
// if err != nil {
229-
// klog.Warningf("[cpumanager] unable to remove init container (pod: %s, container: %s, error: %v)", string(p.UID), initContainer.Name, err)
230-
// }
231-
// }
232-
//}
233-
234218
// Call down into the policy to assign this container CPUs if required.
235219
err := m.policy.Allocate(m.state, p, c)
236220
if err != nil {

pkg/kubelet/cm/cpumanager/policy_static.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ func (p *staticPolicy) Allocate(s state.State, pod *v1.Pod, container *v1.Contai
209209
return err
210210
}
211211
s.SetCPUSet(string(pod.UID), container.Name, cpuset)
212+
213+
// Check if the container that has just been allocated resources is an init container.
214+
// If so, release its CPUs back into the shared pool so they can be reallocated.
215+
for _, initContainer := range pod.Spec.InitContainers {
216+
if container.Name == initContainer.Name {
217+
if toRelease, ok := s.GetCPUSet(string(pod.UID), container.Name); ok {
218+
// Mutate the shared pool, adding released cpus.
219+
s.SetDefaultCPUSet(s.GetDefaultCPUSet().Union(toRelease))
220+
}
221+
}
222+
}
212223
}
213224
// container belongs in the shared pool (nothing to do; use default cpuset)
214225
return nil

0 commit comments

Comments
 (0)