github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/plugins/noderesourcetopology/reserve.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package noderesourcetopology
    18  
    19  import (
    20  	"context"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/util/sets"
    24  	"k8s.io/klog/v2"
    25  	"k8s.io/kubernetes/pkg/scheduler/framework"
    26  
    27  	"github.com/kubewharf/katalyst-api/pkg/apis/node/v1alpha1"
    28  	"github.com/kubewharf/katalyst-core/pkg/scheduler/cache"
    29  	"github.com/kubewharf/katalyst-core/pkg/scheduler/util"
    30  )
    31  
    32  func (tm *TopologyMatch) Reserve(ctx context.Context, state *framework.CycleState, pod *corev1.Pod, nodeName string) *framework.Status {
    33  	if tm.topologyMatchSupport(pod) {
    34  		podCopy := pod.DeepCopy()
    35  		if util.IsNumaBinding(pod) && util.IsExclusive(pod) {
    36  			// get node NUMA capacity
    37  			rt := cache.GetCache().GetNodeResourceTopology(nodeName, nil)
    38  			var numaCapacity *corev1.ResourceList
    39  			for _, topologyZone := range rt.TopologyZone {
    40  				if topologyZone.Type != v1alpha1.TopologyTypeSocket {
    41  					continue
    42  				}
    43  				for _, child := range topologyZone.Children {
    44  					if child.Type != v1alpha1.TopologyTypeNuma {
    45  						continue
    46  					}
    47  					numaCapacity = child.Resources.Capacity
    48  					break
    49  				}
    50  				if numaCapacity != nil {
    51  					break
    52  				}
    53  			}
    54  			if numaCapacity == nil {
    55  				klog.Warningf("[TopologyMatch] reserve get node %v NUMA capacity fail")
    56  			} else {
    57  				adjustExclusivePodRequest(podCopy, *numaCapacity, tm.alignedResources)
    58  			}
    59  		}
    60  		cache.GetCache().ReserveNodeResource(nodeName, podCopy)
    61  	}
    62  	return framework.NewStatus(framework.Success, "")
    63  }
    64  
    65  func (tm *TopologyMatch) Unreserve(ctx context.Context, state *framework.CycleState, pod *corev1.Pod, nodeName string) {
    66  	if tm.topologyMatchSupport(pod) {
    67  		cache.GetCache().UnreserveNodeResource(nodeName, pod)
    68  	}
    69  }
    70  
    71  func adjustExclusivePodRequest(pod *corev1.Pod, capacity corev1.ResourceList, alignedResource sets.String) {
    72  	NUMAFit := false
    73  
    74  	podRequests := util.GetPodEffectiveRequest(pod)
    75  
    76  	for resourceName, request := range podRequests {
    77  		if !alignedResource.Has(resourceName.String()) {
    78  			continue
    79  		}
    80  
    81  		quantity, ok := capacity[resourceName]
    82  		if !ok {
    83  			continue
    84  		}
    85  
    86  		if request.Cmp(quantity) < 0 {
    87  			NUMAFit = true
    88  			podRequests[resourceName] = quantity
    89  		}
    90  	}
    91  
    92  	if NUMAFit {
    93  		pod.Spec.InitContainers = []corev1.Container{}
    94  		pod.Spec.Containers = []corev1.Container{
    95  			{
    96  				Name: "fake-container",
    97  				Resources: corev1.ResourceRequirements{
    98  					Limits:   podRequests,
    99  					Requests: podRequests,
   100  				},
   101  			},
   102  		}
   103  	}
   104  }