Skip to content

Commit 91bfe62

Browse files
webhook: improve nodeSelectorsOverlap to detect subset overlaps
Previously, nodeSelectorsOverlap only detected identical selectors using string equality. This missed partial overlaps where one selector is a subset of another, allowing conflicting rules to be created. Fix by using sel.Matches() to check if one selector's matchLabels satisfies the other selector, catching subset overlap cases. Add test case for subset selector overlap scenario. Add comment noting matchExpressions limitation. Signed-off-by: Shreya2005-2005 <bhakatmistu@email.com> Signed-off-by: Shreya2005-2005 <bhakatmistu2005@gmail.com>
1 parent 7514218 commit 91bfe62

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

internal/webhook/nodereadinessgaterule_webhook.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ func (w *NodeReadinessRuleWebhook) nodeSelectorsOverlap(selector1, selector2 met
125125

126126
// Check overlap: if one selector's matchLabels is a subset of the other,
127127
// a node could match both selectors, causing a conflict.
128+
// Note: this only covers matchLabels-based overlap; selectors using
129+
// matchExpressions may still overlap without being detected here.
128130
if sel1.Matches(labels.Set(selector2.MatchLabels)) {
129131
return true
130132
}

internal/webhook/nodereadinessgaterule_webhook_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,18 @@ var _ = Describe("NodeReadinessRule Validation Webhook", func() {
330330
overlaps := webhook.nodeSelectorsOverlap(selector1, selector2)
331331
Expect(overlaps).To(BeFalse()) // Different selectors don't overlap (simple heuristic)
332332
})
333+
It("should detect subset selectors as overlapping", func() {
334+
// selector1 (env=prod) is subset of selector2 (env=prod,region=us)
335+
// Both match nodes labeled env=prod,region=us => they overlap
336+
selector1 := metav1.LabelSelector{
337+
MatchLabels: map[string]string{"env": "prod"},
338+
}
339+
selector2 := metav1.LabelSelector{
340+
MatchLabels: map[string]string{"env": "prod", "region": "us"},
341+
}
342+
overlaps := webhook.nodeSelectorsOverlap(selector1, selector2)
343+
Expect(overlaps).To(BeTrue()) // subset selectors overlap
344+
})
333345
})
334346

335347
Context("CustomValidator Interface", func() {

0 commit comments

Comments
 (0)