Skip to content

Commit cd3dd92

Browse files
authored
Merge pull request #1 from CSdread/314-label-selector
initial pass at adding label selector to watch
2 parents b3413a6 + d0169c6 commit cd3dd92

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

pkg/sdk/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Watch(apiVersion, kind, namespace string, resyncPeriod int, opts ...watchOp
5353
}
5454
o := newWatchOp()
5555
o.applyOpts(opts)
56-
informer := NewInformer(resourcePluralName, namespace, resourceClient, resyncPeriod, collector, o.numWorkers)
56+
informer := NewInformer(resourcePluralName, namespace, resourceClient, resyncPeriod, collector, o.numWorkers, o.labelSelector)
5757
informers = append(informers, informer)
5858
}
5959

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 int, c *metrics.Collector, n int) Informer {
49+
func NewInformer(resourcePluralName, namespace string, resourceClient dynamic.ResourceInterface, resyncPeriod int, c *metrics.Collector, n int, labelSelector string) Informer {
5050
i := &informer{
5151
resourcePluralName: resourcePluralName,
5252
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), resourcePluralName),
@@ -58,7 +58,7 @@ func NewInformer(resourcePluralName, namespace string, resourceClient dynamic.Re
5858

5959
resyncDuration := time.Duration(resyncPeriod) * time.Second
6060
i.sharedIndexInformer = cache.NewSharedIndexInformer(
61-
newListWatcherFromResourceClient(resourceClient), &unstructured.Unstructured{}, resyncDuration, cache.Indexers{},
61+
newListWatcherFromResourceClient(resourceClient, labelSelector), &unstructured.Unstructured{}, resyncDuration, cache.Indexers{},
6262
)
6363
i.sharedIndexInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
6464
AddFunc: i.handleAddResourceEvent,
@@ -68,11 +68,17 @@ func NewInformer(resourcePluralName, namespace string, resourceClient dynamic.Re
6868
return i
6969
}
7070

71-
func newListWatcherFromResourceClient(resourceClient dynamic.ResourceInterface) *cache.ListWatch {
71+
func newListWatcherFromResourceClient(resourceClient dynamic.ResourceInterface, labelSelector string) *cache.ListWatch {
7272
listFunc := func(options metav1.ListOptions) (runtime.Object, error) {
73+
if labelSelector != "" {
74+
options.LabelSelector = labelSelector
75+
}
7376
return resourceClient.List(options)
7477
}
7578
watchFunc := func(options metav1.ListOptions) (watch.Interface, error) {
79+
if labelSelector != "" {
80+
options.LabelSelector = labelSelector
81+
}
7682
return resourceClient.Watch(options)
7783
}
7884
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)