forked from kubernetes-sigs/apiserver-runtime
/
memory_rest.go
445 lines (387 loc) · 10.1 KB
/
memory_rest.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package memory
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"time"
"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/conversion"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
)
// ErrFileNotExists means the file doesn't actually exist.
var ErrFileNotExists = fmt.Errorf("file doesn't exist")
// ErrNamespaceNotExists means the directory for the namespace doesn't actually exist.
var ErrNamespaceNotExists = errors.New("namespace does not exist")
var _ rest.StandardStorage = &memoryREST{}
var _ rest.Scoper = &memoryREST{}
var _ rest.Storage = &memoryREST{}
// NewMemoryREST instantiates a new REST storage.
func NewMemoryREST(
groupResource schema.GroupResource,
codec runtime.Codec,
isNamespaced bool,
newFunc func() runtime.Object,
newListFunc func() runtime.Object,
) rest.Storage {
objRoot := filepath.Join(groupResource.Group, groupResource.Resource)
if err := ensureDir(objRoot); err != nil {
panic(fmt.Sprintf("unable to write data dir: %s", err))
}
// file REST
rest := &memoryREST{
TableConvertor: rest.NewDefaultTableConvertor(groupResource),
codec: codec,
objRootPath: objRoot,
isNamespaced: isNamespaced,
newFunc: newFunc,
newListFunc: newListFunc,
watchers: make(map[int]*jsonWatch, 10),
}
return rest
}
type memoryREST struct {
rest.TableConvertor
codec runtime.Codec
objRootPath string
isNamespaced bool
muWatchers sync.RWMutex
watchers map[int]*jsonWatch
newFunc func() runtime.Object
newListFunc func() runtime.Object
muStorage sync.RWMutex
storage map[string]memoryStorage
}
type memoryStorage struct {
obj runtime.Object
createdTime time.Time
}
func (f *memoryREST) notifyWatchers(ev watch.Event) {
f.muWatchers.RLock()
for _, w := range f.watchers {
w.ch <- ev
}
f.muWatchers.RUnlock()
}
func (f *memoryREST) New() runtime.Object {
return f.newFunc()
}
func (f *memoryREST) Destroy() {}
func (f *memoryREST) NewList() runtime.Object {
return f.newListFunc()
}
func (f *memoryREST) NamespaceScoped() bool {
return f.isNamespaced
}
func (f *memoryREST) Get(
ctx context.Context,
name string,
options *metav1.GetOptions,
) (runtime.Object, error) {
filename := f.objectMemoryKey(ctx, name)
f.muStorage.RLock()
defer f.muStorage.RUnlock()
return f.storage[filename].obj, nil
}
func (f *memoryREST) List(
ctx context.Context,
options *metainternalversion.ListOptions,
) (runtime.Object, error) {
newListObj := f.NewList()
v, err := getListPrt(newListObj)
if err != nil {
return nil, err
}
dirname := f.objectDirName(ctx)
if err := visitDir(dirname, f.newFunc, f.codec, func(path string, obj runtime.Object) {
appendItem(v, obj)
}); err != nil {
return nil, fmt.Errorf("failed walking filepath %v", dirname)
}
return newListObj, nil
}
func (f *memoryREST) Create(
ctx context.Context,
obj runtime.Object,
createValidation rest.ValidateObjectFunc,
options *metav1.CreateOptions,
) (runtime.Object, error) {
if createValidation != nil {
if err := createValidation(ctx, obj); err != nil {
return nil, err
}
}
accessor, err := meta.Accessor(obj)
if err != nil {
return nil, err
}
filename := f.objectMemoryKey(ctx, accessor.GetName())
if f.isNamespaced {
// ensures namespace dir
_, ok := genericapirequest.NamespaceFrom(ctx)
if !ok {
return nil, ErrNamespaceNotExists
}
}
f.muStorage.RLock()
_, found := f.storage[filename]
f.muStorage.RUnlock()
if !found {
f.muStorage.Lock()
f.storage[filename] = memoryStorage{
obj: obj,
createdTime: time.Now(),
}
f.muStorage.Unlock()
}
f.muStorage.RLock()
if _, ok := f.storage[filename]; !ok {
return nil, ErrFileNotExists
}
f.muStorage.RUnlock()
f.notifyWatchers(watch.Event{
Type: watch.Added,
Object: obj,
})
return obj, nil
}
func (f *memoryREST) Update(
ctx context.Context,
name string,
objInfo rest.UpdatedObjectInfo,
createValidation rest.ValidateObjectFunc,
updateValidation rest.ValidateObjectUpdateFunc,
forceAllowCreate bool,
options *metav1.UpdateOptions,
) (runtime.Object, bool, error) {
isCreate := false
oldObj, err := f.Get(ctx, name, nil)
if err != nil {
if !forceAllowCreate {
return nil, false, err
}
isCreate = true
}
// TODO: should not be necessary, verify Get works before creating filepath
if f.isNamespaced {
// ensures namespace dir
_, ok := genericapirequest.NamespaceFrom(ctx)
if !ok {
return nil, false, ErrNamespaceNotExists
}
}
updatedObj, err := objInfo.UpdatedObject(ctx, oldObj)
if err != nil {
return nil, false, err
}
filename := f.objectMemoryKey(ctx, name)
if isCreate {
if createValidation != nil {
if err := createValidation(ctx, updatedObj); err != nil {
return nil, false, err
}
}
f.muStorage.Lock()
f.storage[filename] = memoryStorage{
obj: updatedObj,
createdTime: time.Now(),
}
f.muStorage.Unlock()
f.notifyWatchers(watch.Event{
Type: watch.Added,
Object: updatedObj,
})
return updatedObj, true, nil
}
if updateValidation != nil {
if err := updateValidation(ctx, updatedObj, oldObj); err != nil {
return nil, false, err
}
}
if err := write(f.codec, filename, updatedObj); err != nil {
return nil, false, err
}
f.notifyWatchers(watch.Event{
Type: watch.Modified,
Object: updatedObj,
})
return updatedObj, false, nil
}
func (f *memoryREST) Delete(
ctx context.Context,
name string,
deleteValidation rest.ValidateObjectFunc,
options *metav1.DeleteOptions) (runtime.Object, bool, error) {
filename := f.objectMemoryKey(ctx, name)
if !exists(filename) {
return nil, false, ErrFileNotExists
}
oldObj, err := f.Get(ctx, name, nil)
if err != nil {
return nil, false, err
}
if deleteValidation != nil {
if err := deleteValidation(ctx, oldObj); err != nil {
return nil, false, err
}
}
if err := os.Remove(filename); err != nil {
return nil, false, err
}
f.notifyWatchers(watch.Event{
Type: watch.Deleted,
Object: oldObj,
})
return oldObj, true, nil
}
func (f *memoryREST) DeleteCollection(
ctx context.Context,
deleteValidation rest.ValidateObjectFunc,
options *metav1.DeleteOptions,
listOptions *metainternalversion.ListOptions,
) (runtime.Object, error) {
newListObj := f.NewList()
v, err := getListPrt(newListObj)
if err != nil {
return nil, err
}
dirname := f.objectDirName(ctx)
if err := visitDir(dirname, f.newFunc, f.codec, func(path string, obj runtime.Object) {
_ = os.Remove(path)
appendItem(v, obj)
}); err != nil {
return nil, fmt.Errorf("failed walking filepath %v", dirname)
}
return newListObj, nil
}
func (f *memoryREST) objectMemoryKey(ctx context.Context, name string) string {
if f.isNamespaced {
// FIXME: return error if namespace is not found
ns, _ := genericapirequest.NamespaceFrom(ctx)
return filepath.Join(ns, name+".json")
}
return filepath.Join(name + ".json")
}
func (f *memoryREST) objectDirName(ctx context.Context) string {
if f.isNamespaced {
// FIXME: return error if namespace is not found
ns, _ := genericapirequest.NamespaceFrom(ctx)
return filepath.Join(f.objRootPath, ns)
}
return f.objRootPath
}
func write(encoder runtime.Encoder, filepath string, obj runtime.Object) error {
buf := new(bytes.Buffer)
if err := encoder.Encode(obj, buf); err != nil {
return err
}
return os.WriteFile(filepath, buf.Bytes(), 0600)
}
func read(decoder runtime.Decoder, path string, newFunc func() runtime.Object) (runtime.Object, error) {
content, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return nil, err
}
newObj := newFunc()
decodedObj, _, err := decoder.Decode(content, nil, newObj)
if err != nil {
return nil, err
}
return decodedObj, nil
}
func exists(filepath string) bool {
_, err := os.Stat(filepath)
return err == nil
}
func ensureDir(dirname string) error {
if !exists(dirname) {
return os.MkdirAll(dirname, 0700)
}
return nil
}
func visitDir(dirname string, newFunc func() runtime.Object, codec runtime.Decoder, visitFunc func(string, runtime.Object)) error {
return filepath.Walk(dirname, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(info.Name(), ".json") {
return nil
}
newObj, err := read(codec, path, newFunc)
if err != nil {
return err
}
visitFunc(path, newObj)
return nil
})
}
func appendItem(v reflect.Value, obj runtime.Object) {
v.Set(reflect.Append(v, reflect.ValueOf(obj).Elem()))
}
func getListPrt(listObj runtime.Object) (reflect.Value, error) {
listPtr, err := meta.GetItemsPtr(listObj)
if err != nil {
return reflect.Value{}, err
}
v, err := conversion.EnforcePtr(listPtr)
if err != nil || v.Kind() != reflect.Slice {
return reflect.Value{}, fmt.Errorf("need ptr to slice: %v", err)
}
return v, nil
}
func (f *memoryREST) Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) {
jw := &jsonWatch{
id: len(f.watchers),
f: f,
ch: make(chan watch.Event, 10),
}
// On initial watch, send all the existing objects
list, err := f.List(ctx, options)
if err != nil {
return nil, err
}
danger := reflect.ValueOf(list).Elem()
items := danger.FieldByName("Items")
for i := 0; i < items.Len(); i++ {
obj := items.Index(i).Addr().Interface().(runtime.Object)
jw.ch <- watch.Event{
Type: watch.Added,
Object: obj,
}
}
f.muWatchers.Lock()
f.watchers[jw.id] = jw
f.muWatchers.Unlock()
return jw, nil
}
type jsonWatch struct {
f *memoryREST
id int
ch chan watch.Event
}
func (w *jsonWatch) Stop() {
w.f.muWatchers.Lock()
delete(w.f.watchers, w.id)
w.f.muWatchers.Unlock()
}
func (w *jsonWatch) ResultChan() <-chan watch.Event {
return w.ch
}
// TODO: implement custom table printer optionally
// func (f *memoryREST) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
// return &metav1.Table{}, nil
// }