Skip to content

Commit 2605363

Browse files
authored
Merge pull request #54 from sttts/sttts-kube-informer-by-default
🐛 Instantiate kube index informers by default
2 parents e86462c + e50f901 commit 2605363

File tree

7 files changed

+97
-25
lines changed

7 files changed

+97
-25
lines changed

pkg/cache/cache.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"net/http"
2323
"time"
2424

25-
"github.com/kcp-dev/apimachinery/v2/third_party/informers"
2625
"golang.org/x/exp/maps"
2726
corev1 "k8s.io/api/core/v1"
2827
"k8s.io/apimachinery/pkg/api/meta"
@@ -486,7 +485,7 @@ func defaultOpts(config *rest.Config, opts Options) (Options, error) {
486485
}
487486

488487
if opts.NewInformerFunc == nil {
489-
opts.NewInformerFunc = informers.NewSharedIndexInformer
488+
opts.NewInformerFunc = toolscache.NewSharedIndexInformer
490489
}
491490
return opts, nil
492491
}

pkg/cache/cache_test.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"strings"
2727
"time"
2828

29-
kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
3029
. "github.com/onsi/ginkgo/v2"
3130
. "github.com/onsi/gomega"
3231
corev1 "k8s.io/api/core/v1"
@@ -545,14 +544,9 @@ func NonBlockingGetTest(createCacheFunc func(config *rest.Config, opts cache.Opt
545544
Expect(err).NotTo(HaveOccurred())
546545

547546
By("creating the informer cache")
548-
v := reflect.ValueOf(&opts).Elem()
549-
newInformerField := v.FieldByName("NewInformerFunc")
550-
newFakeInformer := func(_ kcache.ListerWatcher, _ runtime.Object, _ time.Duration, _ kcache.Indexers) kcpcache.ScopeableSharedIndexInformer {
547+
opts.NewInformerFunc = func(_ kcache.ListerWatcher, _ runtime.Object, _ time.Duration, _ kcache.Indexers) kcache.SharedIndexInformer {
551548
return &controllertest.FakeInformer{Synced: false}
552549
}
553-
reflect.NewAt(newInformerField.Type(), newInformerField.Addr().UnsafePointer()).
554-
Elem().
555-
Set(reflect.ValueOf(newFakeInformer))
556550
informerCache, err = createCacheFunc(cfg, opts)
557551
Expect(err).NotTo(HaveOccurred())
558552
By("running the cache and waiting for it to sync")

pkg/cache/internal/informers.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ import (
2424
"sync"
2525
"time"
2626

27-
kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
28-
"github.com/kcp-dev/apimachinery/v2/third_party/informers"
2927
apierrors "k8s.io/apimachinery/pkg/api/errors"
3028
"k8s.io/apimachinery/pkg/api/meta"
3129
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -48,7 +46,7 @@ type InformersOpts struct {
4846
Mapper meta.RESTMapper
4947
ResyncPeriod time.Duration
5048
Namespace string
51-
NewInformer func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) kcpcache.ScopeableSharedIndexInformer
49+
NewInformer func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer
5250
Selector Selector
5351
Transform cache.TransformFunc
5452
UnsafeDisableDeepCopy bool
@@ -57,7 +55,7 @@ type InformersOpts struct {
5755

5856
// NewInformers creates a new InformersMap that can create informers under the hood.
5957
func NewInformers(config *rest.Config, options *InformersOpts) *Informers {
60-
newInformer := informers.NewSharedIndexInformer
58+
newInformer := cache.NewSharedIndexInformer
6159
if options.NewInformer != nil {
6260
newInformer = options.NewInformer
6361
}
@@ -177,7 +175,7 @@ type Informers struct {
177175
unsafeDisableDeepCopy bool
178176

179177
// NewInformer allows overriding of the shared index informer constructor for testing.
180-
newInformer func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) kcpcache.ScopeableSharedIndexInformer
178+
newInformer func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer
181179

182180
// WatchErrorHandler allows the shared index informer's
183181
// watchErrorHandler to be set by overriding the options

pkg/cache/kcp_test.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cache_test
18+
19+
import (
20+
"context"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
corev1 "k8s.io/api/core/v1"
25+
"sigs.k8s.io/controller-runtime/pkg/cache"
26+
"sigs.k8s.io/controller-runtime/pkg/client"
27+
)
28+
29+
var _ = Describe("KCP cluster-unaware informer cache", func() {
30+
// Test whether we can have a cluster-unaware informer cache against a single workspace.
31+
// I.e. every object has a kcp.io/cluster annotation, but it should not be taken
32+
// into consideration by the cache to compute the key.
33+
It("should be able to get the default namespace despite kcp.io/cluster annotation", func() {
34+
ctx, cancel := context.WithCancel(context.Background())
35+
defer cancel()
36+
37+
c, err := cache.New(cfg, cache.Options{})
38+
Expect(err).NotTo(HaveOccurred())
39+
40+
By("Annotating the default namespace with kcp.io/cluster")
41+
cl, err := client.New(cfg, client.Options{})
42+
Expect(err).NotTo(HaveOccurred())
43+
ns := &corev1.Namespace{}
44+
err = cl.Get(ctx, client.ObjectKey{Name: "default"}, ns)
45+
Expect(err).NotTo(HaveOccurred())
46+
ns.Annotations = map[string]string{"kcp.io/cluster": "cluster1"}
47+
err = cl.Update(ctx, ns)
48+
Expect(err).NotTo(HaveOccurred())
49+
50+
go c.Start(ctx) //nolint:errcheck // Start is blocking, and error not relevant here.
51+
c.WaitForCacheSync(ctx)
52+
53+
By("By getting the default namespace with the informer")
54+
err = c.Get(ctx, client.ObjectKey{Name: "default"}, ns)
55+
Expect(err).NotTo(HaveOccurred())
56+
})
57+
})
58+
59+
// TODO: get envtest in place with kcp
60+
/*
61+
var _ = Describe("KCP cluster-aware informer cache", func() {
62+
It("should be able to get the default namespace with kcp.io/cluster annotation", func() {
63+
ctx, cancel := context.WithCancel(context.Background())
64+
defer cancel()
65+
66+
c, err := kcp.NewClusterAwareCache(cfg, cache.Options{})
67+
Expect(err).NotTo(HaveOccurred())
68+
69+
By("Annotating the default namespace with kcp.io/cluster")
70+
cl, err := client.New(cfg, client.Options{})
71+
Expect(err).NotTo(HaveOccurred())
72+
ns := &corev1.Namespace{}
73+
err = cl.Get(ctx, client.ObjectKey{Name: "default"}, ns)
74+
Expect(err).NotTo(HaveOccurred())
75+
ns.Annotations = map[string]string{"kcp.io/cluster": "cluster1"}
76+
err = cl.Update(ctx, ns)
77+
Expect(err).NotTo(HaveOccurred())
78+
79+
go c.Start(ctx) //nolint:errcheck // Start is blocking, and error not relevant here.
80+
c.WaitForCacheSync(ctx)
81+
82+
By("By getting the default namespace with the informer, but cluster-less key should fail")
83+
err = c.Get(ctx, client.ObjectKey{Name: "default"}, ns)
84+
Expect(err).To(HaveOccurred())
85+
86+
By("By getting the default namespace with the informer, but cluster-aware key should succeed")
87+
err = c.Get(kontext.WithCluster(ctx, "cluster1"), client.ObjectKey{Name: "default", Namespace: "cluster1"}, ns)
88+
})
89+
})
90+
*/

pkg/client/interfaces.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"time"
2222

23-
kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
2423
apierrors "k8s.io/apimachinery/pkg/api/errors"
2524
"k8s.io/apimachinery/pkg/runtime/schema"
2625
"k8s.io/client-go/tools/cache"
@@ -49,7 +48,7 @@ type Patch interface {
4948

5049
// NewInformerFunc describes a function that creates SharedIndexInformers.
5150
// Its signature matches cache.NewSharedIndexInformer from client-go.
52-
type NewInformerFunc func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) kcpcache.ScopeableSharedIndexInformer
51+
type NewInformerFunc func(cache.ListerWatcher, runtime.Object, time.Duration, cache.Indexers) cache.SharedIndexInformer
5352

5453
// TODO(directxman12): is there a sane way to deal with get/delete options?
5554

pkg/controller/controllertest/util.go

-8
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@ package controllertest
1919
import (
2020
"time"
2121

22-
kcpcache "github.com/kcp-dev/apimachinery/v2/pkg/cache"
23-
"github.com/kcp-dev/logicalcluster/v3"
2422
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2523
"k8s.io/client-go/tools/cache"
2624
)
2725

2826
var _ cache.SharedIndexInformer = &FakeInformer{}
29-
var _ kcpcache.ScopeableSharedIndexInformer = &FakeInformer{}
3027

3128
// FakeInformer provides fake Informer functionality for testing.
3229
type FakeInformer struct {
@@ -81,11 +78,6 @@ func (e eventHandlerWrapper) OnDelete(obj interface{}) {
8178
e.handler.(legacyResourceEventHandler).OnDelete(obj)
8279
}
8380

84-
// Cluster returns the fake Informer.
85-
func (f *FakeInformer) Cluster(clusterName logicalcluster.Name) cache.SharedIndexInformer {
86-
return f
87-
}
88-
8981
// AddIndexers does nothing. TODO(community): Implement this.
9082
func (f *FakeInformer) AddIndexers(indexers cache.Indexers) error {
9183
return nil

pkg/kcp/wrappers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func NewClusterAwareCache(config *rest.Config, opts cache.Options) (cache.Cache,
7070
c := rest.CopyConfig(config)
7171
c.Host += "/clusters/*"
7272

73-
opts.NewInformerFunc = func(lw k8scache.ListerWatcher, obj runtime.Object, syncPeriod time.Duration, indexers k8scache.Indexers) kcpcache.ScopeableSharedIndexInformer {
73+
opts.NewInformerFunc = func(lw k8scache.ListerWatcher, obj runtime.Object, syncPeriod time.Duration, indexers k8scache.Indexers) k8scache.SharedIndexInformer {
7474
indexers[kcpcache.ClusterIndexName] = kcpcache.ClusterIndexFunc
7575
indexers[kcpcache.ClusterAndNamespaceIndexName] = kcpcache.ClusterAndNamespaceIndexFunc
7676

0 commit comments

Comments
 (0)