github.com/kubewharf/katalyst-core@v0.5.3/pkg/scheduler/cache/resourcetopology.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 cache
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/kubewharf/katalyst-api/pkg/apis/node/v1alpha1"
    23  	"github.com/kubewharf/katalyst-core/pkg/util/native"
    24  )
    25  
    26  type ResourceTopology struct {
    27  	TopologyZone []*v1alpha1.TopologyZone
    28  
    29  	TopologyPolicy v1alpha1.TopologyPolicy
    30  }
    31  
    32  type podFilter func(consumer string) bool
    33  
    34  func (rt *ResourceTopology) Update(cnr *v1alpha1.CustomNodeResource) {
    35  	cp := cnr.DeepCopy()
    36  
    37  	rt.TopologyZone = cp.Status.TopologyZone
    38  
    39  	rt.TopologyPolicy = cp.Status.TopologyPolicy
    40  }
    41  
    42  // WithPodReousrce add assumedPodResource to ResourceTopology,
    43  // performing pessimistic overallocation across all the NUMA zones.
    44  func (rt *ResourceTopology) WithPodReousrce(podResource native.PodResource, filter podFilter) *ResourceTopology {
    45  	cp := rt.DeepCopy()
    46  
    47  	for i := range cp.TopologyZone {
    48  		if cp.TopologyZone[i].Type != v1alpha1.TopologyTypeSocket {
    49  			continue
    50  		}
    51  		for j, child := range cp.TopologyZone[i].Children {
    52  			if child.Type != v1alpha1.TopologyTypeNuma {
    53  				continue
    54  			}
    55  			allocation := make([]*v1alpha1.Allocation, 0)
    56  
    57  			if filter != nil {
    58  				for _, alloc := range child.Allocations {
    59  					if filter(alloc.Consumer) {
    60  						allocation = append(allocation, alloc.DeepCopy())
    61  					}
    62  				}
    63  			} else {
    64  				allocation = append(allocation, child.Allocations...)
    65  			}
    66  
    67  			for key, podReq := range podResource {
    68  				copyReq := podReq.DeepCopy()
    69  				fakeAllocation := v1alpha1.Allocation{
    70  					Consumer: fmt.Sprintf("fake-consumer/%s/uid", key),
    71  					Requests: &copyReq,
    72  				}
    73  				allocation = append(allocation, &fakeAllocation)
    74  			}
    75  
    76  			cp.TopologyZone[i].Children[j].Allocations = allocation
    77  		}
    78  	}
    79  
    80  	return cp
    81  }
    82  
    83  func (rt *ResourceTopology) DeepCopy() *ResourceTopology {
    84  	out := new(ResourceTopology)
    85  	if rt.TopologyZone != nil {
    86  		out.TopologyZone = make([]*v1alpha1.TopologyZone, len(rt.TopologyZone))
    87  		for i := range rt.TopologyZone {
    88  			if rt.TopologyZone[i] != nil {
    89  				out.TopologyZone[i] = rt.TopologyZone[i].DeepCopy()
    90  			}
    91  		}
    92  	}
    93  	out.TopologyPolicy = rt.TopologyPolicy
    94  	return out
    95  }