Skip to content

Commit 2b949dd

Browse files
committed
Drop dependency on kcp-dev kubernetes fork
- Remove customizable keyfunction - Add ability to customize function used to build new informers - Add multicluster SharedInformer implementation - Embed cluster name in reconcile.Request instead of client.ObjectKey
1 parent 6ae831c commit 2b949dd

File tree

12 files changed

+755
-57
lines changed

12 files changed

+755
-57
lines changed

go.mod

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,3 @@ require (
6666
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
6767
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
6868
)
69-
70-
replace (
71-
k8s.io/api => github.com/kcp-dev/kubernetes/staging/src/k8s.io/api v0.0.0-20220524063253-5bb0eeecf2cf
72-
k8s.io/apimachinery => github.com/kcp-dev/kubernetes/staging/src/k8s.io/apimachinery v0.0.0-20220524063253-5bb0eeecf2cf
73-
k8s.io/client-go => github.com/kcp-dev/kubernetes/staging/src/k8s.io/client-go v0.0.0-20220524063253-5bb0eeecf2cf
74-
)

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,6 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
294294
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
295295
github.com/kcp-dev/apimachinery v0.0.0-20220518152549-f62703561e55 h1:nwrXHxVkTQ9BI6slgqWgg8CVC+7AbJR02byYq+iBOUg=
296296
github.com/kcp-dev/apimachinery v0.0.0-20220518152549-f62703561e55/go.mod h1:FIzhTU6DM3HYZhGv8w/1S/mbmSv1HzulZpjr/1/6i/I=
297-
github.com/kcp-dev/kubernetes/staging/src/k8s.io/api v0.0.0-20220524063253-5bb0eeecf2cf h1:b3UxxmQEB0ZGXGmlGOhFPElpaLPoEEjNneH6ipn4WdE=
298-
github.com/kcp-dev/kubernetes/staging/src/k8s.io/api v0.0.0-20220524063253-5bb0eeecf2cf/go.mod h1:YaTOAfRXJ+yMgQtYx2JxXdxGGoAonb1tg1rTLU0IT54=
299-
github.com/kcp-dev/kubernetes/staging/src/k8s.io/apimachinery v0.0.0-20220524063253-5bb0eeecf2cf h1:BiQgcrehqYiTG507kL3GFJtR5ni/fztwll9VodvlEd4=
300-
github.com/kcp-dev/kubernetes/staging/src/k8s.io/apimachinery v0.0.0-20220524063253-5bb0eeecf2cf/go.mod h1:O3MicknNAEIDH6QnfbYhov76VLccl2SLhbR1odSgttE=
301-
github.com/kcp-dev/kubernetes/staging/src/k8s.io/client-go v0.0.0-20220524063253-5bb0eeecf2cf h1:+8s2nSOTIZvG1kCxfDGQaq0aTkXJGsWhda1tyn3/bdc=
302-
github.com/kcp-dev/kubernetes/staging/src/k8s.io/client-go v0.0.0-20220524063253-5bb0eeecf2cf/go.mod h1:9wQfirZbMt4ylVsssW6p1Q1YUzB3Mk9LgcCzmExzE8A=
303297
github.com/kcp-dev/logicalcluster v1.0.0 h1:qCnoUNaqJ0Tgefkj3vXn17EC76AP44WZwnIuHLw6yGQ=
304298
github.com/kcp-dev/logicalcluster v1.0.0/go.mod h1:M0CBFkJTW29XtIP5XIkDfhYQ8LU6HrnseRb4zmgBltE=
305299
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
@@ -931,9 +925,15 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
931925
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
932926
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
933927
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
928+
k8s.io/api v0.23.5 h1:zno3LUiMubxD/V1Zw3ijyKO3wxrhbUF1Ck+VjBvfaoA=
929+
k8s.io/api v0.23.5/go.mod h1:Na4XuKng8PXJ2JsploYYrivXrINeTaycCGcYgF91Xm8=
934930
k8s.io/apiextensions-apiserver v0.23.5 h1:5SKzdXyvIJKu+zbfPc3kCbWpbxi+O+zdmAJBm26UJqI=
935931
k8s.io/apiextensions-apiserver v0.23.5/go.mod h1:ntcPWNXS8ZPKN+zTXuzYMeg731CP0heCTl6gYBxLcuQ=
932+
k8s.io/apimachinery v0.23.5 h1:Va7dwhp8wgkUPWsEXk6XglXWU4IKYLKNlv8VkX7SDM0=
933+
k8s.io/apimachinery v0.23.5/go.mod h1:BEuFMMBaIbcOqVIJqNZJXGFTP4W6AycEpb5+m/97hrM=
936934
k8s.io/apiserver v0.23.5/go.mod h1:7wvMtGJ42VRxzgVI7jkbKvMbuCbVbgsWFT7RyXiRNTw=
935+
k8s.io/client-go v0.23.5 h1:zUXHmEuqx0RY4+CsnkOn5l0GU+skkRXKGJrhmE2SLd8=
936+
k8s.io/client-go v0.23.5/go.mod h1:flkeinTO1CirYgzMPRWxUCnV0G4Fbu2vLhYCObnt/r4=
937937
k8s.io/code-generator v0.23.5/go.mod h1:S0Q1JVA+kSzTI1oUvbKAxZY/DYbA/ZUb4Uknog12ETk=
938938
k8s.io/component-base v0.23.5 h1:8qgP5R6jG1BBSXmRYW+dsmitIrpk8F/fPEvgDenMCCE=
939939
k8s.io/component-base v0.23.5/go.mod h1:c5Nq44KZyt1aLl0IpHX82fhsn84Sb0jjzwjpcA42bY0=

