|
| 1 | +package noderesourcesfitplus |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + v1 "k8s.io/api/core/v1" |
| 8 | + "k8s.io/apimachinery/pkg/runtime" |
| 9 | + "k8s.io/kubernetes/pkg/api/v1/resource" |
| 10 | + k8sConfig "k8s.io/kubernetes/pkg/scheduler/apis/config" |
| 11 | + "k8s.io/kubernetes/pkg/scheduler/framework" |
| 12 | + plfeature "k8s.io/kubernetes/pkg/scheduler/framework/plugins/feature" |
| 13 | + "k8s.io/kubernetes/pkg/scheduler/framework/plugins/noderesources" |
| 14 | + "sigs.k8s.io/scheduler-plugins/apis/config" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + // Name is plugin name |
| 19 | + Name = "NodeResourcesFitPlus" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + _ framework.ScorePlugin = &Plugin{} |
| 24 | +) |
| 25 | + |
| 26 | +type Plugin struct { |
| 27 | + handle framework.Handle |
| 28 | + args *config.NodeResourcesFitPlusArgs |
| 29 | +} |
| 30 | + |
| 31 | +func New(_ context.Context, args runtime.Object, handle framework.Handle) (framework.Plugin, error) { |
| 32 | + |
| 33 | + nodeResourcesFitPlusArgs, ok := args.(*config.NodeResourcesFitPlusArgs) |
| 34 | + |
| 35 | + if !ok { |
| 36 | + return nil, fmt.Errorf("want args to be of type NodeResourcesFitPlusArgs, got %T", args) |
| 37 | + } |
| 38 | + |
| 39 | + return &Plugin{ |
| 40 | + handle: handle, |
| 41 | + args: nodeResourcesFitPlusArgs, |
| 42 | + }, nil |
| 43 | +} |
| 44 | + |
| 45 | +func (s *Plugin) Name() string { |
| 46 | + return Name |
| 47 | +} |
| 48 | + |
| 49 | +func (s *Plugin) Score(ctx context.Context, state *framework.CycleState, p *v1.Pod, nodeName string) (int64, *framework.Status) { |
| 50 | + nodeInfo, err := s.handle.SnapshotSharedLister().NodeInfos().Get(nodeName) |
| 51 | + if err != nil { |
| 52 | + return 0, framework.NewStatus(framework.Error, fmt.Sprintf("getting node %q from Snapshot: %v", nodeName, err)) |
| 53 | + } |
| 54 | + |
| 55 | + var nodeScore int64 |
| 56 | + var weightSum int64 |
| 57 | + |
| 58 | + podRequest, _ := fitsRequest(computePodResourceRequest(p).Resource, nodeInfo) |
| 59 | + |
| 60 | + for _, requestSourceName := range podRequest { |
| 61 | + v, ok := s.args.Resources[requestSourceName] |
| 62 | + if !ok { |
| 63 | + continue |
| 64 | + } |
| 65 | + fit, err := noderesources.NewFit(ctx, |
| 66 | + &k8sConfig.NodeResourcesFitArgs{ |
| 67 | + ScoringStrategy: &k8sConfig.ScoringStrategy{ |
| 68 | + Type: v.Type, // MostAllocated or LeastAllocated |
| 69 | + Resources: []k8sConfig.ResourceSpec{ |
| 70 | + {Name: string(requestSourceName), Weight: 1}, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, s.handle, plfeature.Features{}) |
| 74 | + |
| 75 | + if err != nil { |
| 76 | + return 0, framework.NewStatus(framework.Error, err.Error()) |
| 77 | + } |
| 78 | + resourceScore, status := fit.(framework.ScorePlugin).Score(ctx, state, p, nodeName) |
| 79 | + if !status.IsSuccess() { |
| 80 | + return 0, framework.NewStatus(framework.Error, err.Error()) |
| 81 | + } |
| 82 | + |
| 83 | + nodeScore += resourceScore * v.Weight |
| 84 | + weightSum += v.Weight |
| 85 | + } |
| 86 | + |
| 87 | + if weightSum == 0 { |
| 88 | + return framework.MaxNodeScore, framework.NewStatus(framework.Success, "") |
| 89 | + } |
| 90 | + scores := nodeScore / weightSum |
| 91 | + |
| 92 | + return scores, nil |
| 93 | +} |
| 94 | + |
| 95 | +func (p *Plugin) ScoreExtensions() framework.ScoreExtensions { |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +type preFilterState struct { |
| 100 | + framework.Resource |
| 101 | +} |
| 102 | + |
| 103 | +func computePodResourceRequest(pod *v1.Pod) *preFilterState { |
| 104 | + // pod hasn't scheduled yet so we don't need to worry about InPlacePodVerticalScalingEnabled |
| 105 | + reqs := resource.PodRequests(pod, resource.PodResourcesOptions{}) |
| 106 | + result := &preFilterState{} |
| 107 | + result.SetMaxResource(reqs) |
| 108 | + return result |
| 109 | +} |
| 110 | + |
| 111 | +func fitsRequest(podRequest framework.Resource, nodeInfo *framework.NodeInfo) ([]v1.ResourceName, []v1.ResourceName) { |
| 112 | + var podRequestResource []v1.ResourceName |
| 113 | + var nodeRequestResource []v1.ResourceName |
| 114 | + |
| 115 | + if podRequest.MilliCPU > 0 { |
| 116 | + podRequestResource = append(podRequestResource, v1.ResourceCPU) |
| 117 | + } |
| 118 | + |
| 119 | + if nodeInfo.Allocatable.MilliCPU > 0 { |
| 120 | + nodeRequestResource = append(nodeRequestResource, v1.ResourceCPU) |
| 121 | + } |
| 122 | + |
| 123 | + if podRequest.Memory > 0 { |
| 124 | + podRequestResource = append(podRequestResource, v1.ResourceMemory) |
| 125 | + } |
| 126 | + |
| 127 | + if nodeInfo.Allocatable.Memory > 0 { |
| 128 | + nodeRequestResource = append(nodeRequestResource, v1.ResourceMemory) |
| 129 | + } |
| 130 | + |
| 131 | + if podRequest.EphemeralStorage > 0 { |
| 132 | + podRequestResource = append(podRequestResource, v1.ResourceEphemeralStorage) |
| 133 | + } |
| 134 | + |
| 135 | + if nodeInfo.Allocatable.EphemeralStorage > 0 { |
| 136 | + nodeRequestResource = append(nodeRequestResource, v1.ResourceEphemeralStorage) |
| 137 | + } |
| 138 | + |
| 139 | + for rName, rQuant := range podRequest.ScalarResources { |
| 140 | + if rQuant > 0 { |
| 141 | + podRequestResource = append(podRequestResource, rName) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + for rName, rQuant := range nodeInfo.Allocatable.ScalarResources { |
| 146 | + if rQuant > 0 { |
| 147 | + nodeRequestResource = append(nodeRequestResource, rName) |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return podRequestResource, nodeRequestResource |
| 152 | +} |
0 commit comments