Skip to content
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

deeper podAffinity and anti-affinity checks #1442

Merged
merged 2 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pkg/controllers/selection/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,22 @@ func validateTopology(pod *v1.Pod) (errs error) {
return errs
}

func validateAffinity(pod *v1.Pod) (errs error) {
if pod.Spec.Affinity == nil {
func validateAffinity(p *v1.Pod) (errs error) {
if p.Spec.Affinity == nil {
return nil
}
if pod.Spec.Affinity.PodAffinity != nil {
if pod.HasPodAffinity(p) {
bwagner5 marked this conversation as resolved.
Show resolved Hide resolved
errs = multierr.Append(errs, fmt.Errorf("pod affinity is not supported"))
}
if pod.Spec.Affinity.PodAntiAffinity != nil {
if pod.HasPodAntiAffinity(p) {
errs = multierr.Append(errs, fmt.Errorf("pod anti-affinity is not supported"))
}
if pod.Spec.Affinity.NodeAffinity != nil {
for _, term := range pod.Spec.Affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution {
if p.Spec.Affinity.NodeAffinity != nil {
for _, term := range p.Spec.Affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution {
errs = multierr.Append(errs, validateNodeSelectorTerm(term.Preference))
}
if pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution != nil {
for _, term := range pod.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms {
if p.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution != nil {
for _, term := range p.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution.NodeSelectorTerms {
errs = multierr.Append(errs, validateNodeSelectorTerm(term))
}
}
Expand Down
39 changes: 39 additions & 0 deletions pkg/controllers/selection/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,42 @@ var _ = Describe("Multiple Provisioners", func() {
Expect(node.Labels[v1alpha5.ProvisionerNameLabelKey]).To(Equal(provisioner2.Name))
})
})

var _ = Describe("Pod Affinity and AntiAffinity", func() {
It("should not schedule a pod with pod affinity", func() {
ExpectCreated(ctx, env.Client)
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, provisioner, test.UnschedulablePod(test.PodOptions{
PodRequirements: []v1.PodAffinityTerm{{TopologyKey: "foo"}},
}))[0]
ExpectNotScheduled(ctx, env.Client, pod)
})
It("should not schedule a pod with pod anti-affinity", func() {
ExpectCreated(ctx, env.Client)
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, provisioner, test.UnschedulablePod(test.PodOptions{
PodAntiRequirements: []v1.PodAffinityTerm{{TopologyKey: "foo"}},
}))[0]
ExpectNotScheduled(ctx, env.Client, pod)
})
It("should not schedule a pod with pod affinity preference", func() {
ExpectCreated(ctx, env.Client)
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, provisioner, test.UnschedulablePod(test.PodOptions{
PodPreferences: []v1.WeightedPodAffinityTerm{{Weight: 1, PodAffinityTerm: v1.PodAffinityTerm{TopologyKey: "foo"}}},
}))[0]
ExpectNotScheduled(ctx, env.Client, pod)
})
It("should not schedule a pod with pod anti-affinity preference", func() {
ExpectCreated(ctx, env.Client)
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, provisioner, test.UnschedulablePod(test.PodOptions{
PodAntiPreferences: []v1.WeightedPodAffinityTerm{{Weight: 1, PodAffinityTerm: v1.PodAffinityTerm{TopologyKey: "foo"}}},
}))[0]
ExpectNotScheduled(ctx, env.Client, pod)
})
It("should schedule a pod with empty pod affinity and anti-affinity", func() {
ExpectCreated(ctx, env.Client)
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, provisioner, test.UnschedulablePod(test.PodOptions{
PodRequirements: []v1.PodAffinityTerm{},
PodAntiRequirements: []v1.PodAffinityTerm{},
}))[0]
ExpectScheduled(ctx, env.Client, pod)
})
})
69 changes: 61 additions & 8 deletions pkg/test/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type PodOptions struct {
NodeSelector map[string]string
NodeRequirements []v1.NodeSelectorRequirement
NodePreferences []v1.NodeSelectorRequirement
PodRequirements []v1.PodAffinityTerm
PodPreferences []v1.WeightedPodAffinityTerm
PodAntiRequirements []v1.PodAffinityTerm
PodAntiPreferences []v1.WeightedPodAffinityTerm
TopologySpreadConstraints []v1.TopologySpreadConstraint
Tolerations []v1.Toleration
PersistentVolumeClaims []string
Expand Down Expand Up @@ -73,7 +77,7 @@ func Pod(overrides ...PodOptions) *v1.Pod {
ObjectMeta: ObjectMeta(options.ObjectMeta),
Spec: v1.PodSpec{
NodeSelector: options.NodeSelector,
Affinity: buildAffinity(options.NodeRequirements, options.NodePreferences),
Affinity: buildAffinity(options),
TopologySpreadConstraints: options.TopologySpreadConstraints,
Tolerations: options.Tolerations,
Containers: []v1.Container{{
Expand Down Expand Up @@ -138,22 +142,71 @@ func PodDisruptionBudget(overrides ...PDBOptions) *v1beta1.PodDisruptionBudget {
}
}

func buildAffinity(nodeRequirements []v1.NodeSelectorRequirement, nodePreferences []v1.NodeSelectorRequirement) *v1.Affinity {
var affinity *v1.Affinity
func buildAffinity(options PodOptions) *v1.Affinity {
affinity := &v1.Affinity{}
if nodeAffinity := buildNodeAffinity(options.NodeRequirements, options.NodePreferences); nodeAffinity != nil {
affinity.NodeAffinity = nodeAffinity
}
if podAffinity := buildPodAffinity(options.PodRequirements, options.PodPreferences); podAffinity != nil {
affinity.PodAffinity = podAffinity
}
if podAntiAffinity := buildPodAntiAffinity(options.PodAntiRequirements, options.PodAntiPreferences); podAntiAffinity != nil {
affinity.PodAntiAffinity = podAntiAffinity
}
if affinity.NodeAffinity == nil && affinity.PodAffinity == nil && affinity.PodAntiAffinity == nil {
return nil
}
return affinity
}

func buildPodAffinity(podRequirements []v1.PodAffinityTerm, podPreferences []v1.WeightedPodAffinityTerm) *v1.PodAffinity {
var podAffinity *v1.PodAffinity
if podRequirements == nil && podPreferences == nil {
return podAffinity
}
podAffinity = &v1.PodAffinity{}

if podRequirements != nil {
podAffinity.RequiredDuringSchedulingIgnoredDuringExecution = podRequirements
}
if podPreferences != nil {
podAffinity.PreferredDuringSchedulingIgnoredDuringExecution = podPreferences
}
return podAffinity
}

func buildPodAntiAffinity(podAntiRequirements []v1.PodAffinityTerm, podAntiPreferences []v1.WeightedPodAffinityTerm) *v1.PodAntiAffinity {
var podAntiAffinity *v1.PodAntiAffinity
if podAntiRequirements == nil && podAntiPreferences == nil {
return podAntiAffinity
}
podAntiAffinity = &v1.PodAntiAffinity{}

if podAntiRequirements != nil {
podAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution = podAntiRequirements
}
if podAntiPreferences != nil {
podAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution = podAntiPreferences
}
return podAntiAffinity
}

func buildNodeAffinity(nodeRequirements []v1.NodeSelectorRequirement, nodePreferences []v1.NodeSelectorRequirement) *v1.NodeAffinity {
var nodeAffinity *v1.NodeAffinity
if nodeRequirements == nil && nodePreferences == nil {
return affinity
return nodeAffinity
}
affinity = &v1.Affinity{NodeAffinity: &v1.NodeAffinity{}}
nodeAffinity = &v1.NodeAffinity{}

if nodeRequirements != nil {
affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution = &v1.NodeSelector{
nodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution = &v1.NodeSelector{
NodeSelectorTerms: []v1.NodeSelectorTerm{{MatchExpressions: nodeRequirements}},
}
}
if nodePreferences != nil {
affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution = []v1.PreferredSchedulingTerm{
nodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution = []v1.PreferredSchedulingTerm{
{Weight: 1, Preference: v1.NodeSelectorTerm{MatchExpressions: nodePreferences}},
}
}
return affinity
return nodeAffinity
}
14 changes: 14 additions & 0 deletions pkg/utils/pod/scheduling.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,17 @@ func IsOwnedBy(pod *v1.Pod, gvks []schema.GroupVersionKind) bool {
}
return false
}

// HasPodAffinity returns true if a non-empty PodAffinity is defined in the pod spec
func HasPodAffinity(pod *v1.Pod) bool {
return pod.Spec.Affinity.PodAffinity != nil &&
(len(pod.Spec.Affinity.PodAffinity.RequiredDuringSchedulingIgnoredDuringExecution) != 0 ||
len(pod.Spec.Affinity.PodAffinity.PreferredDuringSchedulingIgnoredDuringExecution) != 0)
}

// HasPodAntiAffinity returns true if a non-empty PodAntiAffinity is defined in the pod spec
func HasPodAntiAffinity(pod *v1.Pod) bool {
return pod.Spec.Affinity.PodAntiAffinity != nil &&
(len(pod.Spec.Affinity.PodAntiAffinity.RequiredDuringSchedulingIgnoredDuringExecution) != 0 ||
len(pod.Spec.Affinity.PodAntiAffinity.PreferredDuringSchedulingIgnoredDuringExecution) != 0)
}