Skip to content

Commit 7b3fc93

Browse files
committed
remove ce sdk-go deps -v2
Signed-off-by: Wei Liu <liuweixa@redhat.com>
1 parent 33a7021 commit 7b3fc93

18 files changed

Lines changed: 1663 additions & 213 deletions

File tree

pkg/cloudevents/clients/options/generic.go

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type GenericClientOptions[T generic.ResourceObject] struct {
2121
clusterName string
2222
subscription bool
2323
resync bool
24+
v2 bool
2425
}
2526

2627
// NewGenericClientOptions create a GenericClientOptions
@@ -86,6 +87,11 @@ func (o *GenericClientOptions[T]) WithResyncEnabled(resync bool) *GenericClientO
8687
return o
8788
}
8889

90+
func (o *GenericClientOptions[T]) V2(enabled bool) *GenericClientOptions[T] {
91+
o.v2 = enabled
92+
return o
93+
}
94+
8995
func (o *GenericClientOptions[T]) ClusterName() string {
9096
return o.clusterName
9197
}
@@ -111,20 +117,39 @@ func (o *GenericClientOptions[T]) AgentClient(ctx context.Context) (*generic.Clo
111117
o.watcherStore = store.NewAgentInformerWatcherStore[T]()
112118
}
113119

114-
options, err := generic.BuildCloudEventsAgentOptions(o.config, o.clusterName, o.clientID)
115-
if err != nil {
116-
return nil, err
117-
}
120+
var cloudEventsClient *generic.CloudEventAgentClient[T]
121+
if o.v2 {
122+
options, err := generic.BuildCloudEventsAgentOptionsV2(o.config, o.clusterName, o.clientID)
123+
if err != nil {
124+
return nil, err
125+
}
118126

119-
cloudEventsClient, err := generic.NewCloudEventAgentClient(
120-
ctx,
121-
options,
122-
store.NewAgentWatcherStoreLister(o.watcherStore),
123-
statushash.StatusHash,
124-
o.codec,
125-
)
126-
if err != nil {
127-
return nil, err
127+
cloudEventsClient, err = generic.NewCloudEventAgentClientV2(
128+
ctx,
129+
options,
130+
store.NewAgentWatcherStoreLister(o.watcherStore),
131+
statushash.StatusHash,
132+
o.codec,
133+
)
134+
if err != nil {
135+
return nil, err
136+
}
137+
} else {
138+
options, err := generic.BuildCloudEventsAgentOptions(o.config, o.clusterName, o.clientID)
139+
if err != nil {
140+
return nil, err
141+
}
142+
143+
cloudEventsClient, err = generic.NewCloudEventAgentClient(
144+
ctx,
145+
options,
146+
store.NewAgentWatcherStoreLister(o.watcherStore),
147+
statushash.StatusHash,
148+
o.codec,
149+
)
150+
if err != nil {
151+
return nil, err
152+
}
128153
}
129154

130155
if o.subscription {

pkg/cloudevents/generic/agentclient.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ import (
1414
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/options"
1515
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/payload"
1616
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types"
17+
optionsv2 "open-cluster-management.io/sdk-go/pkg/cloudevents/generic/v2/options"
1718
)
1819

1920
// CloudEventAgentClient is a client for an agent to resync/send/receive its resources with cloud events.
2021
//
2122
// An agent is a component that handles the deployment of requested resources on the managed cluster and status report
2223
// to the source.
2324
type CloudEventAgentClient[T ResourceObject] struct {
24-
*baseClient
25+
baseClientInterface
2526
lister Lister[T]
2627
codec Codec[T]
2728
statusHashGetter StatusHashGetter[T]
@@ -56,19 +57,48 @@ func NewCloudEventAgentClient[T ResourceObject](
5657
}
5758

5859
return &CloudEventAgentClient[T]{
59-
baseClient: baseClient,
60-
lister: lister,
61-
codec: codec,
62-
statusHashGetter: statusHashGetter,
63-
agentID: agentOptions.AgentID,
64-
clusterName: agentOptions.ClusterName,
60+
baseClientInterface: baseClient,
61+
lister: lister,
62+
codec: codec,
63+
statusHashGetter: statusHashGetter,
64+
agentID: agentOptions.AgentID,
65+
clusterName: agentOptions.ClusterName,
66+
}, nil
67+
}
68+
69+
func NewCloudEventAgentClientV2[T ResourceObject](
70+
ctx context.Context,
71+
agentOptions *optionsv2.CloudEventsAgentOptions,
72+
lister Lister[T],
73+
statusHashGetter StatusHashGetter[T],
74+
codec Codec[T],
75+
) (*CloudEventAgentClient[T], error) {
76+
baseClient := &baseClientV2{
77+
clientID: agentOptions.AgentID,
78+
transport: agentOptions.EventTransport,
79+
// TODO move the ratelimiter to a package
80+
cloudEventsRateLimiter: NewRateLimiter(options.EventRateLimit{}),
81+
reconnectedChan: make(chan struct{}),
82+
}
83+
84+
if err := baseClient.connect(ctx); err != nil {
85+
return nil, err
86+
}
87+
88+
return &CloudEventAgentClient[T]{
89+
baseClientInterface: baseClient,
90+
lister: lister,
91+
codec: codec,
92+
statusHashGetter: statusHashGetter,
93+
agentID: agentOptions.AgentID,
94+
clusterName: agentOptions.ClusterName,
6595
}, nil
6696
}
6797

6898
// ReconnectedChan returns a chan which indicates the source/agent client is reconnected.
6999
// The source/agent client callers should consider sending a resync request when receiving this signal.
70100
func (c *CloudEventAgentClient[T]) ReconnectedChan() <-chan struct{} {
71-
return c.reconnectedChan
101+
return c.getReconnectedChan()
72102
}
73103

74104
// Resync the resources spec by sending a spec resync request from the current to the given source.

pkg/cloudevents/generic/agentclient_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ func TestAgentResync(t *testing.T) {
6868
stop := make(chan bool)
6969

7070
go func() {
71-
err = agent.cloudEventsClient.StartReceiver(ctx, func(event cloudevents.Event) {
71+
baseClient := agent.baseClientInterface.(*baseClient)
72+
err = baseClient.cloudEventsClient.StartReceiver(ctx, func(event cloudevents.Event) {
7273
eventChan <- receiveEvent{event: event}
7374
})
7475
if err != nil {
@@ -136,7 +137,8 @@ func TestAgentPublish(t *testing.T) {
136137
eventChan := make(chan receiveEvent)
137138
stop := make(chan bool)
138139
go func() {
139-
err = agent.cloudEventsClient.StartReceiver(ctx, func(event cloudevents.Event) {
140+
baseClient := agent.baseClientInterface.(*baseClient)
141+
err = baseClient.cloudEventsClient.StartReceiver(ctx, func(event cloudevents.Event) {
140142
eventChan <- receiveEvent{event: event}
141143
})
142144
if err != nil {
@@ -292,7 +294,8 @@ func TestStatusResyncResponse(t *testing.T) {
292294
mutex := &sync.Mutex{}
293295

294296
go func() {
295-
_ = agent.cloudEventsClient.StartReceiver(ctx, func(event cloudevents.Event) {
297+
baseClient := agent.baseClientInterface.(*baseClient)
298+
_ = baseClient.cloudEventsClient.StartReceiver(ctx, func(event cloudevents.Event) {
296299
mutex.Lock()
297300
defer mutex.Unlock()
298301
receivedEvents = append(receivedEvents, event)

pkg/cloudevents/generic/baseclient.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ type baseClient struct {
4747
dataType types.CloudEventsDataType
4848
}
4949

50+
func (c *baseClient) getReconnectedChan() <-chan struct{} {
51+
return c.reconnectedChan
52+
}
53+
5054
func (c *baseClient) connect(ctx context.Context) error {
5155
logger := klog.FromContext(ctx)
5256

0 commit comments

Comments
 (0)