简单总结
1.32前cpumanager对于非guaranteed或者request非整数,则仍然会使用reservedCPUs
1.32后新增了strict-cpu-reservation option,设置为true后,pod无法使用reservedCPUs
源码
pkg/kubelet/cm/cpumanager/cpu_manager.go中
构建cpumanager
func NewManager(cpuPolicyName string, cpuPolicyOptions map[string]string, reconcilePeriod time.Duration, machineInfo *cadvisorapi.MachineInfo, specificCPUs cpuset.CPUSet, nodeAllocatableReservation v1.ResourceList, stateFileDirectory string, affinity topologymanager.Store) (Manager, error) {
...
switch policyName(cpuPolicyName) {
...
case PolicyStatic:
...
policy, err = NewStaticPolicy(topo, numReservedCPUs, specificCPUs, affinity, cpuPolicyOptions)
...
...
return manager, nil
}
启动
func (m *manager) Start(activePods ActivePodsFunc, sourcesReady config.SourcesReady, podStatusProvider status.PodStatusProvider, containerRuntime runtimeService, initialContainers containermap.ContainerMap) error {
...
加载checkpoint
stateImpl, err := state.NewCheckpointState(m.stateFileDirectory, cpuManagerStateFileName, m.policy.Name(), m.containerMap)
if err != nil {
return err
}
m.state = stateImpl
启动policy
err = m.policy.Start(m.state)
if err != nil {
return err
}
}
pkg/kubelet/cm/cpumanager/policy_static.go中
构建policy
func NewStaticPolicy(topology *topology.CPUTopology, numReservedCPUs int, reservedCPUs cpuset.CPUSet, affinity topologymanager.Store, cpuPolicyOptions map[string]string) (Policy, error) {
...
return policy, nil
}
启动
func (p *staticPolicy) Start(s state.State) error {
校验状态
if err := p.validateState(s); err != nil {
}
return nil
}
校验状态
func (p *staticPolicy) validateState(s state.State) error {
...
tmpDefaultCPUset := s.GetDefaultCPUSet()
allCPUs := p.topology.CPUDetails.CPUs()
如果开启了strict-cpu-reservation,那么reservedCPUs不能包含在allCPUs中
1.32前没有这三行
if p.options.StrictCPUReservation {
allCPUs = allCPUs.Difference(p.reservedCPUs)
}
如果checkpoint中没有defaultCPUset
if tmpDefaultCPUset.IsEmpty() {
...
设置state中的defaultCPUset为allCPUs(已移除reservedCPUs)
s.SetDefaultCPUSet(allCPUs)
return nil
}
}
获取可用的cpu
func (p *staticPolicy) GetAvailableCPUs(s state.State) cpuset.CPUSet {
默认cpu去除reservedCPUs
return s.GetDefaultCPUSet().Difference(p.reservedCPUs)
}
分配cpu
func (p *staticPolicy) Allocate(s state.State, pod *v1.Pod, container *v1.Container) (rerr error) {
numCPUs := p.guaranteedCPUs(pod, container)
if numCPUs == 0 {
使用defaultCPUset(即上面移除了reservedCPUs的cpu)
return nil
}
...
}