Skip to content

Commit 8c6025d

Browse files
committed
Use RunInContainer to get the mustKeepCPUs
1 parent 7c03d97 commit 8c6025d

File tree

5 files changed

+291
-99
lines changed

5 files changed

+291
-99
lines changed

pkg/apis/core/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2495,6 +2495,8 @@ type ResourceRequirements struct {
24952495
// +featureGate=DynamicResourceAllocation
24962496
// +optional
24972497
Claims []ResourceClaim
2498+
// Add mustKeepCPUs
2499+
MustKeepCPUs string
24982500
}
24992501

25002502
// VolumeResourceRequirements describes the storage resource requirements for a volume.

pkg/kubelet/cm/cpumanager/policy_static.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,12 @@ func (p *staticPolicy) Allocate(s state.State, pod *v1.Pod, container *v1.Contai
483483
// Attempt new allocation ( reusing allocated CPUs ) according to the NUMA affinity contained in the hint
484484
// Since NUMA affinity container in the hint is unmutable already allocated CPUs pass the criteria
485485
if mustKeepCPUsForResize, ok := s.GetPromisedCPUSet(string(pod.UID), container.Name); ok {
486+
mustKeepCPUsBaseFromContainer := p.GetMustKeepCPUs(container, cpuset)
487+
if mustKeepCPUsBasePerCpuUsage != nil {
488+
if mustKeepCPUsForResize.IsSubsetOf(mustKeepCPUsBaseFromContainer) && mustKeepCPUsBaseFromContainer.Size() < numCPUs {
489+
mustKeepCPUsForResize = mustKeepCPUsBaseFromContainer
490+
}
491+
}
486492
newallocatedcpuset, err := p.allocateCPUs(s, numCPUs, hint.NUMANodeAffinity, p.cpusToReuse[string(pod.UID)], &cpusInUseByPodContainerToResize, &mustKeepCPUsForResize)
487493
if err != nil {
488494
klog.ErrorS(err, "Static policy: Unable to allocate new CPUs", "pod", klog.KObj(pod), "containerName", container.Name, "numCPUs", numCPUs)
@@ -542,6 +548,30 @@ func (p *staticPolicy) Allocate(s state.State, pod *v1.Pod, container *v1.Contai
542548
return nil
543549
}
544550

551+
func (p *staticPolicy) GetMustKeepCPUs(container *v1.Container, oldCpuset cpuset.CPUSet) *cpuset.CPUSet {
552+
mustKeepCPUs := cpuset.New()
553+
klog.InfoS("GetMustKeepCPUs", "container.Resources.MustKeepCPUs", container.Resources.MustKeepCPUs)
554+
ResourcesMustKeepCPUs, err := cpuset.Parse(container.Resources.MustKeepCPUs)
555+
if err == nil && ResourcesMustKeepCPUs.Size() != 0 {
556+
mustKeepCPUs = oldCpuset.Intersection(ResourcesMustKeepCPUs)
557+
}
558+
klog.InfoS("mustKeepCPUs ", "is", mustKeepCPUs)
559+
if p.options.FullPhysicalCPUsOnly {
560+
// mustKeepCPUs must be aligned to the physical core
561+
if (mustKeepCPUs.Size() % 2) != 0 {
562+
return nil
563+
}
564+
mustKeepCPUsDetail := p.topology.CPUDetails.KeepOnly(mustKeepCPUs)
565+
mustKeepCPUsDetailCores := mustKeepCPUsDetail.Cores()
566+
if (mustKeepCPUs.Size() / mustKeepCPUsDetailCores.Size()) != p.cpuGroupSize {
567+
klog.InfoS("mustKeepCPUs is nil")
568+
return nil
569+
}
570+
}
571+
klog.InfoS("GetMustKeepCPUs", "mustKeepCPUs", mustKeepCPUs)
572+
return &mustKeepCPUs
573+
}
574+
545575
// getAssignedCPUsOfSiblings returns assigned cpus of given container's siblings(all containers other than the given container) in the given pod `podUID`.
546576
func getAssignedCPUsOfSiblings(s state.State, podUID string, containerName string) cpuset.CPUSet {
547577
assignments := s.GetCPUAssignments()

pkg/kubelet/kubelet.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,6 +2861,31 @@ func (kl *Kubelet) HandlePodSyncs(pods []*v1.Pod) {
28612861
}
28622862
}
28632863

2864+
func (kl *Kubelet) getMustKeepCPUs(pod *v1.Pod) error {
2865+
for i, container := range pod.Spec.Containers {
2866+
Command := strings.Split("cat /tmp/mustKeepCPUs", " ")
2867+
ctx := context.Background()
2868+
output, er := kl.RunInContainer(
2869+
ctx,
2870+
kubecontainer.GetPodFullName(pod),
2871+
pod.UID,
2872+
container.Name,
2873+
Command)
2874+
if er != nil {
2875+
klog.InfoS("Allocate: RunInContainer run error", "err", er)
2876+
continue
2877+
}
2878+
str := string(output)
2879+
str = strings.ReplaceAll(str, "&lt;", "")
2880+
str = strings.ReplaceAll(str, "&gt;", "")
2881+
str = strings.ReplaceAll(str, "\t", " ")
2882+
str = strings.ReplaceAll(str, "\n", "")
2883+
pod.Spec.Containers[i].Resources.MustKeepCPUs = str
2884+
klog.InfoS("RunInContainer", "kubecontainer.GetPodFullName(pod)", kubecontainer.GetPodFullName(pod), "container.Name", container.Name, "output:%s", output, "container.Resources.MustKeepCPUs", container.Resources.MustKeepCPUs)
2885+
}
2886+
return nil
2887+
}
2888+
28642889
// canResizePod determines if the requested resize is currently feasible.
28652890
// pod should hold the desired (pre-allocated) spec.
28662891
// Returns true if the resize can proceed; returns a reason and message
@@ -2882,6 +2907,12 @@ func (kl *Kubelet) canResizePod(pod *v1.Pod) (bool, string, string) {
28822907
}
28832908
}
28842909

2910+
if v1qos.GetPodQOS(pod) == v1.PodQOSGuaranteed && utilfeature.DefaultFeatureGate.Enabled(features.InPlacePodVerticalScalingExclusiveCPUs) {
2911+
if kl.containerManager.GetNodeConfig().CPUManagerPolicy == "static" {
2912+
kl.getMustKeepCPUs(pod)
2913+
}
2914+
}
2915+
28852916
node, err := kl.getNodeAnyWay()
28862917
if err != nil {
28872918
klog.ErrorS(err, "getNodeAnyway function failed")

staging/src/k8s.io/api/core/v1/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,6 +2707,8 @@ type ResourceRequirements struct {
27072707
// +featureGate=DynamicResourceAllocation
27082708
// +optional
27092709
Claims []ResourceClaim `json:"claims,omitempty" protobuf:"bytes,3,opt,name=claims"`
2710+
// Add mustKeepCPUs
2711+
MustKeepCPUs string
27102712
}
27112713

27122714
// VolumeResourceRequirements describes the storage resource requirements for a volume.

0 commit comments

Comments
 (0)