pkg/cache/cache.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ type Options struct {
109109
// So that all informers will not send list requests simultaneously.
110110
Resync *time.Duration
111111

112-
// KeyFunction is the cache.KeyFunc that the informers will be configured to use.
113-
// Defaults to cache.MetaNamespaceKeyFunc from client-go
114-
KeyFunction cache.KeyFunc
112+
// NewInformerFunc is a function that is used to create SharedIndexInformers.
113+
// Defaults to cache.NewSharedIndexInformer from client-go
114+
NewInformerFunc client.NewInformerFunc
115115

116116
// Indexers is the indexers that the informers will be configured to use.
117117
// Will always have the standard NamespaceIndex.
@@ -155,7 +155,7 @@ func New(config *rest.Config, opts Options) (Cache, error) {
155155
if err != nil {
156156
return nil, err
157157
}
158-
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync, opts.Namespace, selectorsByGVK, disableDeepCopyByGVK, opts.KeyFunction, opts.Indexers)
158+
im := internal.NewInformersMap(config, opts.Scheme, opts.Mapper, *opts.Resync, opts.Namespace, selectorsByGVK, disableDeepCopyByGVK, opts.NewInformerFunc, opts.Indexers)
159159
return &informerCache{InformersMap: im}, nil
160160
}
161161

@@ -209,8 +209,8 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
209209
opts.Resync = &defaultResyncTime
210210
}
211211

212-
if opts.KeyFunction == nil {
213-
opts.KeyFunction = cache.MetaNamespaceKeyFunc
212+
if opts.NewInformerFunc == nil {
213+
opts.NewInformerFunc = cache.NewSharedIndexInformer
214214
}
215215
return opts, nil
216216
}

pkg/cache/internal/cache_reader.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
"sigs.k8s.io/controller-runtime/pkg/client"
3737
)
3838

39+
// TODO(KCP) need to implement and plumb through our own cache reader
40+
3941
// CacheReader is a client.Reader.
4042
var _ client.Reader = &CacheReader{}
4143

@@ -57,11 +59,11 @@ type CacheReader struct {
5759
}
5860

