github.com/oam-dev/kubevela@v1.9.11/pkg/multicluster/fake.go (about) 1 /* 2 Copyright 2021 The KubeVela Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package multicluster 18 19 import ( 20 "context" 21 22 "sigs.k8s.io/controller-runtime/pkg/client" 23 ) 24 25 // FakeClient set default client and multicluster clients 26 type FakeClient struct { 27 client.Client 28 clients map[string]client.Client 29 } 30 31 // NewFakeClient create a new fake client 32 func NewFakeClient(baseClient client.Client) *FakeClient { 33 return &FakeClient{Client: baseClient, clients: map[string]client.Client{}} 34 } 35 36 // AddCluster add cluster to client map 37 func (c *FakeClient) AddCluster(cluster string, cli client.Client) { 38 c.clients[cluster] = cli 39 } 40 41 func (c *FakeClient) getClient(ctx context.Context) client.Client { 42 cluster := ClusterNameInContext(ctx) 43 if cli, exists := c.clients[cluster]; exists { 44 return cli 45 } 46 return c.Client 47 } 48 49 // Get retrieves an obj for the given object key from the Kubernetes Cluster. 50 // obj must be a struct pointer so that obj can be updated with the response 51 // returned by the Server. 52 func (c *FakeClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object, _ ...client.GetOption) error { 53 return c.getClient(ctx).Get(ctx, key, obj) 54 } 55 56 // List retrieves list of objects for a given namespace and list options. On a 57 // successful call, Items field in the list will be populated with the 58 // result returned from the server. 59 func (c *FakeClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error { 60 return c.getClient(ctx).List(ctx, list, opts...) 61 } 62 63 // Create saves the object obj in the Kubernetes cluster. 64 func (c *FakeClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error { 65 return c.getClient(ctx).Create(ctx, obj, opts...) 66 } 67 68 // Delete deletes the given obj from Kubernetes cluster. 69 func (c *FakeClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error { 70 return c.getClient(ctx).Delete(ctx, obj, opts...) 71 } 72 73 // Update updates the given obj in the Kubernetes cluster. obj must be a 74 // struct pointer so that obj can be updated with the content returned by the Server. 75 func (c *FakeClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error { 76 return c.getClient(ctx).Update(ctx, obj, opts...) 77 } 78 79 // Patch patches the given obj in the Kubernetes cluster. obj must be a 80 // struct pointer so that obj can be updated with the content returned by the Server. 81 func (c *FakeClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error { 82 return c.getClient(ctx).Patch(ctx, obj, patch, opts...) 83 } 84 85 // DeleteAllOf deletes all objects of the given type matching the given options. 86 func (c *FakeClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error { 87 return c.getClient(ctx).DeleteAllOf(ctx, obj, opts...) 88 }