@@ -28,7 +28,6 @@ import (
2828 "github.com/karmada-io/karmada/pkg/estimator"
2929 "github.com/karmada-io/karmada/pkg/estimator/pb"
3030 "github.com/karmada-io/karmada/pkg/estimator/server/framework"
31- nodeutil "github.com/karmada-io/karmada/pkg/estimator/server/nodes"
3231 "github.com/karmada-io/karmada/pkg/util"
3332 schedcache "github.com/karmada-io/karmada/pkg/util/lifted/scheduler/cache"
3433 schedulerframework "github.com/karmada-io/karmada/pkg/util/lifted/scheduler/framework"
@@ -70,27 +69,20 @@ func (pl *nodeResourceEstimator) Name() string {
7069// Estimate the replica allowed by the node resources for a given pb.ReplicaRequirements.
7170func (pl * nodeResourceEstimator ) Estimate (ctx context.Context , snapshot * schedcache.Snapshot , requirements * pb.ReplicaRequirements ) (int32 , * framework.Result ) {
7271 if ! pl .enabled {
73- klog .V (5 ).Info ("Estimator Plugin" , "name" , Name , "enabled" , pl .enabled )
74- return noNodeConstraint , framework .NewResult (framework .Noopperation , fmt .Sprintf ("%s is disabled" , pl .Name ()))
72+ return pl .disabledResult ()
7573 }
7674
7775 allNodes , err := snapshot .NodeInfos ().List ()
7876 if err != nil {
7977 return 0 , framework .AsResult (err )
8078 }
81- var (
82- affinity = nodeutil .GetRequiredNodeAffinity (* requirements )
83- tolerations []corev1.Toleration
84- )
8579
86- if requirements .NodeClaim != nil {
87- tolerations = requirements .NodeClaim .Tolerations
88- }
80+ affinity , tolerations := estimator .GetAffinityAndTolerations (requirements .NodeClaim )
8981
9082 var res int32
9183 processNode := func (i int ) {
92- node := allNodes [i ]
93- if ! nodeutil . IsNodeAffinityMatched (node . Node () , affinity ) || ! nodeutil . IsTolerationMatched ( node . Node () , tolerations ) {
84+ node := allNodes [i ]. Clone ()
85+ if ! estimator . MatchNode (node , affinity , tolerations ) {
9486 return
9587 }
9688 maxReplica := pl .nodeMaxAvailableReplica (node , requirements .ResourceRequest )
@@ -102,21 +94,28 @@ func (pl *nodeResourceEstimator) Estimate(ctx context.Context, snapshot *schedca
10294}
10395
10496func (pl * nodeResourceEstimator ) nodeMaxAvailableReplica (node * schedulerframework.NodeInfo , rl corev1.ResourceList ) int32 {
105- rest := node .Allocatable .Clone ().SubResource (node .Requested )
97+ rest := getNodeAvailableResource (node )
98+ return int32 (rest .MaxDivided (rl )) // #nosec G115: integer overflow conversion int64 -> int32
99+ }
100+
101+ // getNodeAvailableResource calculates a node's available resources after deducting requested resources
102+ // and adjusting the pod-count capacity, which isn't included in node.Requested.
103+ func getNodeAvailableResource (node * schedulerframework.NodeInfo ) * util.Resource {
104+ rest := node .Allocatable
105+ rest = rest .SubResource (node .Requested )
106106 // The number of pods in a node is a kind of resource in node allocatable resources.
107107 // However, total requested resources of all pods on this node, i.e. `node.Requested`,
108108 // do not contain pod resources. So after subtraction, we should cope with allowed pod
109109 // number manually which is the upper bound of this node available replicas.
110110 rest .AllowedPodNumber = util .MaxInt64 (rest .AllowedPodNumber - int64 (len (node .Pods )), 0 )
111- return int32 ( rest . MaxDivided ( rl )) // #nosec G115: integer overflow conversion int64 -> int32
111+ return rest
112112}
113113
114114// EstimateComponents estimates the maximum number of complete component sets that can be scheduled.
115115// It returns the number of sets that can fit on the available node resources.
116116func (pl * nodeResourceEstimator ) EstimateComponents (_ context.Context , snapshot * schedcache.Snapshot , components []pb.Component , _ string ) (int32 , * framework.Result ) {
117117 if ! pl .enabled {
118- klog .V (5 ).Info ("Estimator Plugin" , "name" , Name , "enabled" , pl .enabled )
119- return noNodeConstraint , framework .NewResult (framework .Noopperation , fmt .Sprintf ("%s is disabled" , pl .Name ()))
118+ return pl .disabledResult ()
120119 }
121120
122121 if len (components ) == 0 {
@@ -136,6 +135,11 @@ func (pl *nodeResourceEstimator) EstimateComponents(_ context.Context, snapshot
136135 return sets , framework .NewResult (framework .Success )
137136}
138137
138+ func (pl * nodeResourceEstimator ) disabledResult () (int32 , * framework.Result ) {
139+ klog .V (5 ).Info ("Estimator Plugin" , "name" , Name , "enabled" , pl .enabled )
140+ return noNodeConstraint , framework .NewResult (framework .Noopperation , fmt .Sprintf ("%s is disabled" , pl .Name ()))
141+ }
142+
139143// getNodesAvailableResources retrieves and prepares the list of node information from the snapshot.
140144// It clones each node's info and adjusts the allocatable resources by subtracting the requested resources.
141145// So that the returned node infos reflect the actual available resources for scheduling.
@@ -148,8 +152,7 @@ func getNodesAvailableResources(snapshot *schedcache.Snapshot) ([]*schedulerfram
148152 rest := make ([]* schedulerframework.NodeInfo , 0 , len (allNodes ))
149153 for _ , node := range allNodes {
150154 n := node .Clone ()
151- n .Allocatable .SubResource (n .Requested )
152- n .Allocatable .AllowedPodNumber = util .MaxInt64 (n .Allocatable .AllowedPodNumber - int64 (len (node .Pods )), 0 )
155+ n .Allocatable = getNodeAvailableResource (n )
153156 rest = append (rest , n )
154157 }
155158
0 commit comments