From 7f370d651ddef1106ad0d6fa8fd4e1e8a4bf8d44 Mon Sep 17 00:00:00 2001 From: Mengjiao Liu Date: Tue, 21 Mar 2023 16:20:30 +0800 Subject: [PATCH] Migrated `pkg/scheduler/framework/plugins/podtopologyspread` to contextual logging --- .../plugins/podtopologyspread/filtering.go | 10 ++++++---- .../plugins/podtopologyspread/filtering_test.go | 14 +++++++++----- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/pkg/scheduler/framework/plugins/podtopologyspread/filtering.go b/pkg/scheduler/framework/plugins/podtopologyspread/filtering.go index bd0d5a92f30f..437b696b58fd 100644 --- a/pkg/scheduler/framework/plugins/podtopologyspread/filtering.go +++ b/pkg/scheduler/framework/plugins/podtopologyspread/filtering.go @@ -270,11 +270,12 @@ func (pl *PodTopologySpread) calPreFilterState(ctx context.Context, pod *v1.Pod) tpCountsByNode := make([]map[topologyPair]int, len(allNodes)) requiredNodeAffinity := nodeaffinity.GetRequiredNodeAffinity(pod) + logger := klog.FromContext(ctx) processNode := func(i int) { nodeInfo := allNodes[i] node := nodeInfo.Node() if node == nil { - klog.ErrorS(nil, "Node not found") + logger.Error(nil, "Node not found") return } @@ -347,12 +348,13 @@ func (pl *PodTopologySpread) Filter(ctx context.Context, cycleState *framework.C return nil } + logger := klog.FromContext(ctx) podLabelSet := labels.Set(pod.Labels) for _, c := range s.Constraints { tpKey := c.TopologyKey tpVal, ok := node.Labels[c.TopologyKey] if !ok { - klog.V(5).InfoS("Node doesn't have required label", "node", klog.KObj(node), "label", tpKey) + logger.V(5).Info("Node doesn't have required label", "node", klog.KObj(node), "label", tpKey) return framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonNodeLabelNotMatch) } @@ -360,7 +362,7 @@ func (pl *PodTopologySpread) Filter(ctx context.Context, cycleState *framework.C // 'existing matching num' + 'if self-match (1 or 0)' - 'global minimum' <= 'maxSkew' minMatchNum, err := s.minMatchNum(tpKey, c.MinDomains, pl.enableMinDomainsInPodTopologySpread) if err != nil { - klog.ErrorS(err, "Internal error occurred while retrieving value precalculated in PreFilter", "topologyKey", tpKey, "paths", s.TpKeyToCriticalPaths) + logger.Error(err, "Internal error occurred while retrieving value precalculated in PreFilter", "topologyKey", tpKey, "paths", s.TpKeyToCriticalPaths) continue } @@ -376,7 +378,7 @@ func (pl *PodTopologySpread) Filter(ctx context.Context, cycleState *framework.C } skew := matchNum + selfMatchNum - minMatchNum if skew > int(c.MaxSkew) { - klog.V(5).InfoS("Node failed spreadConstraint: matchNum + selfMatchNum - minMatchNum > maxSkew", "node", klog.KObj(node), "topologyKey", tpKey, "matchNum", matchNum, "selfMatchNum", selfMatchNum, "minMatchNum", minMatchNum, "maxSkew", c.MaxSkew) + logger.V(5).Info("Node failed spreadConstraint: matchNum + selfMatchNum - minMatchNum > maxSkew", "node", klog.KObj(node), "topologyKey", tpKey, "matchNum", matchNum, "selfMatchNum", selfMatchNum, "minMatchNum", minMatchNum, "maxSkew", c.MaxSkew) return framework.NewStatus(framework.Unschedulable, ErrReasonConstraintsNotMatch) } } diff --git a/pkg/scheduler/framework/plugins/podtopologyspread/filtering_test.go b/pkg/scheduler/framework/plugins/podtopologyspread/filtering_test.go index d6ea005c919d..c66df78201d6 100644 --- a/pkg/scheduler/framework/plugins/podtopologyspread/filtering_test.go +++ b/pkg/scheduler/framework/plugins/podtopologyspread/filtering_test.go @@ -28,6 +28,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/klog/v2/ktesting" "k8s.io/kubernetes/pkg/scheduler/apis/config" "k8s.io/kubernetes/pkg/scheduler/framework" "k8s.io/kubernetes/pkg/scheduler/framework/plugins/feature" @@ -1456,7 +1457,8 @@ func TestPreFilterState(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) + _, ctx := ktesting.NewTestContext(t) + ctx, cancel := context.WithCancel(ctx) defer cancel() args := &config.PodTopologySpreadArgs{ DefaultConstraints: tt.defaultConstraints, @@ -1967,7 +1969,7 @@ func TestPreFilterStateAddPod(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctx := context.Background() + _, ctx := ktesting.NewTestContext(t) snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes) pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot) p := pl.(*PodTopologySpread) @@ -2283,7 +2285,7 @@ func TestPreFilterStateRemovePod(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctx := context.Background() + _, ctx := ktesting.NewTestContext(t) snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes) pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot) p := pl.(*PodTopologySpread) @@ -2988,13 +2990,14 @@ func TestSingleConstraint(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + _, ctx := ktesting.NewTestContext(t) snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes) pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot) p := pl.(*PodTopologySpread) p.enableMinDomainsInPodTopologySpread = tt.enableMinDomains p.enableNodeInclusionPolicyInPodTopologySpread = tt.enableNodeInclusionPolicy state := framework.NewCycleState() - if _, s := p.PreFilter(context.Background(), state, tt.pod); !s.IsSuccess() { + if _, s := p.PreFilter(ctx, state, tt.pod); !s.IsSuccess() { t.Errorf("preFilter failed with status: %v", s) } @@ -3332,12 +3335,13 @@ func TestMultipleConstraints(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + _, ctx := ktesting.NewTestContext(t) snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes) pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot) p := pl.(*PodTopologySpread) p.enableNodeInclusionPolicyInPodTopologySpread = tt.enableNodeInclusionPolicy state := framework.NewCycleState() - if _, s := p.PreFilter(context.Background(), state, tt.pod); !s.IsSuccess() { + if _, s := p.PreFilter(ctx, state, tt.pod); !s.IsSuccess() { t.Errorf("preFilter failed with status: %v", s) }