Skip to content

Commit 739775c

Browse files
ncdcfabianvf
andcommitted
UPSTREAM: <carry>: support custom informer creation + indexers
Co-authored-by: Fabian von Feilitzsch <[email protected]>
1 parent a2888ea commit 739775c

File tree

4 files changed

+67
-26
lines changed

4 files changed

+67
-26
lines changed

pkg/cache/cache.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"k8s.io/apimachinery/pkg/runtime/schema"
2727
"k8s.io/client-go/kubernetes/scheme"
2828
"k8s.io/client-go/rest"
29+
"k8s.io/client-go/tools/cache"
2930
toolscache "k8s.io/client-go/tools/cache"
3031
"sigs.k8s.io/controller-runtime/pkg/cache/internal"
3132
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -108,6 +109,14 @@ type Options struct {
108109
// So that all informers will not send list requests simultaneously.
109110
Resync *time.Duration
110111

112+
// NewInformerFunc is a function that is used to create SharedIndexInformers.
113+
// Defaults to cache.NewSharedIndexInformer from client-go
114+
NewInformerFunc client.NewInformerFunc
115+
116+
// Indexers is the indexers that the informers will be configured to use.
117+
// Will always have the standard NamespaceIndex.
118+
Indexers cache.Indexers
119+
111120
// Namespace restricts the cache's ListWatch to the desired namespace
112121
// Default watches all namespaces
113122
Namespace string
@@ -163,7 +172,7 @@ func New(config *rest.Config, opts Options) (Cache, error) {
163172
return nil, err
164173
}
165174

166-
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync, opts.Namespace, selectorsByGVK, disableDeepCopyByGVK, transformByGVK)
175+
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync, opts.Namespace, selectorsByGVK, disableDeepCopyByGVK, transformByGVK, opts.NewInformerFunc, opts.Indexers)
167176
return &informerCache{InformersMap: im}, nil
168177
}
169178

@@ -216,6 +225,10 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
216225
if opts.Resync == nil {
217226
opts.Resync = &defaultResyncTime
218227
}
228+
229+
if opts.NewInformerFunc == nil {
230+
opts.NewInformerFunc = cache.NewSharedIndexInformer
231+
}
219232
return opts, nil
220233
}
221234

pkg/cache/internal/deleg_map.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"k8s.io/apimachinery/pkg/runtime/schema"
2828
"k8s.io/client-go/rest"
2929
"k8s.io/client-go/tools/cache"
30+
31+
"sigs.k8s.io/controller-runtime/pkg/client"
3032
)
3133

3234
// InformersMap create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs.
@@ -53,11 +55,13 @@ func NewInformersMap(config *rest.Config,
5355
selectors SelectorsByGVK,
5456
disableDeepCopy DisableDeepCopyByGVK,
5557
transformers TransformFuncByObject,
58+
newInformerFunc client.NewInformerFunc,
59+
indexers cache.Indexers,
5660
) *InformersMap {
5761
return &InformersMap{
58-
structured: newStructuredInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers),
59-
unstructured: newUnstructuredInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers),
60-
metadata: newMetadataInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers),
62+
structured: newStructuredInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, newInformerFunc, indexers),
63+
unstructured: newUnstructuredInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, newInformerFunc, indexers),
64+
metadata: newMetadataInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, newInformerFunc, indexers),
6165

6266
Scheme: scheme,
6367
}
@@ -109,18 +113,18 @@ func (m *InformersMap) Get(ctx context.Context, gvk schema.GroupVersionKind, obj
109113

110114
// newStructuredInformersMap creates a new InformersMap for structured objects.
111115
func newStructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration,
112-
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, transformers TransformFuncByObject) *specificInformersMap {
113-
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, createStructuredListWatch)
116+
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, transformers TransformFuncByObject, newInformerFunc client.NewInformerFunc, indexers cache.Indexers) *specificInformersMap {
117+
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, createStructuredListWatch, newInformerFunc, indexers)
114118
}
115119

116120
// newUnstructuredInformersMap creates a new InformersMap for unstructured objects.
117121
func newUnstructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration,
118-
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, transformers TransformFuncByObject) *specificInformersMap {
119-
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, createUnstructuredListWatch)
122+
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, transformers TransformFuncByObject, newInformerFunc client.NewInformerFunc, indexers cache.Indexers) *specificInformersMap {
123+
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, createUnstructuredListWatch, newInformerFunc, indexers)
120124
}
121125

