Skip to content

Commit 51288eb

Browse files
authored
Merge pull request #428 from CSdread/master
pkg/sdk: Add label selector for sdk.Watch()
2 parents 7152fb7 + 0988333 commit 51288eb

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

doc/user-guide.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,22 @@ func main() {
6565
}
6666
```
6767

68-
**Note:** The number of concurrent informer workers can be configured with an additional Watch option. The default value is 1 if an argument is not given.
68+
#### Options
69+
**Worker Count**
70+
The number of concurrent informer workers can be configured with an additional Watch option. The default value is 1 if an argument is not given.
6971
```Go
7072
sdk.Watch("cache.example.com/v1alpha1", "Memcached", "default", 5, sdk.WithNumWorkers(n))
7173
```
7274

75+
**Label Selector**
76+
Label selectors allow the watch to filter resources by kubernetes labels. It can be specified using the standard kubernetes label selector format:
77+
78+
https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
79+
80+
```Go
81+
sdk.Watch("cache.example.com/v1alpha1", "Memcached", "default", 5, sdk.WithLabelSelector("app=myapp"))
82+
```
83+
7384
### Define the Memcached spec and status
7485

7586
Modify the spec and status of the `Memcached` CR at `pkg/apis/cache/v1alpha1/types.go`:

pkg/sdk/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Watch(apiVersion, kind, namespace string, resyncPeriod time.Duration, opts
5454
}
5555
o := newWatchOp()
5656
o.applyOpts(opts)
57-
informer := NewInformer(resourcePluralName, namespace, resourceClient, resyncPeriod, collector, o.numWorkers)
57+
informer := NewInformer(resourcePluralName, namespace, resourceClient, resyncPeriod, collector, o.numWorkers, o.labelSelector)
5858
informers = append(informers, informer)
5959
}
6060

pkg/sdk/informer.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type informer struct {
4646
numWorkers int
4747
}
4848

49-
func NewInformer(resourcePluralName, namespace string, resourceClient dynamic.ResourceInterface, resyncPeriod time.Duration, c *metrics.Collector, n int) Informer {
49+
func NewInformer(resourcePluralName, namespace string, resourceClient dynamic.ResourceInterface, resyncPeriod time.Duration, c *metrics.Collector, n int, labelSelector string) Informer {
5050
i := &informer{
5151
resourcePluralName: resourcePluralName,
5252
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), resourcePluralName),
@@ -57,7 +57,7 @@ func NewInformer(resourcePluralName, namespace string, resourceClient dynamic.Re
5757
}
5858

5959
i.sharedIndexInformer = cache.NewSharedIndexInformer(
60-
newListWatcherFromResourceClient(resourceClient), &unstructured.Unstructured{}, resyncPeriod, cache.Indexers{},
60+
newListWatcherFromResourceClient(resourceClient, labelSelector), &unstructured.Unstructured{}, resyncPeriod, cache.Indexers{},
6161
)
6262
i.sharedIndexInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
6363
AddFunc: i.handleAddResourceEvent,
@@ -67,11 +67,17 @@ func NewInformer(resourcePluralName, namespace string, resourceClient dynamic.Re
6767
return i
6868
}
6969

70-
func newListWatcherFromResourceClient(resourceClient dynamic.ResourceInterface) *cache.ListWatch {
70+
func newListWatcherFromResourceClient(resourceClient dynamic.ResourceInterface, labelSelector string) *cache.ListWatch {
7171
listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
72+
if labelSelector != "" {
73+
options.LabelSelector = labelSelector
74+
}
7275
return resourceClient.List(options)
7376
}
7477
watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
78+
if labelSelector != "" {
79+
options.LabelSelector = labelSelector
80+
}
7581
return resourceClient.Watch(options)
7682
}
7783
return &cache.ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}

pkg/sdk/watch-opt.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ package sdk
1616

1717
// WatchOp wraps all the options for Watch().
1818
type watchOp struct {
19-
numWorkers int
19+
numWorkers int
20+
labelSelector string
2021
}
2122

2223
// NewWatchOp create a new deafult WatchOp
@@ -47,3 +48,9 @@ func WithNumWorkers(numWorkers int) watchOption {
4748
op.numWorkers = numWorkers
4849
}
4950
}
51+
52+
func WithLabelSelector(labelSelector string) watchOption {
53+
return func(op *watchOp) {
54+
op.labelSelector = labelSelector
55+
}
56+
}

0 commit comments

Comments
 (0)