Skip to content

Commit

Permalink
Merge pull request #57508 from chechiachang/enable-label-selector-for…
Browse files Browse the repository at this point in the history
…-client-go-listwatch

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Enable list option modification when create list watch

**What this PR does / why we need it**:
metav1.ListOptions support both field selector and label selector, but the current NewListWatchFromClient in client-go only support field selector.
 It would be helpful to use label selector in client-go.

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
None
```
  • Loading branch information
Kubernetes Submit Queue committed Jan 8, 2018
2 parents df08069 + 2780b8f commit 8504591
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions staging/src/k8s.io/client-go/tools/cache/listwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,18 @@ type Getter interface {

// NewListWatchFromClient creates a new ListWatch from the specified client, resource, namespace and field selector.
func NewListWatchFromClient(c Getter, resource string, namespace string, fieldSelector fields.Selector) *ListWatch {
listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
optionsModifier := func(options *metav1.ListOptions) {
options.FieldSelector = fieldSelector.String()
}
return NewFilteredListWatchFromClient(c, resource, namespace, optionsModifier)
}

// NewFilteredListWatchFromClient creates a new ListWatch from the specified client, resource, namespace, and option modifier.
// Option modifier is a function takes a ListOptions and modifies the consumed ListOptions. Provide customized modifier function
// to apply modification to ListOptions with a field selector, a label selector, or any other desired options.
func NewFilteredListWatchFromClient(c Getter, resource string, namespace string, optionsModifier func(options *metav1.ListOptions)) *ListWatch {
listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
optionsModifier(&options)
return c.Get().
Namespace(namespace).
Resource(resource).
Expand All @@ -74,7 +84,7 @@ func NewListWatchFromClient(c Getter, resource string, namespace string, fieldSe
}
watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
options.Watch = true
options.FieldSelector = fieldSelector.String()
optionsModifier(&options)
return c.Get().
Namespace(namespace).
Resource(resource).
Expand Down

0 comments on commit 8504591

Please sign in to comment.