122126
// newMetadataInformersMap creates a new InformersMap for metadata-only objects.
123127
func newMetadataInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration,
124-
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, transformers TransformFuncByObject) *specificInformersMap {
125-
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, createMetadataListWatch)
128+
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, transformers TransformFuncByObject, newInformerFunc client.NewInformerFunc, indexers cache.Indexers) *specificInformersMap {
129+
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, transformers, createMetadataListWatch, newInformerFunc, indexers)
126130
}

pkg/cache/internal/informers_map.go

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"k8s.io/client-go/rest"
3636
"k8s.io/client-go/tools/cache"
3737

38+
"sigs.k8s.io/controller-runtime/pkg/client"
3839
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3940
)
4041

@@ -56,21 +57,25 @@ func newSpecificInformersMap(config *rest.Config,
5657
disableDeepCopy DisableDeepCopyByGVK,
5758
transformers TransformFuncByObject,
5859
createListWatcher createListWatcherFunc,
60+
newInformerFunc client.NewInformerFunc,
61+
indexers cache.Indexers,
5962
) *specificInformersMap {
6063
ip := &specificInformersMap{
61-
config: config,
62-
Scheme: scheme,
63-
mapper: mapper,
64-
informersByGVK: make(map[schema.GroupVersionKind]*MapEntry),
65-
codecs: serializer.NewCodecFactory(scheme),
66-
paramCodec: runtime.NewParameterCodec(scheme),
67-
resync: resync,
68-
startWait: make(chan struct{}),
69-
createListWatcher: createListWatcher,
70-
namespace: namespace,
71-
selectors: selectors.forGVK,
72-
disableDeepCopy: disableDeepCopy,
73-
transformers: transformers,
64+
config: config,
65+
Scheme: scheme,
66+
mapper: mapper,
67+
informersByGVK: make(map[schema.GroupVersionKind]*MapEntry),
68+
codecs: serializer.NewCodecFactory(scheme),
69+
paramCodec: runtime.NewParameterCodec(scheme),
70+
resync: resync,
71+
startWait: make(chan struct{}),
72+
createListWatcher: createListWatcher,
73+
namespace: namespace,
74+
selectors: selectors.forGVK,
75+
disableDeepCopy: disableDeepCopy,
76+
transformers: transformers,
77+
newInformerFunc: newInformerFunc,
78+
additionalIndexers: indexers,
7479
}
7580
return ip
7681
}
@@ -141,6 +146,15 @@ type specificInformersMap struct {
141146

142147
// transform funcs are applied to objects before they are committed to the cache
143148
transformers TransformFuncByObject
149+
150+
// keyFunction is the cache.KeyFunc informers will be configured to use
151+
keyFunction cache.KeyFunc
152+
153+
// additionalIndexers is the indexers that the informers will be configured to use.
154+
// Will not allow overwriting the standard NamespaceIndex.
155+
additionalIndexers cache.Indexers
156+
157+
newInformerFunc client.NewInformerFunc
144158
}
145159

146160
// Start calls Run on each of the informers and sets started to true. Blocks on the context.
@@ -230,9 +244,13 @@ func (ip *specificInformersMap) addInformerToMap(gvk schema.GroupVersionKind, ob
230244
if err != nil {
231245
return nil, false, err
232246
}
233-
ni := cache.NewSharedIndexInformer(lw, obj, resyncPeriod(ip.resync)(), cache.Indexers{
234-
cache.NamespaceIndex: cache.MetaNamespaceIndexFunc,
235-
})
247+
indexers := cache.Indexers{}
248+
for indexName, indexer := range ip.additionalIndexers {
249+
indexers[indexName] = indexer
250+
}
251+
indexers[cache.NamespaceIndex] = cache.MetaNamespaceIndexFunc
252+
253+
ni := ip.newInformerFunc(lw, obj, resyncPeriod(ip.resync)(), indexers)
236254

237255
// Check to see if there is a transformer for this gvk
238256
if err := ni.SetTransform(ip.transformers.Get(gvk)); err != nil {

pkg/client/interfaces.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ package client
1818

1919
import (
2020
"context"
21+
"time"
2122

2223
apierrors "k8s.io/apimachinery/pkg/api/errors"
24+
"k8s.io/client-go/tools/cache"
2325

2426
"k8s.io/apimachinery/pkg/api/meta"
2527
"k8s.io/apimachinery/pkg/runtime"
@@ -43,6 +45,10 @@ type Patch interface {
4345
Data(obj Object) ([]byte, error)
4446
}
4547

48+
// NewInformerFunc describes a function that creates SharedIndexInformers.
49+
// Its signature matches cache.NewSharedIndexInformer from client-go
50+
type NewInformerFunc func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer
51+
4652
// TODO(directxman12): is there a sane way to deal with get/delete options?
4753

4854
// Reader knows how to read and list Kubernetes objects.

0 commit comments

Comments
 (0)