Skip to content

Commit 72a4fae

Browse files
committed
Use local store for agent client
Signed-off-by: Jian Qiu <jqiu@redhat.com>
1 parent 105041a commit 72a4fae

13 files changed

Lines changed: 68 additions & 151 deletions

File tree

pkg/cloudevents/clients/addon/client_test.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@ package addon
33
import (
44
"context"
55
"encoding/json"
6-
"testing"
7-
"time"
8-
96
jsonpatch "github.com/evanphx/json-patch/v5"
7+
"testing"
108

119
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1210
"k8s.io/apimachinery/pkg/types"
1311

1412
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
15-
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"
16-
1713
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/statushash"
1814
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/store"
1915
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/clients"
@@ -82,13 +78,9 @@ func TestPatch(t *testing.T) {
8278
NewManagedClusterAddOnClient(ceClient, watcherStore),
8379
}}
8480

85-
addonInformerFactory := addoninformers.NewSharedInformerFactory(addonClientSet, time.Minute*10)
86-
informer := addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns().Informer()
87-
store := informer.GetStore()
88-
if err := store.Add(c.addon); err != nil {
81+
if err := watcherStore.Store.Add(c.addon); err != nil {
8982
t.Error(err)
9083
}
91-
watcherStore.SetInformer(informer)
9284

9385
if _, err = addonClientSet.AddonV1alpha1().ManagedClusterAddOns(c.clusterName).Patch(
9486
ctx,

pkg/cloudevents/clients/cluster/client_test.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,11 @@ func TestCreate(t *testing.T) {
7070
ManagedClusterClient: NewManagedClusterClient(ceClient, watcherStore, "cluster1"),
7171
}}
7272
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClientSet, time.Minute*10)
73-
clusterInformer := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer()
74-
clusterInformerStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()
7573
for _, cluster := range c.clusters {
76-
if err := clusterInformerStore.Add(cluster); err != nil {
74+
if err := watcherStore.Store.Add(cluster); err != nil {
7775
t.Error(err)
7876
}
7977
}
80-
watcherStore.SetInformer(clusterInformer)
8178
go clusterInformerFactory.Start(ctx.Done())
8279

8380
if _, err = clusterClientSet.ClusterV1().ManagedClusters().Create(ctx, c.newCluster, metav1.CreateOptions{}); err != nil {
@@ -129,12 +126,9 @@ func TestPatch(t *testing.T) {
129126
ManagedClusterClient: NewManagedClusterClient(ceClient, watcherStore, "cluster1"),
130127
}}
131128
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClientSet, time.Minute*10)
132-
clusterInformer := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer()
133-
clusterInformerStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()
134-
if err := clusterInformerStore.Add(c.cluster); err != nil {
129+
if err := watcherStore.Store.Add(c.cluster); err != nil {
135130
t.Error(err)
136131
}
137-
watcherStore.SetInformer(clusterInformer)
138132
go clusterInformerFactory.Start(ctx.Done())
139133

140134
oldData, err := json.Marshal(c.cluster)

pkg/cloudevents/clients/csr/client_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,11 @@ func TestCreate(t *testing.T) {
7474
csrInformer := cache.NewSharedIndexInformer(
7575
csrClient, &certificatev1.CertificateSigningRequest{}, 30*time.Second,
7676
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
77-
csrInformerStore := csrInformer.GetStore()
7877
for _, csr := range c.csrs {
79-
if err := csrInformerStore.Add(csr); err != nil {
78+
if err := watcherStore.Store.Add(csr); err != nil {
8079
t.Error(err)
8180
}
8281
}
83-
watcherStore.SetInformer(csrInformer)
8482
go csrInformer.Run(ctx.Done())
8583

8684
if _, err := csrClient.Create(ctx, c.csr, metav1.CreateOptions{}); err != nil {

pkg/cloudevents/clients/csr/clientholder.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package csr
22

33
import (
44
"context"
5-
"fmt"
65
"time"
76

87
certificatev1 "k8s.io/api/certificates/v1"
98
"k8s.io/client-go/tools/cache"
109

1110
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/options"
12-
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/store"
1311
)
1412

1513
// ClientHolder holds a client that implements list/watch for CSR and a SharedIndexInformer for CSR
@@ -39,11 +37,5 @@ func NewAgentClientHolder(ctx context.Context, opt *options.GenericClientOptions
3937
csrClient, &certificatev1.CertificateSigningRequest{}, 30*time.Second,
4038
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
4139

42-
agentStore, ok := opt.WatcherStore().(*store.AgentInformerWatcherStore[*certificatev1.CertificateSigningRequest])
43-
if !ok {
44-
return nil, fmt.Errorf("watcher store must be of type AgentInformerWatcherStore")
45-
}
46-
agentStore.SetInformer(csrInformer)
47-
4840
return &ClientHolder{client: csrClient, informer: csrInformer}, nil
4941
}

pkg/cloudevents/clients/store/informer.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ import (
1818

1919
// AgentInformerWatcherStore extends the BaseClientWatchStore.
2020

21-
// It gets/lists the resources from the given informer store and send
21+
// It gets/lists the resources from the given local store and send
2222
// the resource add/update/delete event to the watch channel directly.
2323
//
2424
// It is used for building resource agent client.
2525
type AgentInformerWatcherStore[T generic.ResourceObject] struct {
2626
BaseClientWatchStore[T]
2727
Watcher *Watcher
28-
29-
informer cache.SharedIndexInformer
3028
}
3129

3230
func NewAgentInformerWatcherStore[T generic.ResourceObject]() *AgentInformerWatcherStore[T] {
3331
return &AgentInformerWatcherStore[T]{
34-
BaseClientWatchStore: BaseClientWatchStore[T]{},
35-
Watcher: NewWatcher(),
32+
BaseClientWatchStore: BaseClientWatchStore[T]{
33+
Store: cache.NewStore(cache.MetaNamespaceKeyFunc),
34+
},
35+
Watcher: NewWatcher(),
3636
}
3737
}
3838

@@ -127,11 +127,5 @@ func (s *AgentInformerWatcherStore[T]) GetWatcher(namespace string, opts metav1.
127127
}
128128

129129
func (s *AgentInformerWatcherStore[T]) HasInitiated() bool {
130-
return s.Initiated && s.informer.HasSynced()
131-
}
132-
133-
func (s *AgentInformerWatcherStore[T]) SetInformer(informer cache.SharedIndexInformer) {
134-
s.informer = informer
135-
s.Store = informer.GetStore()
136-
s.Initiated = true
130+
return true
137131
}

pkg/cloudevents/clients/store/informer_test.go

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,17 @@ import (
99
"github.com/stretchr/testify/require"
1010
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1111
"k8s.io/apimachinery/pkg/watch"
12-
clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
13-
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
1412
clusterv1 "open-cluster-management.io/api/cluster/v1"
1513
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types"
1614
)
1715

1816
func TestGet(t *testing.T) {
19-
clusterClient := clusterfake.NewSimpleClientset()
20-
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClient, time.Minute*10)
21-
clusterStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()
22-
2317
watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()
24-
watchStore.SetInformer(clusterInformerFactory.Cluster().V1().ManagedClusters().Informer())
2518

26-
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test1"}}); err != nil {
19+
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test1"}}); err != nil {
2720
t.Error(err)
2821
}
29-
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test2"}}); err != nil {
22+
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test2"}}); err != nil {
3023
t.Error(err)
3124
}
3225

@@ -66,30 +59,26 @@ func TestGet(t *testing.T) {
6659
}
6760

6861
func TestList(t *testing.T) {
69-
clusterClient := clusterfake.NewSimpleClientset()
70-
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClient, time.Minute*10)
71-
clusterStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()
72-
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
62+
watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()
63+
64+
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
7365
Name: "test1",
7466
Labels: map[string]string{"test": "true"},
7567
}}); err != nil {
7668
t.Error(err)
7769
}
78-
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
70+
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
7971
Name: "test2",
8072
Labels: map[string]string{"test": "true"},
8173
}}); err != nil {
8274
t.Error(err)
8375
}
84-
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
76+
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
8577
Name: "test3",
8678
}}); err != nil {
8779
t.Error(err)
8880
}
8981

