cm: handle nil cpumanager avoiding segfault

If the cpumanager feature gate is disabled, the corresponsing field
of the containerManager will be nil.
A couple functions don't check for this occurrence and happily
deference the pointer unconditionally, leading to possible segfaults.

The relevant functions were introduced to support the podresources API,
so to trigger this segfault all the following are needed:
- cpumanager feature gate has to be disabled explicitely
- any podresources API must be called

Worth pointing out that when the new functions were introduced (around
kubernetes 1.20) the default feature gate for cpumanager was already set
to true, hence this bug is expected to be triggered rarely.

Signed-off-by: Francesco Romani <fromani@redhat.com>
This commit is contained in:
Francesco Romani 2021-06-10 16:22:43 +02:00
parent 0d9c29078b
commit 369416b763

View File

@ -1073,11 +1073,17 @@ func (cm *containerManagerImpl) GetAllocatableDevices() []*podresourcesapi.Conta
} }
func (cm *containerManagerImpl) GetCPUs(podUID, containerName string) []int64 { func (cm *containerManagerImpl) GetCPUs(podUID, containerName string) []int64 {
return cm.cpuManager.GetCPUs(podUID, containerName).ToSliceNoSortInt64() if cm.cpuManager != nil {
return cm.cpuManager.GetCPUs(podUID, containerName).ToSliceNoSortInt64()
}
return []int64{}
} }
func (cm *containerManagerImpl) GetAllocatableCPUs() []int64 { func (cm *containerManagerImpl) GetAllocatableCPUs() []int64 {
return cm.cpuManager.GetAllocatableCPUs().ToSliceNoSortInt64() if cm.cpuManager != nil {
return cm.cpuManager.GetAllocatableCPUs().ToSliceNoSortInt64()
}
return []int64{}
} }
func (cm *containerManagerImpl) ShouldResetExtendedResourceCapacity() bool { func (cm *containerManagerImpl) ShouldResetExtendedResourceCapacity() bool {