forked from open-cluster-management-io/sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
143 lines (116 loc) · 5.55 KB
/
Copy pathclient.go
File metadata and controls
143 lines (116 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package addon
import (
"context"
"net/http"
addonapiv1alpha1 "open-cluster-management.io/api/addon/v1alpha1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubetypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/klog/v2"
addonv1alpha1client "open-cluster-management.io/api/client/addon/clientset/versioned/typed/addon/v1alpha1"
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/common"
cloudeventserrors "open-cluster-management.io/sdk-go/pkg/cloudevents/clients/errors"
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/store"
"open-cluster-management.io/sdk-go/pkg/cloudevents/clients/utils"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic"
"open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types"
)
// ManagedClusterAddOnClient implements the ManagedClusterAddonInterface.
type ManagedClusterAddOnClient struct {
cloudEventsClient generic.CloudEventsClient[*addonapiv1alpha1.ManagedClusterAddOn]
watcherStore store.ClientWatcherStore[*addonapiv1alpha1.ManagedClusterAddOn]
namespace string
}
var _ addonv1alpha1client.ManagedClusterAddOnInterface = &ManagedClusterAddOnClient{}
func NewManagedClusterAddOnClient(
cloudEventsClient generic.CloudEventsClient[*addonapiv1alpha1.ManagedClusterAddOn],
watcherStore store.ClientWatcherStore[*addonapiv1alpha1.ManagedClusterAddOn],
) *ManagedClusterAddOnClient {
return &ManagedClusterAddOnClient{
cloudEventsClient: cloudEventsClient,
watcherStore: watcherStore,
}
}
func (c *ManagedClusterAddOnClient) Namespace(namespace string) *ManagedClusterAddOnClient {
c.namespace = namespace
return c
}
func (c *ManagedClusterAddOnClient) Create(
ctx context.Context, addon *addonapiv1alpha1.ManagedClusterAddOn, opts metav1.CreateOptions) (*addonapiv1alpha1.ManagedClusterAddOn, error) {
return nil, errors.NewMethodNotSupported(common.ManagedClusterAddOnGR, "create")
}
func (c *ManagedClusterAddOnClient) Update(ctx context.Context, addon *addonapiv1alpha1.ManagedClusterAddOn, opts metav1.UpdateOptions) (*addonapiv1alpha1.ManagedClusterAddOn, error) {
return nil, errors.NewMethodNotSupported(common.ManagedClusterAddOnGR, "update")
}
func (c *ManagedClusterAddOnClient) UpdateStatus(ctx context.Context, addon *addonapiv1alpha1.ManagedClusterAddOn, opts metav1.UpdateOptions) (*addonapiv1alpha1.ManagedClusterAddOn, error) {
return nil, errors.NewMethodNotSupported(common.ManagedClusterAddOnGR, "updatestatus")
}
func (c *ManagedClusterAddOnClient) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
return errors.NewMethodNotSupported(common.ManagedClusterAddOnGR, "delete")
}
func (c *ManagedClusterAddOnClient) DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error {
return errors.NewMethodNotSupported(common.ManagedClusterAddOnGR, "deletecollection")
}
func (c *ManagedClusterAddOnClient) Get(ctx context.Context, name string, opts metav1.GetOptions) (*addonapiv1alpha1.ManagedClusterAddOn, error) {
klog.V(4).Infof("getting ManagedClusterAddOn %s/%s", c.namespace, name)
addon, exists, err := c.watcherStore.Get(c.namespace, name)
if err != nil {
return nil, errors.NewInternalError(err)
}
if !exists {
return nil, errors.NewNotFound(common.ManagedClusterAddOnGR, c.namespace+"/"+name)
}
return addon, nil
}
func (c *ManagedClusterAddOnClient) List(ctx context.Context, opts metav1.ListOptions) (*addonapiv1alpha1.ManagedClusterAddOnList, error) {
klog.V(4).Info("list ManagedClusterAddon")
addonList, err := c.watcherStore.List(c.namespace, opts)
if err != nil {
return nil, errors.NewInternalError(err)
}
items := []addonapiv1alpha1.ManagedClusterAddOn{}
for _, cluster := range addonList.Items {
items = append(items, *cluster)
}
return &addonapiv1alpha1.ManagedClusterAddOnList{ListMeta: addonList.ListMeta, Items: items}, nil
}
func (c *ManagedClusterAddOnClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) {
klog.V(4).Info("watch ManagedClusterAddOn")
watcher, err := c.watcherStore.GetWatcher(c.namespace, opts)
if err != nil {
return nil, errors.NewInternalError(err)
}
return watcher, nil
}
func (c *ManagedClusterAddOnClient) Patch(
ctx context.Context, name string, pt kubetypes.PatchType, data []byte, opts metav1.PatchOptions, subresources ...string) (*addonapiv1alpha1.ManagedClusterAddOn, error) {
klog.V(4).Infof("patching ManagedClusterAddon %s/%s", c.namespace, name)
last, exists, err := c.watcherStore.Get(c.namespace, name)
if err != nil {
return nil, errors.NewInternalError(err)
}
if !exists {
return nil, errors.NewNotFound(common.ManagedClusterAddOnGR, c.namespace+"/"+name)
}
patchedAddon, err := utils.Patch(pt, last, data)
if err != nil {
return nil, errors.NewInternalError(err)
}
eventType := types.CloudEventsType{
CloudEventsDataType: ManagedClusterAddOnEventDataType,
SubResource: types.SubResourceStatus,
}
newAddon := patchedAddon.DeepCopy()
if !utils.IsStatusPatch(subresources) {
msg := "subresources \"status\" is required"
return nil, errors.NewGenericServerResponse(http.StatusMethodNotAllowed, "patch", common.ManagedClusterAddOnGR, name, msg, 0, false)
}
// publish the status update event to source, source will check the resource version
// and reject the update if it's status update is outdated.
eventType.Action = types.UpdateRequestAction
if err := c.cloudEventsClient.Publish(ctx, eventType, newAddon); err != nil {
return nil, cloudeventserrors.ToStatusError(common.ManagedClusterAddOnGR, name, err)
}
return newAddon, nil
}