|
| 1 | +package v1beta1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "k8s.io/apimachinery/pkg/api/errors" |
| 6 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 7 | + kubetypes "k8s.io/apimachinery/pkg/types" |
| 8 | + "k8s.io/apimachinery/pkg/watch" |
| 9 | + "k8s.io/client-go/rest" |
| 10 | + "k8s.io/klog/v2" |
| 11 | + "net/http" |
| 12 | + addonapiv1beta1 "open-cluster-management.io/api/addon/v1beta1" |
| 13 | + |
| 14 | + addonv1beta1client "open-cluster-management.io/api/client/addon/clientset/versioned/typed/addon/v1beta1" |
| 15 | + "open-cluster-management.io/sdk-go/pkg/cloudevents/clients/common" |
| 16 | + cloudeventserrors "open-cluster-management.io/sdk-go/pkg/cloudevents/clients/errors" |
| 17 | + "open-cluster-management.io/sdk-go/pkg/cloudevents/clients/store" |
| 18 | + "open-cluster-management.io/sdk-go/pkg/cloudevents/clients/utils" |
| 19 | + "open-cluster-management.io/sdk-go/pkg/cloudevents/generic" |
| 20 | + "open-cluster-management.io/sdk-go/pkg/cloudevents/generic/types" |
| 21 | +) |
| 22 | + |
| 23 | +// ManagedClusterAddOnClient implements the ManagedClusterAddonInterface. |
| 24 | +type ManagedClusterAddOnClient struct { |
| 25 | + cloudEventsClient generic.CloudEventsClient[*addonapiv1beta1.ManagedClusterAddOn] |
| 26 | + watcherStore store.ClientWatcherStore[*addonapiv1beta1.ManagedClusterAddOn] |
| 27 | + namespace string |
| 28 | +} |
| 29 | + |
| 30 | +var _ addonv1beta1client.ManagedClusterAddOnInterface = &ManagedClusterAddOnClient{} |
| 31 | + |
| 32 | +func NewManagedClusterAddOnClient( |
| 33 | + cloudEventsClient generic.CloudEventsClient[*addonapiv1beta1.ManagedClusterAddOn], |
| 34 | + watcherStore store.ClientWatcherStore[*addonapiv1beta1.ManagedClusterAddOn], |
| 35 | +) *ManagedClusterAddOnClient { |
| 36 | + return &ManagedClusterAddOnClient{ |
| 37 | + cloudEventsClient: cloudEventsClient, |
| 38 | + watcherStore: watcherStore, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (c *ManagedClusterAddOnClient) Namespace(namespace string) *ManagedClusterAddOnClient { |
| 43 | + return &ManagedClusterAddOnClient{ |
| 44 | + cloudEventsClient: c.cloudEventsClient, |
| 45 | + watcherStore: c.watcherStore, |
| 46 | + namespace: namespace, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (c *ManagedClusterAddOnClient) Create( |
| 51 | + _ context.Context, _ *addonapiv1beta1.ManagedClusterAddOn, _ metav1.CreateOptions) (*addonapiv1beta1.ManagedClusterAddOn, error) { |
| 52 | + return nil, errors.NewMethodNotSupported(common.ManagedClusterAddOnGR, "create") |
| 53 | +} |
| 54 | + |
| 55 | +func (c *ManagedClusterAddOnClient) Update(_ context.Context, _ *addonapiv1beta1.ManagedClusterAddOn, _ metav1.UpdateOptions) (*addonapiv1beta1.ManagedClusterAddOn, error) { |
| 56 | + return nil, errors.NewMethodNotSupported(common.ManagedClusterAddOnGR, "update") |
| 57 | +} |
| 58 | + |
| 59 | +func (c *ManagedClusterAddOnClient) UpdateStatus(_ context.Context, _ *addonapiv1beta1.ManagedClusterAddOn, _ metav1.UpdateOptions) (*addonapiv1beta1.ManagedClusterAddOn, error) { |
| 60 | + return nil, errors.NewMethodNotSupported(common.ManagedClusterAddOnGR, "updatestatus") |
| 61 | +} |
| 62 | + |
| 63 | +func (c *ManagedClusterAddOnClient) Delete(_ context.Context, _ string, _ metav1.DeleteOptions) error { |
| 64 | + return errors.NewMethodNotSupported(common.ManagedClusterAddOnGR, "delete") |
| 65 | +} |
| 66 | + |
| 67 | +func (c *ManagedClusterAddOnClient) DeleteCollection(_ context.Context, _ metav1.DeleteOptions, _ metav1.ListOptions) error { |
| 68 | + return errors.NewMethodNotSupported(common.ManagedClusterAddOnGR, "deletecollection") |
| 69 | +} |
| 70 | + |
| 71 | +func (c *ManagedClusterAddOnClient) Get(ctx context.Context, name string, _ metav1.GetOptions) (*addonapiv1beta1.ManagedClusterAddOn, error) { |
| 72 | + logger := klog.FromContext(ctx) |
| 73 | + logger.V(4).Info("getting ManagedClusterAddOn", "namespace", c.namespace, "name", name) |
| 74 | + addon, exists, err := c.watcherStore.Get(c.namespace, name) |
| 75 | + if err != nil { |
| 76 | + return nil, errors.NewInternalError(err) |
| 77 | + } |
| 78 | + if !exists { |
| 79 | + return nil, errors.NewNotFound(common.ManagedClusterAddOnGR, c.namespace+"/"+name) |
| 80 | + } |
| 81 | + |
| 82 | + return addon, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (c *ManagedClusterAddOnClient) List(ctx context.Context, opts metav1.ListOptions) (*addonapiv1beta1.ManagedClusterAddOnList, error) { |
| 86 | + logger := klog.FromContext(ctx) |
| 87 | + logger.V(4).Info("list ManagedClusterAddon") |
| 88 | + addonList, err := c.watcherStore.List(c.namespace, opts) |
| 89 | + if err != nil { |
| 90 | + return nil, errors.NewInternalError(err) |
| 91 | + } |
| 92 | + |
| 93 | + items := []addonapiv1beta1.ManagedClusterAddOn{} |
| 94 | + for _, cluster := range addonList.Items { |
| 95 | + items = append(items, *cluster) |
| 96 | + } |
| 97 | + |
| 98 | + return &addonapiv1beta1.ManagedClusterAddOnList{ListMeta: addonList.ListMeta, Items: items}, nil |
| 99 | +} |
| 100 | + |
| 101 | +func (c *ManagedClusterAddOnClient) Watch(ctx context.Context, opts metav1.ListOptions) (watch.Interface, error) { |
| 102 | + logger := klog.FromContext(ctx) |
| 103 | + logger.V(4).Info("watch ManagedClusterAddOn") |
| 104 | + watcher, err := c.watcherStore.GetWatcher(c.namespace, opts) |
| 105 | + if err != nil { |
| 106 | + return nil, errors.NewInternalError(err) |
| 107 | + } |
| 108 | + |
| 109 | + return watcher, nil |
| 110 | +} |
| 111 | + |
| 112 | +func (c *ManagedClusterAddOnClient) Patch( |
| 113 | + ctx context.Context, name string, pt kubetypes.PatchType, data []byte, _ metav1.PatchOptions, subresources ...string) (*addonapiv1beta1.ManagedClusterAddOn, error) { |
| 114 | + logger := klog.FromContext(ctx) |
| 115 | + logger.V(4).Info("patching ManagedClusterAddon", "namespace", c.namespace, "name", name) |
| 116 | + last, exists, err := c.watcherStore.Get(c.namespace, name) |
| 117 | + if err != nil { |
| 118 | + return nil, errors.NewInternalError(err) |
| 119 | + } |
| 120 | + if !exists { |
| 121 | + return nil, errors.NewNotFound(common.ManagedClusterAddOnGR, c.namespace+"/"+name) |
| 122 | + } |
| 123 | + |
| 124 | + patchedAddon, err := utils.Patch(pt, last, data) |
| 125 | + if err != nil { |
| 126 | + return nil, errors.NewInternalError(err) |
| 127 | + } |
| 128 | + |
| 129 | + eventType := types.CloudEventsType{ |
| 130 | + CloudEventsDataType: ManagedClusterAddOnEventDataType, |
| 131 | + SubResource: types.SubResourceStatus, |
| 132 | + } |
| 133 | + |
| 134 | + newAddon := patchedAddon.DeepCopy() |
| 135 | + |
| 136 | + if !utils.IsStatusPatch(subresources) { |
| 137 | + msg := "subresources \"status\" is required" |
| 138 | + return nil, errors.NewGenericServerResponse(http.StatusMethodNotAllowed, "patch", common.ManagedClusterAddOnGR, name, msg, 0, false) |
| 139 | + } |
| 140 | + |
| 141 | + // publish the status update event to source, source will check the resource version |
| 142 | + // and reject the update if it's status update is outdated. |
| 143 | + eventType.Action = types.UpdateRequestAction |
| 144 | + if err := c.cloudEventsClient.Publish(ctx, eventType, newAddon); err != nil { |
| 145 | + return nil, cloudeventserrors.ToStatusError(common.ManagedClusterAddOnGR, name, err) |
| 146 | + } |
| 147 | + |
| 148 | + return newAddon, nil |
| 149 | +} |
| 150 | + |
| 151 | +// AddonClientWrapper wraps ManagedClusterAddOnClient to AddonV1beta1Interface |
| 152 | +type AddonClientWrapper struct { |
| 153 | + client *ManagedClusterAddOnClient |
| 154 | +} |
| 155 | + |
| 156 | +var _ addonv1beta1client.AddonV1beta1Interface = &AddonClientWrapper{} |
| 157 | + |
| 158 | +func NewAddonClientWrapper(client *ManagedClusterAddOnClient) *AddonClientWrapper { |
| 159 | + return &AddonClientWrapper{client: client} |
| 160 | +} |
| 161 | + |
| 162 | +func (c *AddonClientWrapper) ClusterManagementAddOns() addonv1beta1client.ClusterManagementAddOnInterface { |
| 163 | + panic("ClusterManagementAddOns is unsupported") |
| 164 | +} |
| 165 | + |
| 166 | +func (c *AddonClientWrapper) RESTClient() rest.Interface { |
| 167 | + panic("RESTClient is unsupported") |
| 168 | +} |
| 169 | + |
| 170 | +func (c *AddonClientWrapper) ManagedClusterAddOns(namespace string) addonv1beta1client.ManagedClusterAddOnInterface { |
| 171 | + return c.client.Namespace(namespace) |
| 172 | +} |
0 commit comments