Skip to content

Commit 664932a

Browse files
committed
Revert "[release-0.15] šŸ› hasLabels and matchingLabels step on each other (kubernetes-sigs#2373)"
This reverts commit 0e37217.
1 parent 36bb899 commit 664932a

File tree

2 files changed

+5
-60
lines changed

2 files changed

+5
-60
lines changed

ā€Žpkg/client/options.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -513,15 +513,8 @@ type MatchingLabels map[string]string
513513
// ApplyToList applies this configuration to the given list options.
514514
func (m MatchingLabels) ApplyToList(opts *ListOptions) {
515515
// TODO(directxman12): can we avoid reserializing this over and over?
516-
if opts.LabelSelector == nil {
517-
opts.LabelSelector = labels.NewSelector()
518-
}
519-
// If there's already a selector, we need to AND the two together.
520-
noValidSel := labels.SelectorFromValidatedSet(map[string]string(m))
521-
reqs, _ := noValidSel.Requirements()
522-
for _, req := range reqs {
523-
opts.LabelSelector = opts.LabelSelector.Add(req)
524-
}
516+
sel := labels.SelectorFromValidatedSet(map[string]string(m))
517+
opts.LabelSelector = sel
525518
}
526519

527520
// ApplyToDeleteAllOf applies this configuration to the given an List options.
@@ -535,17 +528,14 @@ type HasLabels []string
535528

536529
// ApplyToList applies this configuration to the given list options.
537530
func (m HasLabels) ApplyToList(opts *ListOptions) {
538-
if opts.LabelSelector == nil {
539-
opts.LabelSelector = labels.NewSelector()
540-
}
541-
// TODO: ignore invalid labels will result in an empty selector.
542-
// This is inconsistent to the that of MatchingLabels.
531+
sel := labels.NewSelector()
543532
for _, label := range m {
544533
r, err := labels.NewRequirement(label, selection.Exists, nil)
545534
if err == nil {
546-
opts.LabelSelector = opts.LabelSelector.Add(*r)
535+
sel = sel.Add(*r)
547536
}
548537
}
538+
opts.LabelSelector = sel
549539
}
550540

551541
// ApplyToDeleteAllOf applies this configuration to the given an List options.

ā€Žpkg/client/options_test.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,6 @@ var _ = Describe("MatchingLabels", func() {
237237
expectedErrMsg := `values[0][k]: Invalid value: "axahm2EJ8Phiephe2eixohbee9eGeiyees1thuozi1xoh0GiuH3diewi8iem7Nui": must be no more than 63 characters`
238238
Expect(err.Error()).To(Equal(expectedErrMsg))
239239
})
240-
241-
It("Should add matchingLabels to existing selector", func() {
242-
listOpts := &client.ListOptions{}
243-
244-
matchingLabels := client.MatchingLabels(map[string]string{"k": "v"})
245-
matchingLabels2 := client.MatchingLabels(map[string]string{"k2": "v2"})
246-
247-
matchingLabels.ApplyToList(listOpts)
248-
Expect(listOpts.LabelSelector.String()).To(Equal("k=v"))
249-
250-
matchingLabels2.ApplyToList(listOpts)
251-
Expect(listOpts.LabelSelector.String()).To(Equal("k=v,k2=v2"))
252-
})
253240
})
254241

255242
var _ = Describe("FieldOwner", func() {
@@ -305,35 +292,3 @@ var _ = Describe("ForceOwnership", func() {
305292
Expect(*o.Force).To(Equal(true))
306293
})
307294
})
308-
309-
var _ = Describe("HasLabels", func() {
310-
It("Should produce hasLabels in given order", func() {
311-
listOpts := &client.ListOptions{}
312-
313-
hasLabels := client.HasLabels([]string{"labelApe", "labelFox"})
314-
hasLabels.ApplyToList(listOpts)
315-
Expect(listOpts.LabelSelector.String()).To(Equal("labelApe,labelFox"))
316-
})
317-
318-
It("Should add hasLabels to existing hasLabels selector", func() {
319-
listOpts := &client.ListOptions{}
320-
321-
hasLabel := client.HasLabels([]string{"labelApe"})
322-
hasLabel.ApplyToList(listOpts)
323-
324-
hasOtherLabel := client.HasLabels([]string{"labelFox"})
325-
hasOtherLabel.ApplyToList(listOpts)
326-
Expect(listOpts.LabelSelector.String()).To(Equal("labelApe,labelFox"))
327-
})
328-
329-
It("Should add hasLabels to existing matchingLabels", func() {
330-
listOpts := &client.ListOptions{}
331-
332-
matchingLabels := client.MatchingLabels(map[string]string{"k": "v"})
333-
matchingLabels.ApplyToList(listOpts)
334-
335-
hasLabel := client.HasLabels([]string{"labelApe"})
336-
hasLabel.ApplyToList(listOpts)
337-
Expect(listOpts.LabelSelector.String()).To(Equal("k=v,labelApe"))
338-
})
339-
})

0 commit comments

Comments
Ā (0)