-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdynakube.go
278 lines (238 loc) · 8.5 KB
/
dynakube.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package dynakube
import (
"context"
"encoding/json"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic/fake"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/testing"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
)
// Client is a fake client which implements controller runtime client.
type Client struct {
client *fake.FakeDynamicClient
scheme *runtime.Scheme
}
// DEPRECATED. Use NewClient
func NewDynamicClient(scheme *runtime.Scheme, objects ...runtime.Object) *Client {
return NewClient(scheme, objects...)
}
func NewClient(scheme *runtime.Scheme, objects ...runtime.Object) *Client {
// https://github.com/kubernetes/kubernetes/commit/418fa71b6b1d1fba930daaba1f8ecf55070b4bdf introduced a change to
// always add unstructured types to the schema created instead of the actual types which then breaks converting
// from the concrete types to unstructured
// workaround this by calling the underlying custom list kinds method
dynamicClient := fake.NewSimpleDynamicClientWithCustomListKinds(scheme, nil, objects...)
return &Client{
client: dynamicClient,
scheme: scheme,
}
}
func (c *Client) invokeAction(action testing.Action, obj client.Object) error {
gvr, err := getGVRFromObject(obj, c.scheme)
if err != nil { // untested section
return err
}
// can't do reflection here, so ugly switch case.
switch v := action.(type) {
case testing.CreateActionImpl:
v.Resource = gvr
action = v
case testing.GetActionImpl:
v.Resource = gvr
action = v
case testing.PatchActionImpl:
v.Resource = gvr
action = v
case testing.DeleteActionImpl:
v.Resource = gvr
action = v
case testing.UpdateActionImpl:
v.Resource = gvr
action = v
default: // untested section
return fmt.Errorf("unsupported type: %v", v)
}
o, err := c.client.Invokes(action, &metav1.Status{Status: "dynamic get fail"})
if err != nil { // untested section
return err
}
j, err := json.Marshal(o)
if err != nil { // untested section
return err
}
decoder := scheme.Codecs.UniversalDecoder()
_, _, err = decoder.Decode(j, nil, obj)
return err
}
// Patch ...
func (c *Client) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
metaObj := obj.(metav1.Object)
data, err := patch.Data(obj)
if err != nil { // untested section
return err
}
action := testing.NewPatchAction(schema.GroupVersionResource{}, metaObj.GetNamespace(), obj.GetName(), patch.Type(), data)
return c.invokeAction(action, obj)
}
// DeleteAllOf ...
func (c *Client) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
panic("not implemented")
}
// Scheme ...
func (c *Client) Scheme() *runtime.Scheme {
return c.scheme
}
// RESTMapper ...
func (c *Client) RESTMapper() meta.RESTMapper {
panic("implement me")
}
var _ client.Client = &Client{}
// Get retrieves an obj for the given object key from the Kubernetes Cluster.
func (c *Client) Get(ctx context.Context, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
action := testing.NewGetAction(schema.GroupVersionResource{}, key.Namespace, key.Name)
return c.invokeAction(action, obj)
}
// List retrieves list of objects for a given namespace and list options.
func (c *Client) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
listOpts := client.ListOptions{}
listOpts.ApplyOptions(opts)
metav1ListOpts := listOpts.AsListOptions()
if metav1ListOpts == nil {
return fmt.Errorf("unable to convert %T to %T", listOpts, metav1ListOpts)
}
gvk, _ := apiutil.GVKForObject(list, c.scheme)
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4] /*base library appends List*/
gvr, _ := meta.UnsafeGuessKindToResource(gvk)
// TODO: support non-namespaced resources
var action testing.Action
if listOpts.Namespace == "" {
action = testing.NewRootListAction(gvr, gvk, *metav1ListOpts)
} else {
action = testing.NewListAction(gvr, gvk, listOpts.Namespace, *metav1ListOpts)
}
o, err := c.client.Invokes(action, &metav1.Status{Status: "dynamic list fail"})
if err != nil { // untested section
return err
}
j, err := json.Marshal(o)
if err != nil { // untested section
return err
}
decoder := scheme.Codecs.UniversalDecoder()
_, _, err = decoder.Decode(j, nil, list)
return err
}
// Create saves the object obj.
func (c *Client) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
action := testing.NewCreateAction(schema.GroupVersionResource{}, obj.GetNamespace(), obj)
return c.invokeAction(action, obj)
}
// Delete deletes the given obj.
func (c *Client) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
action := testing.NewDeleteAction(schema.GroupVersionResource{}, obj.GetNamespace(), obj.GetName())
return c.invokeAction(action, obj)
}
// Update updates the given obj.
func (c *Client) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
action := testing.NewUpdateAction(schema.GroupVersionResource{}, obj.GetNamespace(), obj)
return c.invokeAction(action, obj)
}
// SubResource ...
func (c *Client) SubResource(subResource string) client.SubResourceClient {
return &fakeSubResourceClient{
client: c,
subResource: subResource,
}
}
// Status returns fake status writer.
func (c *Client) Status() client.StatusWriter {
return c.SubResource("status")
}
func (c *Client) PrependReactor(verb string, resource string, action func(action testing.Action) (handled bool, ret runtime.Object, err error)) {
c.client.PrependReactor(verb, resource, action)
}
func (c *Client) AddObjects(objs ...runtime.Object) error {
objects, err := convertObjectsToUnstructured(c.scheme, objs)
if err != nil {
panic(err)
}
for _, obj := range objects {
err := c.client.Tracker().Add(obj)
if err != nil { // untested section
return err
}
}
return nil
}
func getGVRFromObject(obj runtime.Object, scheme *runtime.Scheme) (schema.GroupVersionResource, error) {
// untested section
gvk, err := apiutil.GVKForObject(obj, scheme)
if err != nil {
// untested section
return schema.GroupVersionResource{}, err
}
gvr, _ := meta.UnsafeGuessKindToResource(gvk)
return gvr, nil
}
type fakeSubResourceClient struct {
client *Client
subResource string
}
var _ client.SubResourceClient = &fakeSubResourceClient{}
func (sw *fakeSubResourceClient) Get(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceGetOption) error {
panic("fakeSubResourceClient does not support get")
}
func (sw *fakeSubResourceClient) Create(ctx context.Context, obj client.Object, subResource client.Object, opts ...client.SubResourceCreateOption) error {
panic("fakeSubResourceWriter does not support create")
}
func (sw *fakeSubResourceClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.SubResourcePatchOption) error {
data, err := patch.Data(obj)
if err != nil { // untested section
return err
}
action := testing.NewPatchSubresourceAction(schema.GroupVersionResource{}, obj.GetNamespace(), obj.GetName(), patch.Type(), data, sw.subResource)
return sw.client.invokeAction(action, obj)
}
func (sw *fakeSubResourceClient) Update(ctx context.Context, obj client.Object, opts ...client.SubResourceUpdateOption) error {
action := testing.NewUpdateSubresourceAction(schema.GroupVersionResource{}, sw.subResource, obj.GetNamespace(), obj)
return sw.client.invokeAction(action, obj)
}
func convertObjectsToUnstructured(s *runtime.Scheme, objs []runtime.Object) ([]runtime.Object, error) {
ul := make([]runtime.Object, 0, len(objs))
for _, obj := range objs {
u, err := convertToUnstructured(s, obj)
if err != nil {
return nil, err
}
ul = append(ul, u)
}
return ul, nil
}
func convertToUnstructured(s *runtime.Scheme, obj runtime.Object) (runtime.Object, error) {
var (
err error
u unstructured.Unstructured
)
u.Object, err = runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil { // untested section
return nil, fmt.Errorf("failed to convert to unstructured: %w", err)
}
gvk := u.GroupVersionKind()
if gvk.Group == "" || gvk.Kind == "" {
gvks, _, err := s.ObjectKinds(obj)
if err != nil {
return nil, fmt.Errorf("failed to convert to unstructured - unable to get GVK %w", err)
}
apiv, k := gvks[0].ToAPIVersionAndKind()
u.SetAPIVersion(apiv)
u.SetKind(k)
}
return &u, nil
}