Skip to content

Commit 68e8d9b

Browse files
committed
chore: rebase and fork pkg for v0.8.2
Signed-off-by: Anish Ramasekar <anish.ramasekar@gmail.com>
1 parent 120c5ec commit 68e8d9b

File tree

8 files changed

+166
-98
lines changed

8 files changed

+166
-98
lines changed

cmd/secrets-store-csi-driver/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"strings"
2626
"time"
2727

28+
"k8s.io/apimachinery/pkg/runtime/schema"
29+
2830
"sigs.k8s.io/secrets-store-csi-driver/pkg/cache"
2931
"sigs.k8s.io/secrets-store-csi-driver/pkg/metrics"
3032
"sigs.k8s.io/secrets-store-csi-driver/pkg/rotation"
@@ -110,18 +112,18 @@ func main() {
110112

111113
// this enables filtered watch of pods based on the node name
112114
// only pods running on the same node as the csi driver will be cached
113-
fieldSelectorByResource := map[string]string{
114-
"pods": fields.OneTermEqualSelector("spec.nodeName", *nodeID).String(),
115+
fieldSelectorByResource := map[schema.GroupResource]string{
116+
{Group: "", Resource: "pods"}: fields.OneTermEqualSelector("spec.nodeName", *nodeID).String(),
115117
}
116118
// this enables filtered watch of secretproviderclasspodstatuses based on the internal node label
117119
// internal.secrets-store.csi.k8s.io/node-name=<node name> added by csi driver
118-
labelSelectorByResource := map[string]string{
119-
"secretproviderclasspodstatuses": fmt.Sprintf("%s=%s", v1alpha1.InternalNodeLabel, *nodeID),
120+
labelSelectorByResource := map[schema.GroupResource]string{
121+
{Group: "", Resource: "secretproviderclasspodstatuses"}: fmt.Sprintf("%s=%s", v1alpha1.InternalNodeLabel, *nodeID),
120122
}
121123
// this enables filtered watch of secrets based on the label (secrets-store.csi.k8s.io/managed=true)
122124
// added to the secrets created by the CSI driver
123125
if *filteredWatchSecret {
124-
labelSelectorByResource["secrets"] = fmt.Sprintf("%s=true", controllers.SecretManagedLabel)
126+
labelSelectorByResource[schema.GroupResource{Group: "", Resource: "secrets"}] = fmt.Sprintf("%s=true", controllers.SecretManagedLabel)
125127
}
126128

127129
mgr, err := ctrl.NewManager(cfg, ctrl.Options{

controllers/secretproviderclasspodstatus_controller.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -364,26 +364,23 @@ func (r *SecretProviderClassPodStatusReconciler) SetupWithManager(mgr ctrl.Manag
364364
func (r *SecretProviderClassPodStatusReconciler) belongsToNodePredicate() predicate.Funcs {
365365
return predicate.Funcs{
366366
UpdateFunc: func(e event.UpdateEvent) bool {
367-
return r.processIfBelongsToNode(e.ObjectNew, e.MetaNew)
367+
return r.processIfBelongsToNode(e.ObjectNew)
368368
},
369369
CreateFunc: func(e event.CreateEvent) bool {
370-
return r.processIfBelongsToNode(e.Object, e.Meta)
370+
return r.processIfBelongsToNode(e.Object)
371371
},
372372
DeleteFunc: func(e event.DeleteEvent) bool {
373373
return false
374374
},
375375
GenericFunc: func(e event.GenericEvent) bool {
376-
return r.processIfBelongsToNode(e.Object, e.Meta)
376+
return r.processIfBelongsToNode(e.Object)
377377
},
378378
}
379379
}
380380

381381
// processIfBelongsToNode determines if the secretproviderclasspodstatus belongs to the node based on the
382382
// internal.secrets-store.csi.k8s.io/node-name: <node name> label. If belongs to node, then the spcps is processed.
383-
func (r *SecretProviderClassPodStatusReconciler) processIfBelongsToNode(obj runtime.Object, objMeta metav1.Object) bool {
384-
if _, ok := obj.(*v1alpha1.SecretProviderClassPodStatus); !ok {
385-
return false
386-
}
383+
func (r *SecretProviderClassPodStatusReconciler) processIfBelongsToNode(objMeta metav1.Object) bool {
387384
node, ok := objMeta.GetLabels()[v1alpha1.InternalNodeLabel]
388385
if !ok {
389386
return false

pkg/cache/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# sigs.k8s.io/controller-runtime/pkg/cache
2+
3+
The cache package has been forked from [`sigs.k8s.io/controller-runtime@a8c19c49e49cfba2aa486ff322cbe5222d6da533 (v0.8.2)`](https://github.com/kubernetes-sigs/controller-runtime/releases/tag/v0.8.2).
4+
5+
This fork has been modified to add the ability to perform filtered `ListWatch` based on the field or label selectors.
6+
7+
The original code for the cache package can be found at [https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache](https://github.com/kubernetes-sigs/controller-runtime/tree/v0.8.2/pkg/cache). We'll switch to using the default cache package in controller-runtime after this [Enable filtered list watches as watches](https://github.com/kubernetes-sigs/controller-runtime/issues/244) is implemented.

pkg/cache/cache.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"sigs.k8s.io/secrets-store-csi-driver/pkg/cache/internal"
2424

25+
"k8s.io/apimachinery/pkg/runtime/schema"
2526
"k8s.io/client-go/kubernetes/scheme"
2627
"k8s.io/client-go/rest"
2728
ctrl "sigs.k8s.io/controller-runtime"
@@ -31,15 +32,20 @@ import (
3132

3233
var log = ctrl.Log.WithName("object-cache")
3334

34-
var defaultResyncTime = 10 * time.Hour
35-
3635
// Options are the optional arguments for creating a new InformersMap object
3736
type Options struct {
3837
crcache.Options
39-
FieldSelectorByResource map[string]string
40-
LabelSelectorByResource map[string]string
38+
// FieldSelectorByResource restricts the cache's ListWatch to the resources with desired field
39+
// Default watches resources with any field
40+
FieldSelectorByResource map[schema.GroupResource]string
41+
42+
// LabelSelectorByResource restricts the cache's ListWatch to the resources with desired label
43+
// Default watches resources with any label
44+
LabelSelectorByResource map[schema.GroupResource]string
4145
}
4246

47+
var defaultResyncTime = 10 * time.Hour
48+
4349
// New initializes and returns a new Cache.
4450
func New(config *rest.Config, opts Options) (crcache.Cache, error) {
4551
opts, err := defaultOpts(config, opts)

pkg/cache/informer_cache.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ import (
2222
"reflect"
2323
"strings"
2424

25+
crcache "sigs.k8s.io/controller-runtime/pkg/cache"
26+
2527
"sigs.k8s.io/secrets-store-csi-driver/pkg/cache/internal"
2628

2729
apimeta "k8s.io/apimachinery/pkg/api/meta"
2830
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2931
"k8s.io/apimachinery/pkg/runtime"
3032
"k8s.io/apimachinery/pkg/runtime/schema"
3133
"k8s.io/client-go/tools/cache"
32-
crcache "sigs.k8s.io/controller-runtime/pkg/cache"
3334
"sigs.k8s.io/controller-runtime/pkg/client"
3435
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
3536
)
@@ -53,7 +54,7 @@ type informerCache struct {
5354
}
5455

5556
// Get implements Reader
56-
func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out runtime.Object) error {
57+
func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out client.Object) error {
5758
gvk, err := apiutil.GVKForObject(out, ip.Scheme)
5859
if err != nil {
5960
return err
@@ -71,7 +72,7 @@ func (ip *informerCache) Get(ctx context.Context, key client.ObjectKey, out runt
7172
}
7273

7374
// List implements Reader
74-
func (ip *informerCache) List(ctx context.Context, out runtime.Object, opts ...client.ListOption) error {
75+
func (ip *informerCache) List(ctx context.Context, out client.ObjectList, opts ...client.ListOption) error {
7576

7677
gvk, cacheTypeObj, err := ip.objectTypeForListObject(out)
7778
if err != nil {
@@ -93,7 +94,7 @@ func (ip *informerCache) List(ctx context.Context, out runtime.Object, opts ...c
9394
// objectTypeForListObject tries to find the runtime.Object and associated GVK
9495
// for a single object corresponding to the passed-in list type. We need them
9596
// because they are used as cache map key.
96-
func (ip *informerCache) objectTypeForListObject(list runtime.Object) (*schema.GroupVersionKind, runtime.Object, error) {
97+
func (ip *informerCache) objectTypeForListObject(list client.ObjectList) (*schema.GroupVersionKind, runtime.Object, error) {
9798
gvk, err := apiutil.GVKForObject(list, ip.Scheme)
9899
if err != nil {
99100
return nil, nil, err
@@ -148,7 +149,7 @@ func (ip *informerCache) GetInformerForKind(ctx context.Context, gvk schema.Grou
148149
}
149150

150151
// GetInformer returns the informer for the obj
151-
func (ip *informerCache) GetInformer(ctx context.Context, obj runtime.Object) (crcache.Informer, error) {
152+
func (ip *informerCache) GetInformer(ctx context.Context, obj client.Object) (crcache.Informer, error) {
152153
gvk, err := apiutil.GVKForObject(obj, ip.Scheme)
153154
if err != nil {
154155
return nil, err
@@ -172,7 +173,7 @@ func (ip *informerCache) NeedLeaderElection() bool {
172173
// to List. For one-to-one compatibility with "normal" field selectors, only return one value.
173174
// The values may be anything. They will automatically be prefixed with the namespace of the
174175
// given object, if present. The objects passed are guaranteed to be objects of the correct type.
175-
func (ip *informerCache) IndexField(ctx context.Context, obj runtime.Object, field string, extractValue client.IndexerFunc) error {
176+
func (ip *informerCache) IndexField(ctx context.Context, obj client.Object, field string, extractValue client.IndexerFunc) error {
176177
informer, err := ip.GetInformer(ctx, obj)
177178
if err != nil {
178179
return err
@@ -183,7 +184,7 @@ func (ip *informerCache) IndexField(ctx context.Context, obj runtime.Object, fie
183184
func indexByField(indexer crcache.Informer, field string, extractor client.IndexerFunc) error {
184185
indexFunc := func(objRaw interface{}) ([]string, error) {
185186
// TODO(directxman12): check if this is the correct type?
186-
obj, isObj := objRaw.(runtime.Object)
187+
obj, isObj := objRaw.(client.Object)
187188
if !isObj {
188189
return nil, fmt.Errorf("object of type %T is not an Object", objRaw)
189190
}

pkg/cache/internal/cache_reader.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/apimachinery/pkg/runtime/schema"
3030
"k8s.io/apimachinery/pkg/selection"
3131
"k8s.io/client-go/tools/cache"
32+
3233
"sigs.k8s.io/controller-runtime/pkg/client"
3334
)
3435

@@ -42,10 +43,16 @@ type CacheReader struct {
4243

4344
// groupVersionKind is the group-version-kind of the resource.
4445
groupVersionKind schema.GroupVersionKind
46+
47+
// scopeName is the scope of the resource (namespaced or cluster-scoped).
48+
scopeName apimeta.RESTScopeName
4549
}
4650

4751
// Get checks the indexer for the object and writes a copy of it if found
48-
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out runtime.Object) error {
52+
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Object) error {
53+
if c.scopeName == apimeta.RESTScopeNameRoot {
54+
key.Namespace = ""
55+
}
4956
storeKey := objectKeyToStoreKey(key)
5057

5158
// Lookup the object from the indexer cache
@@ -87,7 +94,7 @@ func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out runtime.O
8794
}
8895

8996
// List lists items out of the indexer and writes them to out
90-
func (c *CacheReader) List(_ context.Context, out runtime.Object, opts ...client.ListOption) error {
97+
func (c *CacheReader) List(_ context.Context, out client.ObjectList, opts ...client.ListOption) error {
9198
var objs []interface{}
9299
var err error
93100

pkg/cache/internal/deleg_map.go

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"k8s.io/apimachinery/pkg/api/meta"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2526
"k8s.io/apimachinery/pkg/runtime"
2627
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -31,10 +32,12 @@ import (
3132
// InformersMap create and caches Informers for (runtime.Object, schema.GroupVersionKind) pairs.
3233
// It uses a standard parameter codec constructed based on the given generated Scheme.
3334
type InformersMap struct {
34-
// we abstract over the details of structured vs unstructured with the specificInformerMaps
35+
// we abstract over the details of structured/unstructured/metadata with the specificInformerMaps
36+
// TODO(directxman12): genericize this over different projections now that we have 3 different maps
3537

3638
structured *specificInformersMap
3739
unstructured *specificInformersMap
40+
metadata *specificInformersMap
3841

3942
// Scheme maps runtime.Objects to GroupVersionKinds
4043
Scheme *runtime.Scheme
@@ -47,58 +50,72 @@ func NewInformersMap(config *rest.Config,
4750
mapper meta.RESTMapper,
4851
resync time.Duration,
4952
namespace string,
50-
fieldSelectorByResource, labelSelectorByResource map[string]string) *InformersMap {
53+
fieldSelectorByResource, labelSelectorByResource map[schema.GroupResource]string) *InformersMap {
5154

5255
return &InformersMap{
5356
structured: newStructuredInformersMap(config, scheme, mapper, resync, namespace, fieldSelectorByResource, labelSelectorByResource),
5457
unstructured: newUnstructuredInformersMap(config, scheme, mapper, resync, namespace, fieldSelectorByResource, labelSelectorByResource),
58+
metadata: newMetadataInformersMap(config, scheme, mapper, resync, namespace, fieldSelectorByResource, labelSelectorByResource),
5559

5660
Scheme: scheme,
5761
}
5862
}
5963

60-
// Start calls Run on each of the informers and sets started to true. Blocks on the stop channel.
61-
func (m *InformersMap) Start(stop <-chan struct{}) error {
62-
go m.structured.Start(stop)
63-
go m.unstructured.Start(stop)
64-
<-stop
64+
// Start calls Run on each of the informers and sets started to true. Blocks on the context.
65+
func (m *InformersMap) Start(ctx context.Context) error {
66+
go m.structured.Start(ctx)
67+
go m.unstructured.Start(ctx)
68+
go m.metadata.Start(ctx)
69+
<-ctx.Done()
6570
return nil
6671
}
6772

6873
// WaitForCacheSync waits until all the caches have been started and synced.
69-
func (m *InformersMap) WaitForCacheSync(stop <-chan struct{}) bool {
74+
func (m *InformersMap) WaitForCacheSync(ctx context.Context) bool {
7075
syncedFuncs := append([]cache.InformerSynced(nil), m.structured.HasSyncedFuncs()...)
7176
syncedFuncs = append(syncedFuncs, m.unstructured.HasSyncedFuncs()...)
77+
syncedFuncs = append(syncedFuncs, m.metadata.HasSyncedFuncs()...)
7278

73-
if !m.structured.waitForStarted(stop) {
79+
if !m.structured.waitForStarted(ctx) {
7480
return false
7581
}
76-
if !m.unstructured.waitForStarted(stop) {
82+
if !m.unstructured.waitForStarted(ctx) {
7783
return false
7884
}
79-
return cache.WaitForCacheSync(stop, syncedFuncs...)
85+
if !m.metadata.waitForStarted(ctx) {
86+
return false
87+
}
88+
return cache.WaitForCacheSync(ctx.Done(), syncedFuncs...)
8089
}
8190

8291
// Get will create a new Informer and add it to the map of InformersMap if none exists. Returns
8392
// the Informer from the map.
8493
func (m *InformersMap) Get(ctx context.Context, gvk schema.GroupVersionKind, obj runtime.Object) (bool, *MapEntry, error) {
85-
_, isUnstructured := obj.(*unstructured.Unstructured)
86-
_, isUnstructuredList := obj.(*unstructured.UnstructuredList)
87-
isUnstructured = isUnstructured || isUnstructuredList
88-
89-
if isUnstructured {
94+
switch obj.(type) {
95+
case *unstructured.Unstructured:
96+
return m.unstructured.Get(ctx, gvk, obj)
97+
case *unstructured.UnstructuredList:
9098
return m.unstructured.Get(ctx, gvk, obj)
99+
case *metav1.PartialObjectMetadata:
100+
return m.metadata.Get(ctx, gvk, obj)
101+
case *metav1.PartialObjectMetadataList:
102+
return m.metadata.Get(ctx, gvk, obj)
103+
default:
104+
return m.structured.Get(ctx, gvk, obj)
91105
}
92-
93-
return m.structured.Get(ctx, gvk, obj)
94106
}
95107

96108
// newStructuredInformersMap creates a new InformersMap for structured objects.
97-
func newStructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration, namespace string, fieldSelectorByResource, labelSelectorByResource map[string]string) *specificInformersMap {
98-
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, fieldSelectorByResource, labelSelectorByResource, createStructuredListWatch)
109+
func newStructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration, namespace string, fieldSelectorByResource, labelSelectorByResource map[schema.GroupResource]string) *specificInformersMap {
110+
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, createStructuredListWatch, fieldSelectorByResource, labelSelectorByResource)
99111
}
100112

101113
// newUnstructuredInformersMap creates a new InformersMap for unstructured objects.
102-
func newUnstructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration, namespace string, fieldSelectorByResource, labelSelectorByResource map[string]string) *specificInformersMap {
103-
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, fieldSelectorByResource, labelSelectorByResource, createUnstructuredListWatch)
114+
func newUnstructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration, namespace string, fieldSelectorByResource, labelSelectorByResource map[schema.GroupResource]string) *specificInformersMap {
115+
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, createUnstructuredListWatch, fieldSelectorByResource, labelSelectorByResource)
116+
}
117+
118+
// newMetadataInformersMap creates a new InformersMap for metadata-only objects.
119+
func newMetadataInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration, namespace string, fieldSelectorByResource, labelSelectorByResource map[schema.GroupResource]string) *specificInformersMap {
120+
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, createMetadataListWatch, fieldSelectorByResource, labelSelectorByResource)
104121
}

0 commit comments

Comments
 (0)