-
Notifications
You must be signed in to change notification settings - Fork 980
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
[Experimental] Implemented Hostname AntiAffinity #1221
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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,81 @@ | ||
/* | ||
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 selection | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
"knative.dev/pkg/apis" | ||
) | ||
|
||
func NewAntiAffinity() *AntiAffinity { | ||
return &AntiAffinity{} | ||
} | ||
|
||
var AllowedAntiAffinityKeys = sets.NewString(v1.LabelHostname) | ||
|
||
type AntiAffinity struct{} | ||
|
||
// Validate that the affinity terms are supported | ||
func (a *AntiAffinity) Validate(pod *v1.Pod) (errs *apis.FieldError) { | ||
for i, term := range a.termsFor(pod) { | ||
errs = errs.Also(a.validateTerm(pod, term).ViaIndex(i)) | ||
} | ||
return errs | ||
} | ||
|
||
func (a *AntiAffinity) validateTerm(pod *v1.Pod, term v1.PodAffinityTerm) (errs *apis.FieldError) { | ||
if term.NamespaceSelector != nil { | ||
errs = errs.Also(apis.ErrDisallowedFields("namespaceSelector")) | ||
} | ||
for i, namespace := range term.Namespaces { | ||
if namespace != pod.Namespace { | ||
errs = errs.Also(apis.ErrInvalidArrayValue(fmt.Sprintf("%s, cross namespace affinity is not supported", namespace), "namespaces", i)) | ||
} | ||
} | ||
if !AllowedAntiAffinityKeys.Has(term.TopologyKey) { | ||
errs = errs.Also(apis.ErrInvalidKeyName(fmt.Sprintf("%s not in %v", term.TopologyKey, AllowedAntiAffinityKeys.UnsortedList()), "topologyKey")) | ||
} | ||
return errs | ||
} | ||
|
||
// Transform pod anti affinity into topology rules | ||
func (a *AntiAffinity) Transform(ctx context.Context, pod *v1.Pod) { | ||
for _, term := range a.termsFor(pod) { | ||
pod.Spec.TopologySpreadConstraints = append(pod.Spec.TopologySpreadConstraints, v1.TopologySpreadConstraint{ | ||
MaxSkew: 1, | ||
TopologyKey: term.TopologyKey, | ||
WhenUnsatisfiable: v1.DoNotSchedule, | ||
LabelSelector: term.LabelSelector, | ||
}) | ||
} | ||
} | ||
|
||
func (a *AntiAffinity) termsFor(pod *v1.Pod) []v1.PodAffinityTerm { | ||
if pod.Spec.Affinity == nil { | ||
return nil | ||
} | ||
if pod.Spec.Affinity.PodAntiAffinity == nil { | ||
return nil | ||
} | ||
terms := pod.Spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution | ||
for _, term := range pod.Spec.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution { | ||
terms = append(terms, term.PodAffinityTerm) | ||
} | ||
return terms | ||
} |
This file contains 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
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.
Oh this is interesting. Is this how this works under the hood in K8s as well or a new approach that gets us there quicker in Karpenter?
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.
This is absolutely not how it works under the hood. It's a bit of a hack that takes advantage of simplifying antiaffinity enough until it's translatable to topology.