5961
// Get checks the indexer for the object and writes a copy of it if found.
60-
func (c *CacheReader) Get(_ context.Context, key client.ObjectKey, out client.Object) error {
62+
func (c *CacheReader) Get(ctx context.Context, key client.ObjectKey, out client.Object) error {
6163
if c.scopeName == apimeta.RESTScopeNameRoot {
6264
key.Namespace = ""
6365
}
64-
storeKey := objectKeyToStoreKey(key)
66+
storeKey := objectKeyToStoreKey(ctx, key)
6567

6668
// Lookup the object from the indexer cache
6769
obj, exists, err := c.indexer.GetByKey(storeKey)
@@ -194,8 +196,16 @@ func (c *CacheReader) List(ctx context.Context, out client.ObjectList, opts ...c
194196
// It's akin to MetaNamespaceKeyFunc. It's separate from
195197
// String to allow keeping the key format easily in sync with
196198
// MetaNamespaceKeyFunc.
197-
func objectKeyToStoreKey(k client.ObjectKey) string {
198-
return kcpcache.ToClusterAwareKey(k.Cluster.String(), k.Namespace, k.Name)
199+
func objectKeyToStoreKey(ctx context.Context, k client.ObjectKey) string {
200+
cluster, ok := kcpclient.ClusterFromContext(ctx)
201+
if ok {
202+
return kcpcache.ToClusterAwareKey(cluster.String(), k.Namespace, k.Name)
203+
}
204+
205+
if k.Namespace == "" {
206+
return k.Name
207+
}
208+
return k.Namespace + "/" + k.Name
199209
}
200210

201211
// requiresExactMatch checks if the given field selector is of the form `k=v` or `k==v`.

pkg/cache/internal/deleg_map.go

Lines changed: 12 additions & 10 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.
@@ -52,13 +54,13 @@ func NewInformersMap(config *rest.Config,
5254
namespace string,
5355
selectors SelectorsByGVK,
5456
disableDeepCopy DisableDeepCopyByGVK,
55-
keyFunc cache.KeyFunc,
57+
newInformerFunc client.NewInformerFunc,
5658
indexers cache.Indexers,
5759
) *InformersMap {
5860
return &InformersMap{
59-
structured: newStructuredInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, keyFunc, indexers),
60-
unstructured: newUnstructuredInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, keyFunc, indexers),
61-
metadata: newMetadataInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, keyFunc, indexers),
61+
structured: newStructuredInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, newInformerFunc, indexers),
62+
unstructured: newUnstructuredInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, newInformerFunc, indexers),
63+
metadata: newMetadataInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, newInformerFunc, indexers),
6264

6365
Scheme: scheme,
6466
}
@@ -110,18 +112,18 @@ func (m *InformersMap) Get(ctx context.Context, gvk schema.GroupVersionKind, obj
110112

111113
// newStructuredInformersMap creates a new InformersMap for structured objects.
112114
func newStructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration,
113-
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, keyFunc cache.KeyFunc, indexers cache.Indexers) *specificInformersMap {
114-
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, createStructuredListWatch, keyFunc, indexers)
115+
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, newInformerFunc client.NewInformerFunc, indexers cache.Indexers) *specificInformersMap {
116+
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, createStructuredListWatch, newInformerFunc, indexers)
115117
}
116118

117119
// newUnstructuredInformersMap creates a new InformersMap for unstructured objects.
118120
func newUnstructuredInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration,
119-
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, keyFunc cache.KeyFunc, indexers cache.Indexers) *specificInformersMap {
120-
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, createUnstructuredListWatch, keyFunc, indexers)
121+
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, newInformerFunc client.NewInformerFunc, indexers cache.Indexers) *specificInformersMap {
122+
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, createUnstructuredListWatch, newInformerFunc, indexers)
121123
}
122124

123125
// newMetadataInformersMap creates a new InformersMap for metadata-only objects.
124126
func newMetadataInformersMap(config *rest.Config, scheme *runtime.Scheme, mapper meta.RESTMapper, resync time.Duration,
125-
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, keyFunc cache.KeyFunc, indexers cache.Indexers) *specificInformersMap {
126-
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, createMetadataListWatch, keyFunc, indexers)
127+
namespace string, selectors SelectorsByGVK, disableDeepCopy DisableDeepCopyByGVK, newInformerFunc client.NewInformerFunc, indexers cache.Indexers) *specificInformersMap {
128+
return newSpecificInformersMap(config, scheme, mapper, resync, namespace, selectors, disableDeepCopy, createMetadataListWatch, newInformerFunc, indexers)
127129
}

pkg/cache/internal/informers_map.go

Lines changed: 7 additions & 6 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

