Skip to content

Commit 52bbcdc

Browse files
skeeeyqiujian16
authored andcommitted
cloudevent clients
Signed-off-by: Wei Liu <[email protected]>
1 parent 9fada84 commit 52bbcdc

File tree

6 files changed

+21
-32
lines changed

6 files changed

+21
-32
lines changed

pkg/cloudevents/clients/addon/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7-
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
87
"strconv"
98
"sync"
109

10+
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
11+
1112
"k8s.io/apimachinery/pkg/api/errors"
1213
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
kubetypes "k8s.io/apimachinery/pkg/types"
@@ -52,7 +53,6 @@ func (c *ManagedClusterAddOnClient) Namespace(name string) *ManagedClusterAddOnC
5253

5354
func (c *ManagedClusterAddOnClient) Create(
5455
ctx context.Context, addon *addonapiv1alpha1.ManagedClusterAddOn, opts metav1.CreateOptions) (*addonapiv1alpha1.ManagedClusterAddOn, error) {
55-
5656
return nil, errors.NewMethodNotSupported(common.ManagedClusterGR, "update")
5757

5858
}
@@ -148,7 +148,7 @@ func (c *ManagedClusterAddOnClient) Patch(
148148
// publish the status update event to source, source will check the resource version
149149
// and reject the update if it's status update is outdated.
150150
if err := c.cloudEventsClient.Publish(ctx, eventType, newAddon); err != nil {
151-
return nil, cloudeventserrors.NewPublishError(common.ManagedClusterGR, name, err)
151+
return nil, cloudeventserrors.ToStatusError(common.ManagedClusterGR, name, err)
152152
}
153153

154154
// Fetch the latest cluster from the store and verify the resource version to avoid updating the store

pkg/cloudevents/clients/csr/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"k8s.io/apimachinery/pkg/watch"
1515
"k8s.io/klog/v2"
1616

17-
utilrand "k8s.io/apimachinery/pkg/util/rand"
1817
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/common"
1918
cloudeventserrors "open-cluster-management.io/sdk-go/pkg/cloudevents/clients/errors"
2019
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/store"

pkg/cloudevents/clients/event/client.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package event
22

33
import (
44
"context"
5+
56
eventv1 "k8s.io/api/events/v1"
67
"k8s.io/apimachinery/pkg/api/errors"
78
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -10,6 +11,7 @@ import (
1011
applyconfigurationseventsv1 "k8s.io/client-go/applyconfigurations/events/v1"
1112
eventv1client "k8s.io/client-go/kubernetes/typed/events/v1"
1213
"k8s.io/klog/v2"
14+
1315
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/common"
1416
cloudeventserrors "open-cluster-management.io/sdk-go/pkg/cloudevents/clients/errors"
1517
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/store"
@@ -49,10 +51,8 @@ func (e EventClient) Create(ctx context.Context, event *eventv1.Event, opts meta
4951
Action: common.CreateRequestAction,
5052
}
5153

52-
// TODO: validate the csr
53-
5454
if err := e.cloudEventsClient.Publish(ctx, eventType, event); err != nil {
55-
return nil, cloudeventserrors.NewPublishError(common.CSRGR, event.Name, err)
55+
return nil, cloudeventserrors.ToStatusError(common.CSRGR, event.Name, err)
5656
}
5757

5858
// add the new csr to the local cache.
@@ -64,42 +64,34 @@ func (e EventClient) Create(ctx context.Context, event *eventv1.Event, opts meta
6464
}
6565

6666
func (e EventClient) Update(ctx context.Context, event *eventv1.Event, opts metav1.UpdateOptions) (*eventv1.Event, error) {
67-
//TODO implement me
6867
return nil, errors.NewMethodNotSupported(eventv1.Resource("events"), "update")
6968
}
7069

7170
func (e EventClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
72-
//TODO implement me
7371
return errors.NewMethodNotSupported(eventv1.Resource("events"), "delete")
7472
}
7573

7674
func (e EventClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
77-
//TODO implement me
7875
return errors.NewMethodNotSupported(eventv1.Resource("events"), "deletecollection")
7976
}
8077

8178
func (e EventClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*eventv1.Event, error) {
82-
//TODO implement me
8379
return nil, errors.NewMethodNotSupported(eventv1.Resource("events"), "get")
8480
}
8581

8682
func (e EventClient) List(ctx context.Context, opts metav1.ListOptions) (*eventv1.EventList, error) {
87-
//TODO implement me
8883
return nil, errors.NewMethodNotSupported(eventv1.Resource("events"), "list")
8984
}
9085

9186
func (e EventClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
92-
//TODO implement me
9387
return nil, errors.NewMethodNotSupported(eventv1.Resource("events"), "watch")
9488
}
9589

9690
func (e EventClient) Patch(ctx context.Context, name string, pt kubetypes.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *eventv1.Event, err error) {
97-
//TODO implement me
9891
return nil, errors.NewMethodNotSupported(eventv1.Resource("events"), "patch")
9992
}
10093

10194
func (e EventClient) Apply(ctx context.Context, event *applyconfigurationseventsv1.EventApplyConfiguration, opts metav1.ApplyOptions) (result *eventv1.Event, err error) {
102-
//TODO implement me
10395
return nil, errors.NewMethodNotSupported(eventv1.Resource("events"), "apply")
10496
}
10597

pkg/cloudevents/clients/event/codec.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package event
22

33
import (
44
"fmt"
5+
56
cloudevents "github.com/cloudevents/sdk-go/v2"
67
eventsv1 "k8s.io/api/events/v1"
78
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -14,19 +15,19 @@ var EventEventDataType = types.CloudEventsDataType{
1415
Resource: "events",
1516
}
1617

17-
// ManagedClusterCodec is a codec to encode/decode a ManagedCluster/cloudevent for an agent.
18+
// EventCodec is a codec to encode/decode a event/cloudevent for an agent.
1819
type EventCodec struct{}
1920

2021
func NewEventCodec() *EventCodec {
2122
return &EventCodec{}
2223
}
2324

24-
// EventDataType always returns the event data type `io.open-cluster-management.cluster.v1.managedclusters`.
25+
// EventDataType always returns the event data type `io.k8s.events.v1.events`.
2526
func (c *EventCodec) EventDataType() types.CloudEventsDataType {
2627
return EventEventDataType
2728
}
2829

29-
// Encode the ManagedCluster to a cloudevent
30+
// Encode the event to a cloudevent
3031
func (c *EventCodec) Encode(source string, eventType types.CloudEventsType, event *eventsv1.Event) (*cloudevents.Event, error) {
3132
if eventType.CloudEventsDataType != EventEventDataType {
3233
return nil, fmt.Errorf("unsupported cloudevents data type %s", eventType.CloudEventsDataType)
@@ -54,7 +55,7 @@ func (c *EventCodec) Encode(source string, eventType types.CloudEventsType, even
5455
return &evt, nil
5556
}
5657

57-
// Decode a cloudevent to a ManagedCluster
58+
// Decode a cloudevent to an event object
5859
func (c *EventCodec) Decode(evt *cloudevents.Event) (*eventsv1.Event, error) {
5960
event := &eventsv1.Event{}
6061
if err := evt.DataAs(event); err != nil {

pkg/cloudevents/clients/lease/client.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package lease
33
import (
44
"context"
55
"fmt"
6+
"strconv"
7+
68
coordinationv1 "k8s.io/api/coordination/v1"
79
"k8s.io/apimachinery/pkg/api/errors"
810
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -17,7 +19,6 @@ import (
1719
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/store"
1820
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic"
1921
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types"
20-
"strconv"
2122
)
2223

2324
type LeaseClient struct {
@@ -27,7 +28,6 @@ type LeaseClient struct {
2728
}
2829

2930
func (l LeaseClient) Create(ctx context.Context, lease *coordinationv1.Lease, opts metav1.CreateOptions) (*coordinationv1.Lease, error) {
30-
//TODO implement me
3131
return nil, errors.NewMethodNotSupported(coordinationv1.Resource("leases"), "create")
3232
}
3333

@@ -39,7 +39,7 @@ func (l LeaseClient) Update(ctx context.Context, lease *coordinationv1.Lease, op
3939
eventType.Action = common.UpdateRequestAction
4040

4141
if err := l.cloudEventsClient.Publish(ctx, eventType, lease); err != nil {
42-
return nil, cloudeventserrors.NewPublishError(coordinationv1.Resource("leases"), lease.Name, err)
42+
return nil, cloudeventserrors.ToStatusError(coordinationv1.Resource("leases"), lease.Name, err)
4343
}
4444

4545
// Fetch the latest cluster from the store and verify the resource version to avoid updating the store
@@ -75,12 +75,10 @@ func (l LeaseClient) Update(ctx context.Context, lease *coordinationv1.Lease, op
7575
}
7676

7777
func (l LeaseClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
78-
//TODO implement me
7978
return errors.NewMethodNotSupported(coordinationv1.Resource("leases"), "delete")
8079
}
8180

8281
func (l LeaseClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
83-
//TODO implement me
8482
return errors.NewMethodNotSupported(coordinationv1.Resource("leases"), "deletecollection")
8583
}
8684

@@ -97,22 +95,18 @@ func (l LeaseClient) Get(ctx context.Context, name string, opts metav1.GetOption
9795
}
9896

9997
func (l LeaseClient) List(ctx context.Context, opts metav1.ListOptions) (*coordinationv1.LeaseList, error) {
100-
//TODO implement me
10198
return nil, errors.NewMethodNotSupported(coordinationv1.Resource("leases"), "list")
10299
}
103100

104101
func (l LeaseClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
105-
//TODO implement me
106102
return nil, errors.NewMethodNotSupported(coordinationv1.Resource("leases"), "watch")
107103
}
108104

109105
func (l LeaseClient) Patch(ctx context.Context, name string, pt kubetypes.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (result *coordinationv1.Lease, err error) {
110-
//TODO implement me
111106
return nil, errors.NewMethodNotSupported(coordinationv1.Resource("leases"), "patch")
112107
}
113108

114109
func (l LeaseClient) Apply(ctx context.Context, lease *v1.LeaseApplyConfiguration, opts metav1.ApplyOptions) (result *coordinationv1.Lease, err error) {
115-
//TODO implement me
116110
return nil, errors.NewMethodNotSupported(coordinationv1.Resource("leases"), "apply")
117111
}
118112

pkg/cloudevents/clients/lease/codec.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package lease
22

33
import (
44
"fmt"
5+
56
cloudevents "github.com/cloudevents/sdk-go/v2"
7+
68
coordinationv1 "k8s.io/api/coordination/v1"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
811
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types"
912
)
1013

@@ -14,19 +17,19 @@ var LeaseEventDataType = types.CloudEventsDataType{
1417
Resource: "leases",
1518
}
1619

17-
// ManagedClusterCodec is a codec to encode/decode a ManagedCluster/cloudevent for an agent.
20+
// LeaseCodec is a codec to encode/decode a lease/cloudevent for an agent.
1821
type LeaseCodec struct{}
1922

2023
func NewManagedClusterAddOnCodec() *LeaseCodec {
2124
return &LeaseCodec{}
2225
}
2326

24-
// EventDataType always returns the event data type `io.open-cluster-management.cluster.v1.managedclusters`.
27+
// EventDataType always returns the event data type `io.k8s.coordination.k8s.v1.leases`.
2528
func (c *LeaseCodec) EventDataType() types.CloudEventsDataType {
2629
return LeaseEventDataType
2730
}
2831

29-
// Encode the ManagedCluster to a cloudevent
32+
// Encode the lease to a cloudevent
3033
func (c *LeaseCodec) Encode(source string, eventType types.CloudEventsType, lease *coordinationv1.Lease) (*cloudevents.Event, error) {
3134
if eventType.CloudEventsDataType != LeaseEventDataType {
3235
return nil, fmt.Errorf("unsupported cloudevents data type %s", eventType.CloudEventsDataType)
@@ -54,7 +57,7 @@ func (c *LeaseCodec) Encode(source string, eventType types.CloudEventsType, leas
5457
return &evt, nil
5558
}
5659

57-
// Decode a cloudevent to a ManagedCluster
60+
// Decode a cloudevent to a lease object
5861
func (c *LeaseCodec) Decode(evt *cloudevents.Event) (*coordinationv1.Lease, error) {
5962
lease := &coordinationv1.Lease{}
6063
if err := evt.DataAs(lease); err != nil {

0 commit comments

Comments
 (0)