Skip to content

Commit

Permalink
fix a bug for Exits opeartor, add corresponding test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
felix-zhe-huang committed Jan 18, 2022
1 parent 96e88d1 commit 4354ddf
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
40 changes: 40 additions & 0 deletions pkg/controllers/provisioning/scheduling/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,46 @@ var _ = Describe("Combined Constraints", func() {
))[0]
ExpectNotScheduled(ctx, env.Client, pod)
})
FIt("should schedule the pod with Exists operator and defined key", func() {
provisioner.Spec.Requirements = v1alpha5.NewRequirements(
v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpIn, Values: []string{"test-zone-1"}})
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, provisioner, test.UnschedulablePod(
test.PodOptions{NodeRequirements: []v1.NodeSelectorRequirement{
{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpExists, Values: []string{}},
}},
))[0]
ExpectScheduled(ctx, env.Client, pod)
})
FIt("should not schedule the pod with Exists operator and undefined key", func() {
provisioner.Spec.Requirements = v1alpha5.NewRequirements(
v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpIn, Values: []string{"test-zone-1"}})
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, provisioner, test.UnschedulablePod(
test.PodOptions{NodeRequirements: []v1.NodeSelectorRequirement{
{Key: "foo", Operator: v1.NodeSelectorOpExists, Values: []string{}},
}},
))[0]
ExpectNotScheduled(ctx, env.Client, pod)
})
FIt("should not schedule the pod with DoesNotExists operator and defined key", func() {
provisioner.Spec.Requirements = v1alpha5.NewRequirements(
v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpIn, Values: []string{"test-zone-1"}})
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, provisioner, test.UnschedulablePod(
test.PodOptions{NodeRequirements: []v1.NodeSelectorRequirement{
{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpDoesNotExist, Values: []string{}},
}},
))[0]
ExpectNotScheduled(ctx, env.Client, pod)
})
FIt("should schedule the pod with Exists operator and undefined key", func() {
provisioner.Spec.Requirements = v1alpha5.NewRequirements(
v1.NodeSelectorRequirement{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpIn, Values: []string{"test-zone-1"}})
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, provisioner, test.UnschedulablePod(
test.PodOptions{NodeRequirements: []v1.NodeSelectorRequirement{
{Key: "foo", Operator: v1.NodeSelectorOpDoesNotExist, Values: []string{}},
}},
))[0]
ExpectScheduled(ctx, env.Client, pod)
})
It("should schedule compatible requirements with Operator=In", func() {
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioners, provisioner, test.UnschedulablePod(
test.PodOptions{NodeRequirements: []v1.NodeSelectorRequirement{
Expand Down
17 changes: 17 additions & 0 deletions pkg/controllers/provisioning/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,23 @@ var _ = Describe("Provisioning", func() {
Expect(*node.Status.Allocatable.Cpu()).To(Equal(resource.MustParse("2")))
Expect(*node.Status.Allocatable.Memory()).To(Equal(resource.MustParse("2Gi")))
})
It("should account daemonsets with NotIn operator and unspecified key", func() {
ExpectCreated(ctx, env.Client, test.DaemonSet(
test.DaemonSetOptions{PodOptions: test.PodOptions{
NodeRequirements: []v1.NodeSelectorRequirement{{Key: "foo", Operator: v1.NodeSelectorOpNotIn, Values: []string{"bar"}}},
ResourceRequirements: v1.ResourceRequirements{Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}},
}},
))
pod := ExpectProvisioned(ctx, env.Client, selectionController, provisioningController, provisioner, test.UnschedulablePod(
test.PodOptions{
NodeRequirements: []v1.NodeSelectorRequirement{{Key: v1.LabelTopologyZone, Operator: v1.NodeSelectorOpIn, Values: []string{"test-zone-2"}}},
ResourceRequirements: v1.ResourceRequirements{Requests: v1.ResourceList{v1.ResourceCPU: resource.MustParse("1"), v1.ResourceMemory: resource.MustParse("1Gi")}},
},
))[0]
node := ExpectScheduled(ctx, env.Client, pod)
Expect(*node.Status.Allocatable.Cpu()).To(Equal(resource.MustParse("4")))
Expect(*node.Status.Allocatable.Memory()).To(Equal(resource.MustParse("4Gi")))
})
})
Context("Labels", func() {
It("should label nodes", func() {
Expand Down
4 changes: 3 additions & 1 deletion pkg/controllers/selection/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"fmt"
"time"

"github.com/aws/karpenter/pkg/apis/provisioning/v1alpha5"
"github.com/aws/karpenter/pkg/controllers/provisioning"
"github.com/aws/karpenter/pkg/utils/functional"
"github.com/aws/karpenter/pkg/utils/pod"
"github.com/go-logr/zapr"
"go.uber.org/multierr"
Expand Down Expand Up @@ -166,7 +168,7 @@ func validateNodeSelectorTerm(term v1.NodeSelectorTerm) (errs error) {
}
if term.MatchExpressions != nil {
for _, requirement := range term.MatchExpressions {
if !sets.NewString(string(v1.NodeSelectorOpIn), string(v1.NodeSelectorOpNotIn)).Has(string(requirement.Operator)) {
if !functional.ContainsString(v1alpha5.SupportedNodeSelectorOps, string(requirement.Operator)) {
errs = multierr.Append(errs, fmt.Errorf("node selector term has unsupported operator, %s", requirement.Operator))
}
}
Expand Down

0 comments on commit 4354ddf

Please sign in to comment.