90-
watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()
91-
watchStore.SetInformer(clusterInformerFactory.Cluster().V1().ManagedClusters().Informer())
92-
9382
clusters, err := watchStore.List("", metav1.ListOptions{LabelSelector: "test=true"})
9483
if err != nil {
9584
t.Error(err)
@@ -142,18 +131,14 @@ func (r *receiveResult) result() bool {
142131
}
143132

144133
func TestWatch(t *testing.T) {
145-
clusterClient := clusterfake.NewSimpleClientset()
146-
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClient, time.Minute*10)
147-
clusterStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()
148-
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test1"}}); err != nil {
134+
watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()
135+
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test1"}}); err != nil {
149136
t.Error(err)
150137
}
151-
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test2"}}); err != nil {
138+
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test2"}}); err != nil {
152139
t.Error(err)
153140
}
154141

155-
watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()
156-
watchStore.SetInformer(clusterInformerFactory.Cluster().V1().ManagedClusters().Informer())
157142
watcher, err := watchStore.GetWatcher("", metav1.ListOptions{})
158143
if err != nil {
159144
t.Error(err)

pkg/cloudevents/clients/store/lister_test.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,25 @@
11
package store
22

33
import (
4-
"testing"
5-
"time"
6-
74
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8-
clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
9-
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
10-
workfake "open-cluster-management.io/api/client/work/clientset/versioned/fake"
11-
workinformers "open-cluster-management.io/api/client/work/informers/externalversions"
125
clusterv1 "open-cluster-management.io/api/cluster/v1"
136
workv1 "open-cluster-management.io/api/work/v1"
7+
"testing"
148

159
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/common"
1610
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types"
1711
)
1812

1913
func TestAgentLister(t *testing.T) {
20-
clusterClient := clusterfake.NewSimpleClientset()
21-
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClient, time.Minute*10)
22-
clusterStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()
23-
2414
watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()
25-
watchStore.SetInformer(clusterInformerFactory.Cluster().V1().ManagedClusters().Informer())
2615

27-
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
16+
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
2817
Name: "test1",
2918
Labels: map[string]string{common.CloudEventsOriginalSourceLabelKey: "source1"},
3019
}}); err != nil {
3120
t.Error(err)
3221
}
33-
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
22+
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
3423
Name: "test2",
3524
Labels: map[string]string{common.CloudEventsOriginalSourceLabelKey: "source1"},
3625
}}); err != nil {
@@ -48,20 +37,15 @@ func TestAgentLister(t *testing.T) {
4837
}
4938

5039
func TestSourceLister(t *testing.T) {
51-
workClient := workfake.NewSimpleClientset()
52-
workInformerFactory := workinformers.NewSharedInformerFactory(workClient, time.Minute*10)
53-
workStore := workInformerFactory.Work().V1().ManifestWorks().Informer().GetStore()
54-
5540
watchStore := NewAgentInformerWatcherStore[*workv1.ManifestWork]()
56-
watchStore.SetInformer(workInformerFactory.Work().V1().ManifestWorks().Informer())
5741

58-
if err := workStore.Add(&workv1.ManifestWork{ObjectMeta: metav1.ObjectMeta{
42+
if err := watchStore.Store.Add(&workv1.ManifestWork{ObjectMeta: metav1.ObjectMeta{
5943
Name: "test1",
6044
Namespace: "cluster1",
6145
}}); err != nil {
6246
t.Error(err)
6347
}
64-
if err := workStore.Add(&workv1.ManifestWork{ObjectMeta: metav1.ObjectMeta{
48+
if err := watchStore.Store.Add(&workv1.ManifestWork{ObjectMeta: metav1.ObjectMeta{
6549
Name: "test2",
6650
Namespace: "cluster2",
6751
}}); err != nil {

0 commit comments

Comments
 (0)