Skip to content

Commit

Permalink
support matchLabelKeys for topology
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdeal committed Dec 8, 2023
1 parent 8df2227 commit 54af319
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/controllers/provisioning/scheduling/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ func (t *Topology) countDomains(ctx context.Context, tg *TopologyGroup) error {
func (t *Topology) newForTopologies(p *v1.Pod) []*TopologyGroup {
var topologyGroups []*TopologyGroup
for _, cs := range p.Spec.TopologySpreadConstraints {
for _, label := range cs.MatchLabelKeys {
if value, ok := p.ObjectMeta.Labels[label]; ok {
cs.LabelSelector.MatchLabels[label] = value
}
}
topologyGroups = append(topologyGroups, NewTopologyGroup(TopologyTypeSpread, cs.TopologyKey, p, sets.New(p.Namespace),
cs.LabelSelector, cs.MaxSkew, cs.MinDomains, cs.NodeTaintsPolicy, cs.NodeAffinityPolicy, t.domains[cs.TopologyKey]))
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/controllers/provisioning/scheduling/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ var _ = Describe("Topology", func() {
ExpectSkew(ctx, env.Client, "default", &topology[0]).To(ConsistOf(2))
})

It("should support matchLabelKeys", func() {
if env.Version.Minor() < 27 {
Skip("Feature \"matchLabelKeys\" only available in K8S >= 1.27.x")
}
matchedLabel := "test-label"
topology := []v1.TopologySpreadConstraint{{
TopologyKey: v1.LabelHostname,
WhenUnsatisfiable: v1.DoNotSchedule,
LabelSelector: &metav1.LabelSelector{MatchLabels: labels},
MatchLabelKeys: []string{matchedLabel},
MaxSkew: 1,
}}
count := 2
pods := test.UnschedulablePods(test.PodOptions{
ObjectMeta: metav1.ObjectMeta{
Labels: lo.Assign(labels, map[string]string{matchedLabel: "value-a"}),
},
TopologySpreadConstraints: topology,
}, count)
pods = append(pods, test.UnschedulablePods(test.PodOptions{
ObjectMeta: metav1.ObjectMeta{
Labels: lo.Assign(labels, map[string]string{matchedLabel: "value-b"}),
},
TopologySpreadConstraints: topology,
}, count)...)
ExpectApplied(ctx, env.Client, nodePool)
ExpectProvisioned(ctx, env.Client, cluster, cloudProvider, prov, pods...)
ExpectSkew(ctx, env.Client, "default", &topology[0]).To(ConsistOf(2, 2))
})

Context("Zonal", func() {
It("should balance pods across zones (match labels)", func() {
topology := []v1.TopologySpreadConstraint{{
Expand Down

0 comments on commit 54af319

Please sign in to comment.