Migrated pkg/scheduler/framework/plugins/podtopologyspread to contextual logging

This commit is contained in:
Mengjiao Liu 2023-03-21 16:20:30 +08:00
parent c9ff286668
commit 7f370d651d
2 changed files with 15 additions and 9 deletions

View File

@ -270,11 +270,12 @@ func (pl *PodTopologySpread) calPreFilterState(ctx context.Context, pod *v1.Pod)
tpCountsByNode := make([]map[topologyPair]int, len(allNodes)) tpCountsByNode := make([]map[topologyPair]int, len(allNodes))
requiredNodeAffinity := nodeaffinity.GetRequiredNodeAffinity(pod) requiredNodeAffinity := nodeaffinity.GetRequiredNodeAffinity(pod)
logger := klog.FromContext(ctx)
processNode := func(i int) { processNode := func(i int) {
nodeInfo := allNodes[i] nodeInfo := allNodes[i]
node := nodeInfo.Node() node := nodeInfo.Node()
if node == nil { if node == nil {
klog.ErrorS(nil, "Node not found") logger.Error(nil, "Node not found")
return return
} }
@ -347,12 +348,13 @@ func (pl *PodTopologySpread) Filter(ctx context.Context, cycleState *framework.C
return nil return nil
} }
logger := klog.FromContext(ctx)
podLabelSet := labels.Set(pod.Labels) podLabelSet := labels.Set(pod.Labels)
for _, c := range s.Constraints { for _, c := range s.Constraints {
tpKey := c.TopologyKey tpKey := c.TopologyKey
tpVal, ok := node.Labels[c.TopologyKey] tpVal, ok := node.Labels[c.TopologyKey]
if !ok { 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) 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' // 'existing matching num' + 'if self-match (1 or 0)' - 'global minimum' <= 'maxSkew'
minMatchNum, err := s.minMatchNum(tpKey, c.MinDomains, pl.enableMinDomainsInPodTopologySpread) minMatchNum, err := s.minMatchNum(tpKey, c.MinDomains, pl.enableMinDomainsInPodTopologySpread)
if err != nil { 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 continue
} }
@ -376,7 +378,7 @@ func (pl *PodTopologySpread) Filter(ctx context.Context, cycleState *framework.C
} }
skew := matchNum + selfMatchNum - minMatchNum skew := matchNum + selfMatchNum - minMatchNum
if skew > int(c.MaxSkew) { 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) return framework.NewStatus(framework.Unschedulable, ErrReasonConstraintsNotMatch)
} }
} }

View File

@ -28,6 +28,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog/v2/ktesting"
"k8s.io/kubernetes/pkg/scheduler/apis/config" "k8s.io/kubernetes/pkg/scheduler/apis/config"
"k8s.io/kubernetes/pkg/scheduler/framework" "k8s.io/kubernetes/pkg/scheduler/framework"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/feature" "k8s.io/kubernetes/pkg/scheduler/framework/plugins/feature"
@ -1456,7 +1457,8 @@ func TestPreFilterState(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { 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() defer cancel()
args := &config.PodTopologySpreadArgs{ args := &config.PodTopologySpreadArgs{
DefaultConstraints: tt.defaultConstraints, DefaultConstraints: tt.defaultConstraints,
@ -1967,7 +1969,7 @@ func TestPreFilterStateAddPod(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ctx := context.Background() _, ctx := ktesting.NewTestContext(t)
snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes) snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes)
pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot) pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot)
p := pl.(*PodTopologySpread) p := pl.(*PodTopologySpread)
@ -2283,7 +2285,7 @@ func TestPreFilterStateRemovePod(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ctx := context.Background() _, ctx := ktesting.NewTestContext(t)
snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes) snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes)
pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot) pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot)
p := pl.(*PodTopologySpread) p := pl.(*PodTopologySpread)
@ -2988,13 +2990,14 @@ func TestSingleConstraint(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
_, ctx := ktesting.NewTestContext(t)
snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes) snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes)
pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot) pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot)
p := pl.(*PodTopologySpread) p := pl.(*PodTopologySpread)
p.enableMinDomainsInPodTopologySpread = tt.enableMinDomains p.enableMinDomainsInPodTopologySpread = tt.enableMinDomains
p.enableNodeInclusionPolicyInPodTopologySpread = tt.enableNodeInclusionPolicy p.enableNodeInclusionPolicyInPodTopologySpread = tt.enableNodeInclusionPolicy
state := framework.NewCycleState() 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) t.Errorf("preFilter failed with status: %v", s)
} }
@ -3332,12 +3335,13 @@ func TestMultipleConstraints(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
_, ctx := ktesting.NewTestContext(t)
snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes) snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes)
pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot) pl := plugintesting.SetupPlugin(t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, snapshot)
p := pl.(*PodTopologySpread) p := pl.(*PodTopologySpread)
p.enableNodeInclusionPolicyInPodTopologySpread = tt.enableNodeInclusionPolicy p.enableNodeInclusionPolicyInPodTopologySpread = tt.enableNodeInclusionPolicy
state := framework.NewCycleState() 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) t.Errorf("preFilter failed with status: %v", s)
} }