Skip to content

Commit

Permalink
simplified code
Browse files Browse the repository at this point in the history
  • Loading branch information
wusai80 committed Jun 2, 2023
1 parent 12dff25 commit 74d7b82
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions pkg/client/options.go
Expand Up @@ -513,19 +513,15 @@ type MatchingLabels map[string]string
// ApplyToList applies this configuration to the given list options.
func (m MatchingLabels) ApplyToList(opts *ListOptions) {
// TODO(directxman12): can we avoid reserializing this over and over?
var sel labels.Selector
if opts.LabelSelector != nil {
sel = opts.LabelSelector
} else {
sel = labels.NewSelector()
if opts.LabelSelector == nil {
opts.LabelSelector = labels.NewSelector()
}
// If there's already a selector, we need to AND the two together.
noValidSel := labels.SelectorFromValidatedSet(map[string]string(m))
reqs, _ := noValidSel.Requirements()
for _, req := range reqs {
sel = sel.Add(req)
opts.LabelSelector = opts.LabelSelector.Add(req)
}
opts.LabelSelector = sel
}

// ApplyToDeleteAllOf applies this configuration to the given an List options.
Expand All @@ -539,21 +535,17 @@ type HasLabels []string

// ApplyToList applies this configuration to the given list options.
func (m HasLabels) ApplyToList(opts *ListOptions) {
var sel labels.Selector
if opts.LabelSelector != nil {
sel = opts.LabelSelector
} else {
sel = labels.NewSelector()
if opts.LabelSelector == nil {
opts.LabelSelector = labels.NewSelector()
}
// TODO: ignore invalid labels will result in empty selector.
// This is not consistent to the MatchingLabels behavior.
// TODO: ignore invalid labels will result in an empty selector.
// This is inconsistent to the that of MatchingLabels.
for _, label := range m {
r, err := labels.NewRequirement(label, selection.Exists, nil)
if err == nil {
sel = sel.Add(*r)
opts.LabelSelector = opts.LabelSelector.Add(*r)
}
}
opts.LabelSelector = sel
}

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

0 comments on commit 74d7b82

Please sign in to comment.