Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions pkg/cloudevents/clients/addon/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ package addon
import (
"context"
"encoding/json"
"testing"
"time"

jsonpatch "github.com/evanphx/json-patch/v5"
"testing"

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

addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
addoninformers "open-cluster-management.io/api/client/addon/informers/externalversions"

"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/statushash"
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/store"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/clients"
Expand Down Expand Up @@ -82,13 +78,9 @@ func TestPatch(t *testing.T) {
NewManagedClusterAddOnClient(ceClient, watcherStore),
}}

addonInformerFactory := addoninformers.NewSharedInformerFactory(addonClientSet, time.Minute*10)
informer := addonInformerFactory.Addon().V1alpha1().ManagedClusterAddOns().Informer()
store := informer.GetStore()
if err := store.Add(c.addon); err != nil {
if err := watcherStore.Store.Add(c.addon); err != nil {
t.Error(err)
}
watcherStore.SetInformer(informer)

if _, err = addonClientSet.AddonV1alpha1().ManagedClusterAddOns(c.clusterName).Patch(
ctx,
Expand Down
10 changes: 2 additions & 8 deletions pkg/cloudevents/clients/cluster/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,11 @@ func TestCreate(t *testing.T) {
ManagedClusterClient: NewManagedClusterClient(ceClient, watcherStore, "cluster1"),
}}
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClientSet, time.Minute*10)
clusterInformer := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer()
clusterInformerStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()
for _, cluster := range c.clusters {
if err := clusterInformerStore.Add(cluster); err != nil {
if err := watcherStore.Store.Add(cluster); err != nil {
t.Error(err)
}
}
watcherStore.SetInformer(clusterInformer)
go clusterInformerFactory.Start(ctx.Done())

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

oldData, err := json.Marshal(c.cluster)
Expand Down
4 changes: 1 addition & 3 deletions pkg/cloudevents/clients/csr/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,11 @@ func TestCreate(t *testing.T) {
csrInformer := cache.NewSharedIndexInformer(
csrClient, &certificatev1.CertificateSigningRequest{}, 30*time.Second,
cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc})
csrInformerStore := csrInformer.GetStore()
for _, csr := range c.csrs {
if err := csrInformerStore.Add(csr); err != nil {
if err := watcherStore.Store.Add(csr); err != nil {
t.Error(err)
}
}
watcherStore.SetInformer(csrInformer)
go csrInformer.Run(ctx.Done())

if _, err := csrClient.Create(ctx, c.csr, metav1.CreateOptions{}); err != nil {
Expand Down
8 changes: 0 additions & 8 deletions pkg/cloudevents/clients/csr/clientholder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package csr

import (
"context"
"fmt"
"time"

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

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

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

agentStore, ok := opt.WatcherStore().(*store.AgentInformerWatcherStore[*certificatev1.CertificateSigningRequest])
if !ok {
return nil, fmt.Errorf("watcher store must be of type AgentInformerWatcherStore")
}
agentStore.SetInformer(csrInformer)

return &ClientHolder{client: csrClient, informer: csrInformer}, nil
}
18 changes: 6 additions & 12 deletions pkg/cloudevents/clients/store/informer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ import (

// AgentInformerWatcherStore extends the BaseClientWatchStore.

// It gets/lists the resources from the given informer store and send
// It gets/lists the resources from the given local store and send
// the resource add/update/delete event to the watch channel directly.
//
// It is used for building resource agent client.
type AgentInformerWatcherStore[T generic.ResourceObject] struct {
BaseClientWatchStore[T]
Watcher *Watcher

informer cache.SharedIndexInformer
}

func NewAgentInformerWatcherStore[T generic.ResourceObject]() *AgentInformerWatcherStore[T] {
return &AgentInformerWatcherStore[T]{
BaseClientWatchStore: BaseClientWatchStore[T]{},
Watcher: NewWatcher(),
BaseClientWatchStore: BaseClientWatchStore[T]{
Store: cache.NewStore(cache.MetaNamespaceKeyFunc),
},
Watcher: NewWatcher(),
}
}

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

func (s *AgentInformerWatcherStore[T]) HasInitiated() bool {
return s.Initiated && s.informer.HasSynced()
}

func (s *AgentInformerWatcherStore[T]) SetInformer(informer cache.SharedIndexInformer) {
s.informer = informer
s.Store = informer.GetStore()
s.Initiated = true
return true
}
35 changes: 10 additions & 25 deletions pkg/cloudevents/clients/store/informer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,17 @@ import (
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
clusterv1 "open-cluster-management.io/api/cluster/v1"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types"
)

func TestGet(t *testing.T) {
clusterClient := clusterfake.NewSimpleClientset()
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClient, time.Minute*10)
clusterStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()

watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()
watchStore.SetInformer(clusterInformerFactory.Cluster().V1().ManagedClusters().Informer())

if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test1"}}); err != nil {
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test1"}}); err != nil {
t.Error(err)
}
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test2"}}); err != nil {
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test2"}}); err != nil {
t.Error(err)
}

Expand Down Expand Up @@ -66,30 +59,26 @@ func TestGet(t *testing.T) {
}

func TestList(t *testing.T) {
clusterClient := clusterfake.NewSimpleClientset()
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClient, time.Minute*10)
clusterStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()

if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
Name: "test1",
Labels: map[string]string{"test": "true"},
}}); err != nil {
t.Error(err)
}
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
Name: "test2",
Labels: map[string]string{"test": "true"},
}}); err != nil {
t.Error(err)
}
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
Name: "test3",
}}); err != nil {
t.Error(err)
}

watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()
watchStore.SetInformer(clusterInformerFactory.Cluster().V1().ManagedClusters().Informer())

clusters, err := watchStore.List("", metav1.ListOptions{LabelSelector: "test=true"})
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -142,18 +131,14 @@ func (r *receiveResult) result() bool {
}

func TestWatch(t *testing.T) {
clusterClient := clusterfake.NewSimpleClientset()
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClient, time.Minute*10)
clusterStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test1"}}); err != nil {
watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test1"}}); err != nil {
t.Error(err)
}
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test2"}}); err != nil {
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{Name: "test2"}}); err != nil {
t.Error(err)
}

watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()
watchStore.SetInformer(clusterInformerFactory.Cluster().V1().ManagedClusters().Informer())
watcher, err := watchStore.GetWatcher("", metav1.ListOptions{})
if err != nil {
t.Error(err)
Expand Down
26 changes: 5 additions & 21 deletions pkg/cloudevents/clients/store/lister_test.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
package store

import (
"testing"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
workfake "open-cluster-management.io/api/client/work/clientset/versioned/fake"
workinformers "open-cluster-management.io/api/client/work/informers/externalversions"
clusterv1 "open-cluster-management.io/api/cluster/v1"
workv1 "open-cluster-management.io/api/work/v1"
"testing"

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

func TestAgentLister(t *testing.T) {
clusterClient := clusterfake.NewSimpleClientset()
clusterInformerFactory := clusterinformers.NewSharedInformerFactory(clusterClient, time.Minute*10)
clusterStore := clusterInformerFactory.Cluster().V1().ManagedClusters().Informer().GetStore()

watchStore := NewAgentInformerWatcherStore[*clusterv1.ManagedCluster]()
watchStore.SetInformer(clusterInformerFactory.Cluster().V1().ManagedClusters().Informer())

if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
Name: "test1",
Labels: map[string]string{common.CloudEventsOriginalSourceLabelKey: "source1"},
}}); err != nil {
t.Error(err)
}
if err := clusterStore.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
if err := watchStore.Store.Add(&clusterv1.ManagedCluster{ObjectMeta: metav1.ObjectMeta{
Name: "test2",
Labels: map[string]string{common.CloudEventsOriginalSourceLabelKey: "source1"},
}}); err != nil {
Expand All @@ -48,20 +37,15 @@ func TestAgentLister(t *testing.T) {
}

func TestSourceLister(t *testing.T) {
workClient := workfake.NewSimpleClientset()
workInformerFactory := workinformers.NewSharedInformerFactory(workClient, time.Minute*10)
workStore := workInformerFactory.Work().V1().ManifestWorks().Informer().GetStore()

watchStore := NewAgentInformerWatcherStore[*workv1.ManifestWork]()
watchStore.SetInformer(workInformerFactory.Work().V1().ManifestWorks().Informer())

if err := workStore.Add(&workv1.ManifestWork{ObjectMeta: metav1.ObjectMeta{
if err := watchStore.Store.Add(&workv1.ManifestWork{ObjectMeta: metav1.ObjectMeta{
Name: "test1",
Namespace: "cluster1",
}}); err != nil {
t.Error(err)
}
if err := workStore.Add(&workv1.ManifestWork{ObjectMeta: metav1.ObjectMeta{
if err := watchStore.Store.Add(&workv1.ManifestWork{ObjectMeta: metav1.ObjectMeta{
Name: "test2",
Namespace: "cluster2",
}}); err != nil {
Expand Down
Loading
Loading