@@ -55,7 +56,7 @@ func newSpecificInformersMap(config *rest.Config,
5556
selectors SelectorsByGVK,
5657
disableDeepCopy DisableDeepCopyByGVK,
5758
createListWatcher createListWatcherFunc,
58-
keyFunction cache.KeyFunc,
59+
newInformerFunc client.NewInformerFunc,
5960
indexers cache.Indexers) *specificInformersMap {
6061

6162
ip := &specificInformersMap{
@@ -71,7 +72,7 @@ func newSpecificInformersMap(config *rest.Config,
7172
namespace: namespace,
7273
selectors: selectors.forGVK,
7374
disableDeepCopy: disableDeepCopy,
74-
keyFunction: keyFunction,
75+
newInformerFunc: newInformerFunc,
7576
additionalIndexers: indexers,
7677
}
7778
return ip
@@ -147,6 +148,8 @@ type specificInformersMap struct {
147148
// additionalIndexers is the indexers that the informers will be configured to use.
148149
// Will not allow overwriting the standard NamespaceIndex.
149150
additionalIndexers cache.Indexers
151+
152+
newInformerFunc client.NewInformerFunc
150153
}
151154

152155
// Start calls Run on each of the informers and sets started to true. Blocks on the context.
@@ -242,10 +245,8 @@ func (ip *specificInformersMap) addInformerToMap(gvk schema.GroupVersionKind, ob
242245
}
243246
indexers[cache.NamespaceIndex] = cache.MetaNamespaceIndexFunc
244247

245-
ni := cache.NewSharedIndexInformerWithOptions(lw, obj,
246-
cache.WithResyncPeriod(resyncPeriod(ip.resync)()),
247-
cache.WithKeyFunction(ip.keyFunction),
248-
cache.WithIndexers(indexers))
248+
ni := ip.newInformerFunc(lw, obj, resyncPeriod(ip.resync)(), indexers)
249+
249250
rm, err := ip.mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
250251
if err != nil {
251252
return nil, false, err

pkg/client/interfaces.go

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

1919
import (
2020
"context"
21-
22-
"github.com/kcp-dev/logicalcluster"
21+
"time"
2322

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

2626
"k8s.io/apimachinery/pkg/api/meta"
2727
"k8s.io/apimachinery/pkg/runtime"
2828
"k8s.io/apimachinery/pkg/types"
2929
"k8s.io/apimachinery/pkg/watch"
3030
)
3131

32-
type ObjectKey struct {
33-
types.NamespacedName
34-
35-
Cluster logicalcluster.Name
36-
}
32+
type ObjectKey = types.NamespacedName
3733

3834
// ObjectKeyFromObject returns the ObjectKey given a runtime.Object.
3935
func ObjectKeyFromObject(obj Object) ObjectKey {
40-
return ObjectKey{NamespacedName: types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}, Cluster: logicalcluster.From(obj)}
36+
return ObjectKey{Namespace: obj.GetNamespace(), Name: obj.GetName()}
4137
}
4238

4339
// Patch is a patch that can be applied to a Kubernetes object.
@@ -48,6 +44,10 @@ type Patch interface {
4844
Data(obj Object) ([]byte, error)
4945
}
5046

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

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

pkg/handler/enqueue.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package handler
1818

1919
import (
2020
"github.com/kcp-dev/logicalcluster"
21-
"k8s.io/apimachinery/pkg/types"
2221
"k8s.io/client-go/util/workqueue"
2322
"sigs.k8s.io/controller-runtime/pkg/client"
2423
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -77,11 +76,12 @@ func (e *EnqueueRequestForObject) Generic(evt event.GenericEvent, q workqueue.Ra
7776
}
7877

7978
func request(obj client.Object) reconcile.Request {
80-
return reconcile.Request{client.ObjectKey{
81-
Cluster: logicalcluster.From(obj),
82-
NamespacedName: types.NamespacedName{
79+
return reconcile.Request{
80+
// TODO(kcp) Need to implement a non-kcp-specific way to support this
81+
ClusterName: logicalcluster.From(obj).String(),
82+
ObjectKey: client.ObjectKey{
8383
Namespace: obj.GetNamespace(),
8484
Name: obj.GetName(),
8585
},
86-
}}
86+
}
8787
}

pkg/handler/enqueue_owner.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/apimachinery/pkg/runtime"
2626
"k8s.io/apimachinery/pkg/runtime/schema"
27-
"k8s.io/apimachinery/pkg/types"
2827
"k8s.io/client-go/util/workqueue"
2928
"sigs.k8s.io/controller-runtime/pkg/client"
3029
"sigs.k8s.io/controller-runtime/pkg/event"
@@ -137,11 +136,9 @@ func (e *EnqueueRequestForOwner) getOwnerReconcileRequest(object metav1.Object,
137136
if ref.Kind == e.groupKind.Kind && refGV.Group == e.groupKind.Group {
138137
// Match found - add a Request for the object referred to in the OwnerReference
139138
request := reconcile.Request{
139+
ClusterName: logicalcluster.From(object).String(),
140140
ObjectKey: client.ObjectKey{
141-
Cluster: logicalcluster.From(object),
142-
NamespacedName: types.NamespacedName{
143-
Name: ref.Name,
144-
},
141+
Name: ref.Name,
145142
},
146143
}
147144

0 commit comments

Comments
 (0)