/
spy_client.go
234 lines (186 loc) · 6.69 KB
/
spy_client.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
package rest
import (
"context"
"k8s.io/apimachinery/pkg/api/meta"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/apis/example"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/klog/v2"
)
// Unified Storage Spy
type StorageSpyClient interface {
Storage
//Counts returns the number of times a certain method was called
Counts(string) int
}
type StorageSpy struct {
counts map[string]int
}
type spyStorageClient struct {
Storage
spy *StorageSpy
}
func (s *StorageSpy) record(seen string) {
s.counts[seen]++
}
func NewStorageSpyClient(s Storage) StorageSpyClient {
return &spyStorageClient{s, &StorageSpy{
counts: map[string]int{},
}}
}
func (c *spyStorageClient) Counts(method string) int {
return c.spy.counts[method]
}
//nolint:golint,unused
type spyStorageShim struct {
Storage
spy *StorageSpy
}
//nolint:golint,unused
type spyLegacyStorageShim struct {
LegacyStorage
spy *StorageSpy
}
func (c *spyStorageClient) Create(ctx context.Context, obj runtime.Object, valitation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
c.spy.record("Storage.Create")
klog.Info("method: Storage.Create")
return &dummyObject{}, nil
}
func (c *spyStorageClient) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
c.spy.record("Storage.Get")
klog.Info("method: Storage.Get")
return &example.Pod{}, nil
}
func (c *spyStorageClient) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
c.spy.record("Storage.List")
klog.Info("method: Storage.List")
i1 := dummyObject{Foo: "Storage field 1"}
accessor, err := meta.Accessor(&i1)
if err != nil {
return nil, err
}
accessor.SetName("Item 1")
i2 := dummyObject{Foo: "Storage field 2"}
accessor, err = meta.Accessor(&i2)
if err != nil {
return nil, err
}
accessor.SetName("Item 2")
return &dummyList{Items: []dummyObject{i1, i2}}, nil
}
type UpdatedObjInfoObj struct{}
func (u UpdatedObjInfoObj) UpdatedObject(ctx context.Context, oldObj runtime.Object) (newObj runtime.Object, err error) {
return &example.Pod{}, nil
}
func (u UpdatedObjInfoObj) Preconditions() *metav1.Preconditions { return &metav1.Preconditions{} }
func (c *spyStorageClient) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
c.spy.record("Storage.Update")
klog.Info("method: Storage.Update")
return &example.Pod{}, false, nil
}
func (c *spyStorageClient) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
c.spy.record("Storage.Delete")
klog.Info("method: Storage.Delete")
return nil, false, nil
}
func (c *spyStorageClient) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
c.spy.record("Storage.DeleteCollection")
klog.Info("method: Storage.DeleteCollection")
return nil, nil
}
// LegacyStorage Spy
type LegacyStorageSpyClient interface {
LegacyStorage
//Counts returns the number of times a certain method was called
Counts(string) int
}
type LegacyStorageSpy struct {
counts map[string]int //nolint:golint,unused
}
type spyLegacyStorageClient struct {
LegacyStorage
spy *StorageSpy
}
//nolint:golint,unused
func (s *LegacyStorageSpy) record(seen string) {
s.counts[seen]++
}
func NewLegacyStorageSpyClient(ls LegacyStorage) LegacyStorageSpyClient {
return &spyLegacyStorageClient{ls, &StorageSpy{
counts: map[string]int{},
}}
}
func (c *spyLegacyStorageClient) Counts(method string) int {
return c.spy.counts[method]
}
func (c *spyLegacyStorageClient) Create(ctx context.Context, obj runtime.Object, valitation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
c.spy.record("LegacyStorage.Create")
klog.Info("method: LegacyStorage.Create")
return &dummyObject{}, nil
}
func (c *spyLegacyStorageClient) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
c.spy.record("LegacyStorage.Get")
klog.Info("method: LegacyStorage.Get")
return &example.Pod{}, nil
}
func (c *spyLegacyStorageClient) NewList() runtime.Object {
// stub for now so that spyLegacyStorageClient implements rest.Lister
return nil
}
func (c *spyLegacyStorageClient) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
c.spy.record("LegacyStorage.List")
klog.Info("method: LegacyStorage.List")
i1 := dummyObject{Foo: "Legacy field 1"}
accessor, err := meta.Accessor(&i1)
if err != nil {
return nil, err
}
accessor.SetName("Item 1")
i3 := dummyObject{Foo: "Legacy field 3"}
accessor, err = meta.Accessor(&i3)
if err != nil {
return nil, err
}
accessor.SetName("Item 3")
return &dummyList{Items: []dummyObject{i1, i3}}, nil
}
func (c *spyLegacyStorageClient) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
c.spy.record("LegacyStorage.Update")
klog.Info("method: LegacyStorage.Update")
return &example.Pod{}, false, nil
}
func (c *spyLegacyStorageClient) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
c.spy.record("LegacyStorage.Delete")
klog.Info("method: LegacyStorage.Delete")
return nil, false, nil
}
func (c *spyLegacyStorageClient) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
c.spy.record("LegacyStorage.DeleteCollection")
klog.Info("method: LegacyStorage.DeleteCollection")
return nil, nil
}
type dummyList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []dummyObject `json:"items,omitempty"`
}
type dummyObject struct {
metav1.TypeMeta `json:",inline"`
Foo string
metav1.ObjectMeta `json:"metadata,omitempty"`
}
func (d *dummyList) GetObjectKind() schema.ObjectKind {
return nil
}
func (d *dummyList) DeepCopyObject() runtime.Object {
return nil
}
func (d *dummyObject) GetObjectKind() schema.ObjectKind {
return nil
}
func (d *dummyObject) DeepCopyObject() runtime.Object {
return nil
}