Skip to content

Commit

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

Migrated `pkg/scheduler/framework/plugins/podtopologyspread` to contextual logging
  • Loading branch information
k8s-ci-robot committed Apr 27, 2023
2 parents a38efac + 7f370d6 commit b44482a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
10 changes: 6 additions & 4 deletions pkg/scheduler/framework/plugins/podtopologyspread/filtering.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -347,20 +348,21 @@ 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)
}

// judging criteria:
// '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
}

Expand All @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit b44482a

Please sign in to comment.