-
Notifications
You must be signed in to change notification settings - Fork 864
/
fake.go
88 lines (71 loc) · 3.24 KB
/
fake.go
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
/*
Copyright 2021 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package multicluster
import (
"context"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// FakeClient set default client and multicluster clients
type FakeClient struct {
client.Client
clients map[string]client.Client
}
// NewFakeClient create a new fake client
func NewFakeClient(baseClient client.Client) *FakeClient {
return &FakeClient{Client: baseClient, clients: map[string]client.Client{}}
}
// AddCluster add cluster to client map
func (c *FakeClient) AddCluster(cluster string, cli client.Client) {
c.clients[cluster] = cli
}
func (c *FakeClient) getClient(ctx context.Context) client.Client {
cluster := ClusterNameInContext(ctx)
if cli, exists := c.clients[cluster]; exists {
return cli
}
return c.Client
}
// Get retrieves an obj for the given object key from the Kubernetes Cluster.
// obj must be a struct pointer so that obj can be updated with the response
// returned by the Server.
func (c *FakeClient) Get(ctx context.Context, key client.ObjectKey, obj client.Object) error {
return c.getClient(ctx).Get(ctx, key, obj)
}
// List retrieves list of objects for a given namespace and list options. On a
// successful call, Items field in the list will be populated with the
// result returned from the server.
func (c *FakeClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
return c.getClient(ctx).List(ctx, list, opts...)
}
// Create saves the object obj in the Kubernetes cluster.
func (c *FakeClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
return c.getClient(ctx).Create(ctx, obj, opts...)
}
// Delete deletes the given obj from Kubernetes cluster.
func (c *FakeClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
return c.getClient(ctx).Delete(ctx, obj, opts...)
}
// Update updates the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
func (c *FakeClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
return c.getClient(ctx).Update(ctx, obj, opts...)
}
// Patch patches the given obj in the Kubernetes cluster. obj must be a
// struct pointer so that obj can be updated with the content returned by the Server.
func (c *FakeClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
return c.getClient(ctx).Patch(ctx, obj, patch, opts...)
}
// DeleteAllOf deletes all objects of the given type matching the given options.
func (c *FakeClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
return c.getClient(ctx).DeleteAllOf(ctx, obj, opts...)
}