move nodeaffinity helpers to component-helpers package (only impact scheduler plugins)

This commit is contained in:
Mengxue Zhang
2021-03-09 21:33:02 +00:00
parent eeccc9f9c0
commit fd8128d7d6
5 changed files with 740 additions and 36 deletions

View File

@@ -21,7 +21,6 @@ import (
"fmt"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/component-helpers/scheduling/corev1/nodeaffinity"
"k8s.io/kubernetes/pkg/scheduler/apis/config"
@@ -63,8 +62,7 @@ func (pl *NodeAffinity) Name() string {
}
type preFilterState struct {
requiredNodeSelector labels.Selector
requiredNodeAffinity *nodeaffinity.LazyErrorNodeSelector
requiredNodeSelectorAndAffinity nodeaffinity.RequiredNodeAffinity
}
// Clone just returns the same state because it is not affected by pod additions or deletions.
@@ -74,7 +72,7 @@ func (s *preFilterState) Clone() framework.StateData {
// PreFilter builds and writes cycle state used by Filter.
func (pl *NodeAffinity) PreFilter(ctx context.Context, cycleState *framework.CycleState, pod *v1.Pod) *framework.Status {
state := getPodRequiredNodeSelectorAndAffinity(pod)
state := &preFilterState{requiredNodeSelectorAndAffinity: nodeaffinity.GetRequiredNodeAffinity(pod)}
cycleState.Write(preFilterStateKey, state)
return nil
}
@@ -99,21 +97,15 @@ func (pl *NodeAffinity) Filter(ctx context.Context, state *framework.CycleState,
if err != nil {
// Fallback to calculate requiredNodeSelector and requiredNodeAffinity
// here when PreFilter is disabled.
s = getPodRequiredNodeSelectorAndAffinity(pod)
s = &preFilterState{requiredNodeSelectorAndAffinity: nodeaffinity.GetRequiredNodeAffinity(pod)}
}
if s.requiredNodeSelector != nil {
if !s.requiredNodeSelector.Matches(labels.Set(node.Labels)) {
return framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonPod)
}
}
if s.requiredNodeAffinity != nil {
// Ignore parsing errors for backwards compatibility.
matches, _ := s.requiredNodeAffinity.Match(node)
if !matches {
return framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonPod)
}
// Ignore parsing errors for backwards compatibility.
match, _ := s.requiredNodeSelectorAndAffinity.Match(node)
if !match {
return framework.NewStatus(framework.UnschedulableAndUnresolvable, ErrReasonPod)
}
return nil
}
@@ -248,21 +240,6 @@ func getPreScoreState(cycleState *framework.CycleState) (*preScoreState, error)
return s, nil
}
func getPodRequiredNodeSelectorAndAffinity(pod *v1.Pod) *preFilterState {
var selector labels.Selector
if len(pod.Spec.NodeSelector) > 0 {
selector = labels.SelectorFromSet(pod.Spec.NodeSelector)
}
// Use LazyErrorNodeSelector for backwards compatibility of parsing errors.
var affinity *nodeaffinity.LazyErrorNodeSelector
if pod.Spec.Affinity != nil &&
pod.Spec.Affinity.NodeAffinity != nil &&
pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution != nil {
affinity = nodeaffinity.NewLazyErrorNodeSelector(pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution)
}
return &preFilterState{requiredNodeSelector: selector, requiredNodeAffinity: affinity}
}
func getPreFilterState(cycleState *framework.CycleState) (*preFilterState, error) {
c, err := cycleState.Read(preFilterStateKey)
if err != nil {