Skip to content

Commit

Permalink
Merge pull request #116748 from mengjiao-liu/contextual-logging-sched…
Browse files Browse the repository at this point in the history
…uler-plugin-noderesource

Migrated `pkg/scheduler/framework/plugins/noderesources` to contextual logging
  • Loading branch information
k8s-ci-robot committed Apr 27, 2023
2 parents 3a15029 + 54e6f60 commit a38efac
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Expand Up @@ -105,7 +105,7 @@ func (ba *BalancedAllocation) Score(ctx context.Context, state *framework.CycleS
// Detail: score = (1 - std) * MaxNodeScore, where std is calculated by the root square of Σ((fraction(i)-mean)^2)/len(resources)
// The algorithm is partly inspired by:
// "Wei Huang et al. An Energy Efficient Virtual Machine Placement Algorithm with Balanced Resource Utilization"
return ba.score(pod, nodeInfo, s.podRequests)
return ba.score(ctx, pod, nodeInfo, s.podRequests)
}

// ScoreExtensions of the Score plugin.
Expand Down
3 changes: 1 addition & 2 deletions pkg/scheduler/framework/plugins/noderesources/fit.go
Expand Up @@ -24,7 +24,6 @@ import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/sets"

"k8s.io/kubernetes/pkg/api/v1/resource"
v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
"k8s.io/kubernetes/pkg/scheduler/apis/config"
Expand Down Expand Up @@ -382,5 +381,5 @@ func (f *Fit) Score(ctx context.Context, state *framework.CycleState, pod *v1.Po
}
}

return f.score(pod, nodeInfo, s.podRequests)
return f.score(ctx, pod, nodeInfo, s.podRequests)
}
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package noderesources

import (
"context"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
utilfeature "k8s.io/apiserver/pkg/util/feature"
Expand Down Expand Up @@ -44,9 +46,11 @@ type resourceAllocationScorer struct {

// score will use `scorer` function to calculate the score.
func (r *resourceAllocationScorer) score(
ctx context.Context,
pod *v1.Pod,
nodeInfo *framework.NodeInfo,
podRequests []int64) (int64, *framework.Status) {
logger := klog.FromContext(ctx)
node := nodeInfo.Node()
if node == nil {
return 0, framework.NewStatus(framework.Error, "node not found")
Expand All @@ -59,7 +63,7 @@ func (r *resourceAllocationScorer) score(
requested := make([]int64, len(r.resources))
allocatable := make([]int64, len(r.resources))
for i := range r.resources {
alloc, req := r.calculateResourceAllocatableRequest(nodeInfo, v1.ResourceName(r.resources[i].Name), podRequests[i])
alloc, req := r.calculateResourceAllocatableRequest(logger, nodeInfo, v1.ResourceName(r.resources[i].Name), podRequests[i])
// Only fill the extended resource entry when it's non-zero.
if alloc == 0 {
continue
Expand All @@ -70,8 +74,8 @@ func (r *resourceAllocationScorer) score(

score := r.scorer(requested, allocatable)

if klogV := klog.V(10); klogV.Enabled() { // Serializing these maps is costly.
klogV.InfoS("Listing internal info for allocatable resources, requested resources and score", "pod",
if loggerV := logger.V(10); loggerV.Enabled() { // Serializing these maps is costly.
loggerV.Info("Listed internal info for allocatable resources, requested resources and score", "pod",
klog.KObj(pod), "node", klog.KObj(node), "resourceAllocationScorer", r.Name,
"allocatableResource", allocatable, "requestedResource", requested, "resourceScore", score,
)
Expand All @@ -84,7 +88,7 @@ func (r *resourceAllocationScorer) score(
// - 1st param: quantity of allocatable resource on the node.
// - 2nd param: aggregated quantity of requested resource on the node.
// Note: if it's an extended resource, and the pod doesn't request it, (0, 0) is returned.
func (r *resourceAllocationScorer) calculateResourceAllocatableRequest(nodeInfo *framework.NodeInfo, resource v1.ResourceName, podRequest int64) (int64, int64) {
func (r *resourceAllocationScorer) calculateResourceAllocatableRequest(logger klog.Logger, nodeInfo *framework.NodeInfo, resource v1.ResourceName, podRequest int64) (int64, int64) {
requested := nodeInfo.NonZeroRequested
if r.useRequested {
requested = nodeInfo.Requested
Expand All @@ -107,7 +111,7 @@ func (r *resourceAllocationScorer) calculateResourceAllocatableRequest(nodeInfo
return nodeInfo.Allocatable.ScalarResources[resource], (nodeInfo.Requested.ScalarResources[resource] + podRequest)
}
}
klog.V(10).InfoS("Requested resource is omitted for node score calculation", "resourceName", resource)
logger.V(10).Info("Requested resource is omitted for node score calculation", "resourceName", resource)
return 0, 0
}

Expand Down

0 comments on commit a38efac

Please sign in to comment.