From 54e6f609ceb7313853963f261d931fb490ed6854 Mon Sep 17 00:00:00 2001 From: Mengjiao Liu Date: Mon, 20 Mar 2023 17:54:40 +0800 Subject: [PATCH] Migrated `pkg/scheduler/framework/plugins/noderesources` to contextual logging --- .../plugins/noderesources/balanced_allocation.go | 2 +- .../framework/plugins/noderesources/fit.go | 3 +-- .../plugins/noderesources/resource_allocation.go | 14 +++++++++----- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pkg/scheduler/framework/plugins/noderesources/balanced_allocation.go b/pkg/scheduler/framework/plugins/noderesources/balanced_allocation.go index ef6b8723b65..f375be2d42f 100644 --- a/pkg/scheduler/framework/plugins/noderesources/balanced_allocation.go +++ b/pkg/scheduler/framework/plugins/noderesources/balanced_allocation.go @@ -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. diff --git a/pkg/scheduler/framework/plugins/noderesources/fit.go b/pkg/scheduler/framework/plugins/noderesources/fit.go index 81edad6e277..b81c4c411f4 100644 --- a/pkg/scheduler/framework/plugins/noderesources/fit.go +++ b/pkg/scheduler/framework/plugins/noderesources/fit.go @@ -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" @@ -381,5 +380,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) } diff --git a/pkg/scheduler/framework/plugins/noderesources/resource_allocation.go b/pkg/scheduler/framework/plugins/noderesources/resource_allocation.go index 68e4433f918..535aacf9ecc 100644 --- a/pkg/scheduler/framework/plugins/noderesources/resource_allocation.go +++ b/pkg/scheduler/framework/plugins/noderesources/resource_allocation.go @@ -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" @@ -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") @@ -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 @@ -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, ) @@ -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 @@ -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 }