-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Implement fast predicate index for cluster-autoscaler simulator #9461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
x13n
wants to merge
1
commit into
kubernetes:master
Choose a base branch
from
x13n:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
cluster-autoscaler/simulator/clustersnapshot/predicate/fast_predicates.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| /* | ||
| Copyright 2024 The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package predicate | ||
|
|
||
| import ( | ||
| apiv1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/labels" | ||
| "k8s.io/autoscaler/cluster-autoscaler/simulator/clustersnapshot" | ||
| "k8s.io/autoscaler/cluster-autoscaler/simulator/framework" | ||
| "k8s.io/klog/v2" | ||
| "k8s.io/kubernetes/pkg/scheduler/framework/plugins/interpodaffinity" | ||
| "k8s.io/kubernetes/pkg/scheduler/framework/plugins/podtopologyspread" | ||
| ) | ||
|
|
||
| type FastPredicateState struct { | ||
| podAffinitySelectors []labels.Selector | ||
| podAntiAffinitySelectors []labels.Selector | ||
| topologySpreadSelectors []labels.Selector | ||
| } | ||
|
|
||
| func (p *SchedulerPluginRunner) computeFastPredicateState(pod *apiv1.Pod) (*FastPredicateState, error) { | ||
| state := &FastPredicateState{} | ||
|
|
||
| affinity := pod.Spec.Affinity | ||
| if affinity != nil { | ||
| if affinity.PodAffinity != nil { | ||
| for _, term := range affinity.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution { | ||
| sel, err := metav1.LabelSelectorAsSelector(term.LabelSelector) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| state.podAffinitySelectors = append(state.podAffinitySelectors, sel) | ||
| } | ||
| } | ||
| if affinity.PodAntiAffinity != nil { | ||
| for _, term := range affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution { | ||
| sel, err := metav1.LabelSelectorAsSelector(term.LabelSelector) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| state.podAntiAffinitySelectors = append(state.podAntiAffinitySelectors, sel) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for _, constraint := range pod.Spec.TopologySpreadConstraints { | ||
| if constraint.WhenUnsatisfiable == apiv1.DoNotSchedule { | ||
| sel, err := metav1.LabelSelectorAsSelector(constraint.LabelSelector) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| state.topologySpreadSelectors = append(state.topologySpreadSelectors, sel) | ||
| } | ||
| } | ||
|
|
||
| return state, nil | ||
| } | ||
|
|
||
| func (p *SchedulerPluginRunner) fastCheckPredicates(pod *apiv1.Pod, nodeInfo *framework.NodeInfo, state *FastPredicateState) clustersnapshot.SchedulingError { | ||
| lister := p.snapshot.FastPredicateLister() | ||
| if lister == nil { | ||
| klog.V(4).Infof("FastPredicateLister is nil, skipping fast predicates") | ||
| return nil | ||
| } | ||
|
|
||
| if err := p.fastCheckPodAffinity(pod, nodeInfo, lister, state); err != nil { | ||
| klog.V(4).Infof("fastCheckPodAffinity failed: %v", err) | ||
| return err | ||
| } | ||
|
|
||
| if err := p.fastCheckPodTopologySpread(pod, nodeInfo, lister, state); err != nil { | ||
| klog.V(4).Infof("fastCheckPodTopologySpread failed: %v", err) | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (p *SchedulerPluginRunner) fastCheckPodAffinity(pod *apiv1.Pod, nodeInfo *framework.NodeInfo, lister clustersnapshot.FastPredicateLister, state *FastPredicateState) clustersnapshot.SchedulingError { | ||
| affinity := pod.Spec.Affinity | ||
| if affinity == nil || (affinity.PodAffinity == nil && affinity.PodAntiAffinity == nil) { | ||
| return nil | ||
| } | ||
|
|
||
| node := nodeInfo.Node() | ||
|
|
||
| if affinity.PodAffinity != nil { | ||
| for i, term := range affinity.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution { | ||
| topoValue, ok := node.Labels[term.TopologyKey] | ||
| if !ok { | ||
| continue | ||
| } | ||
| var selector labels.Selector | ||
| var err error | ||
| if state != nil && i < len(state.podAffinitySelectors) { | ||
| selector = state.podAffinitySelectors[i] | ||
| } else { | ||
| selector, err = metav1.LabelSelectorAsSelector(term.LabelSelector) | ||
| if err != nil { | ||
| return clustersnapshot.NewSchedulingInternalError(pod, err.Error()) | ||
| } | ||
| } | ||
| count := lister.PodAffinityCount(term.TopologyKey, topoValue, selector) | ||
| if count == 0 { | ||
| return clustersnapshot.NewFailingPredicateError(pod, "InterPodAffinity", []string{interpodaffinity.ErrReasonAffinityRulesNotMatch}, "", "") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if affinity.PodAntiAffinity != nil { | ||
| for i, term := range affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution { | ||
| topoValue, ok := node.Labels[term.TopologyKey] | ||
| if !ok { | ||
| continue | ||
| } | ||
| var selector labels.Selector | ||
| var err error | ||
| if state != nil && i < len(state.podAntiAffinitySelectors) { | ||
| selector = state.podAntiAffinitySelectors[i] | ||
| } else { | ||
| selector, err = metav1.LabelSelectorAsSelector(term.LabelSelector) | ||
| if err != nil { | ||
| return clustersnapshot.NewSchedulingInternalError(pod, err.Error()) | ||
| } | ||
| } | ||
| count := lister.PodAffinityCount(term.TopologyKey, topoValue, selector) | ||
| if count > 0 { | ||
| return clustersnapshot.NewFailingPredicateError(pod, "InterPodAffinity", []string{interpodaffinity.ErrReasonAntiAffinityRulesNotMatch}, "", "") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (p *SchedulerPluginRunner) fastCheckPodTopologySpread(pod *apiv1.Pod, nodeInfo *framework.NodeInfo, lister clustersnapshot.FastPredicateLister, state *FastPredicateState) clustersnapshot.SchedulingError { | ||
| if len(pod.Spec.TopologySpreadConstraints) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| node := nodeInfo.Node() | ||
|
|
||
| spreadIdx := 0 | ||
| for _, constraint := range pod.Spec.TopologySpreadConstraints { | ||
| if constraint.WhenUnsatisfiable != apiv1.DoNotSchedule { | ||
| continue | ||
| } | ||
|
|
||
| topoValue, ok := node.Labels[constraint.TopologyKey] | ||
| if !ok { | ||
| // If node doesn't have the topology key, it's usually skipped or fails depending on configuration. | ||
| spreadIdx++ | ||
| continue | ||
| } | ||
|
|
||
| var selector labels.Selector | ||
| var err error | ||
| if state != nil && spreadIdx < len(state.topologySpreadSelectors) { | ||
| selector = state.topologySpreadSelectors[spreadIdx] | ||
| } else { | ||
| selector, err = metav1.LabelSelectorAsSelector(constraint.LabelSelector) | ||
| if err != nil { | ||
| return clustersnapshot.NewSchedulingInternalError(pod, err.Error()) | ||
| } | ||
| } | ||
| spreadIdx++ | ||
|
|
||
| count := lister.PodAffinityCount(constraint.TopologyKey, topoValue, selector) | ||
|
|
||
| // To implement maxSkew, we need minCount across all domains. | ||
| minCount := p.getMinTopologyCount(constraint.TopologyKey, selector, lister) | ||
|
|
||
| if int32(count+1-minCount) > constraint.MaxSkew { | ||
| return clustersnapshot.NewFailingPredicateError(pod, "PodTopologySpread", []string{podtopologyspread.ErrReasonConstraintsNotMatch}, "", "") | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (p *SchedulerPluginRunner) getMinTopologyCount(topoKey string, selector labels.Selector, lister clustersnapshot.FastPredicateLister) int { | ||
| domains := lister.TopologyDomains(topoKey) | ||
| if len(domains) == 0 { | ||
| return 0 | ||
| } | ||
|
|
||
| minCount := -1 | ||
| for _, domain := range domains { | ||
| count := lister.PodAffinityCount(topoKey, domain, selector) | ||
| if minCount == -1 || count < minCount { | ||
| minCount = count | ||
| } | ||
| if minCount == 0 { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if minCount == -1 { | ||
| return 0 | ||
| } | ||
| return minCount | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At which point do we check if existing pods have anti-affinity against incoming pod? Incoming pod may have no AA in the spec, but we still need to check if it violates constraints of the existing pods.