-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcbt-rest.go
376 lines (330 loc) · 11.4 KB
/
cbt-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
package storage
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/ihcsim/cbt-aggapi/pkg/apis/cbt/v1alpha1"
cbtclient "github.com/ihcsim/cbt-aggapi/pkg/generated/cbt/clientset/versioned"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/watch"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
restregistry "k8s.io/apiserver/pkg/registry/rest"
"k8s.io/apiserver/pkg/storage"
"k8s.io/klog"
builderresource "sigs.k8s.io/apiserver-runtime/pkg/builder/resource"
builderrest "sigs.k8s.io/apiserver-runtime/pkg/builder/rest"
)
var _ rest.Connecter = &cbt{}
var _ rest.CreaterUpdater = &cbt{}
var _ rest.GracefulDeleter = &cbt{}
var _ rest.Watcher = &cbt{}
var _ rest.Lister = &cbt{}
var _ rest.Scoper = &cbt{}
// NewCustomStorage creates a new instance of a custom storage provider used
// to handle changed block entries.
func NewCustomStorage(
obj builderresource.Object,
clientset cbtclient.Interface,
etcdStorage storage.Interface,
) builderrest.ResourceHandlerProvider {
return func(s *runtime.Scheme, g genericregistry.RESTOptionsGetter) (restregistry.Storage, error) {
return &cbt{
clientset: clientset,
namespaceScoped: obj.NamespaceScoped(),
newFunc: obj.New,
newListFunc: obj.NewList,
etcd: etcdStorage,
TableConvertor: restregistry.NewDefaultTableConvertor(schema.GroupResource{
Group: obj.GetGroupVersionResource().Group,
Resource: obj.GetGroupVersionResource().Resource,
}),
}, nil
}
}
type cbt struct {
clientset cbtclient.Interface
namespaceScoped bool
newFunc func() runtime.Object
newListFunc func() runtime.Object
etcd storage.Interface
restregistry.TableConvertor
}
func (c *cbt) New() runtime.Object {
return c.newFunc()
}
// NewList returns an empty object that can be used with the List call.
// This object must be a pointer type for use with Codec.DecodeInto([]byte, runtime.Object)
func (c *cbt) NewList() runtime.Object {
return c.newListFunc()
}
// NamespaceScoped returns true if the storage is namespaced
func (c *cbt) NamespaceScoped() bool {
return c.namespaceScoped
}
// Connect returns an http.Handler that will handle the request/response for a given API invocation.
// The provided responder may be used for common API responses. The responder will write both status
// code and body, so the ServeHTTP method should exit after invoking the responder. The Handler will
// be used for a single API request and then discarded. The Responder is guaranteed to write to the
// same http.ResponseWriter passed to ServeHTTP.
func (c *cbt) Connect(ctx context.Context, id string, options runtime.Object, r restregistry.Responder) (http.Handler, error) {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
var (
result = v1alpha1.VolumeSnapshotDelta{}
getOpts = storage.GetOptions{}
)
// retrieve object from etcd
if err := c.etcd.Get(ctx, keyPath(id), getOpts, &result); err != nil {
status := http.StatusInternalServerError
if se, ok := err.(*storage.StorageError); ok && se.Code == storage.ErrCodeKeyNotFound {
status = http.StatusNotFound
err = se
}
http.Error(resp, fmt.Sprintf("can't find VolumeSnapshotDelta: %s", err), status)
return
}
klog.Infof("found VolumeSnapshotDelta: %s", id)
// parse query parameter options
opts, ok := options.(*v1alpha1.VolumeSnapshotDeltaOption)
if !ok {
http.Error(resp, "failed to parse VolumeSnapshotDeltaOptions", http.StatusInternalServerError)
return
}
if !opts.FetchCBD {
writeResponse(resp, result)
return
}
r, err := c.invokeCBTService(ctx)
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
defer r.Close()
raw, err := io.ReadAll(r)
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
var entries []*v1alpha1.ChangedBlockDelta
if err := json.Unmarshal(raw, &entries); err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
result.Status = v1alpha1.VolumeSnapshotDeltaStatus{
ChangedBlockDeltas: entries,
}
writeResponse(resp, result)
}), nil
}
func (c *cbt) invokeCBTService(ctx context.Context) (io.ReadCloser, error) {
obj, err := c.clientset.CbtV1alpha1().DriverDiscoveries().Get(ctx, "example.csi.k8s.io", metav1.GetOptions{})
if err != nil {
return nil, err
}
klog.Infof("discovered CSI driver: %s", obj.GetName())
endpoint := fmt.Sprintf("http://%s.%s:%d", obj.Spec.Service.Name, obj.Spec.Service.Namespace, obj.Spec.Service.Port)
res, err := http.Get(endpoint)
if err != nil {
return nil, err
}
return res.Body, nil
}
// NewConnectOptions returns an empty options object that will be used to pass
// options to the Connect method. If nil, then a nil options object is passed to
// Connect. It may return a bool and a string. If true, the value of the request
// path below the object will be included as the named string in the serialization
// of the runtime object.
func (c *cbt) NewConnectOptions() (runtime.Object, bool, string) {
return &v1alpha1.VolumeSnapshotDeltaOption{}, false, ""
}
// ConnectMethods returns the list of HTTP methods handled by Connect
func (c *cbt) ConnectMethods() []string {
return []string{"GET"}
}
// List selects resources in the storage which match to the selector. 'options' can be nil.
func (c *cbt) List(
ctx context.Context,
options *metainternalversion.ListOptions,
) (runtime.Object, error) {
labelSelector := options.LabelSelector
if labelSelector == nil {
labelSelector = labels.Everything()
}
fieldSelector := options.FieldSelector
if fieldSelector == nil {
fieldSelector = fields.Everything()
}
var (
list v1alpha1.VolumeSnapshotDeltaList
key = v1alpha1.SchemeGroupResource.Group
opts = storage.ListOptions{
ResourceVersion: options.ResourceVersion,
ResourceVersionMatch: options.ResourceVersionMatch,
Predicate: storage.SelectionPredicate{
Label: labelSelector,
Field: fieldSelector,
AllowWatchBookmarks: options.AllowWatchBookmarks,
Limit: options.Limit,
Continue: options.Continue,
IndexLabels: []string{},
IndexFields: []string{},
GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
return storage.DefaultNamespaceScopedAttr(obj)
},
},
}
)
if err := c.etcd.List(ctx, key, opts, &list); err != nil {
return nil, err
}
return &list, nil
}
// Create creates a new version of a resource.
func (c *cbt) Create(
ctx context.Context,
obj runtime.Object,
createValidation rest.ValidateObjectFunc,
options *metav1.CreateOptions) (runtime.Object, error) {
casted, ok := obj.(*v1alpha1.VolumeSnapshotDelta)
if !ok {
return nil, fmt.Errorf("")
}
casted.SetCreationTimestamp(metav1.Now())
var out v1alpha1.VolumeSnapshotDelta
if err := c.etcd.Create(ctx, keyPath(casted.GetName()), casted, &out, 0); err != nil {
if e, ok := err.(*storage.StorageError); ok {
if e.Code == storage.ErrCodeKeyExists {
klog.Infof("VolumeSnapshotDelta %s already exists", casted.GetName())
return casted, nil
}
}
return nil, err
}
klog.Infof("created VolumeSnapshotDelta: %s", out.GetName())
return &out, nil
}
// Update finds a resource in the storage and updates it. Some implementations
// may allow updates creates the object - they should set the created boolean
// to true.
func (c *cbt) Update(
ctx context.Context,
name string,
objInfo rest.UpdatedObjectInfo,
createValidation rest.ValidateObjectFunc,
updateValidation rest.ValidateObjectUpdateFunc,
forceAllowCreate bool,
options *metav1.UpdateOptions) (runtime.Object, bool, error) {
var (
updated v1alpha1.VolumeSnapshotDelta
preconditions *storage.Preconditions
)
if objInfo.Preconditions() != nil {
preconditions = &storage.Preconditions{
UID: objInfo.Preconditions().UID,
ResourceVersion: objInfo.Preconditions().ResourceVersion,
}
}
klog.Infof("updating VolumeSnapshotDelta: %s", name)
if err := c.etcd.GuaranteedUpdate(
ctx,
keyPath(name),
&updated,
true,
preconditions,
func(input runtime.Object, res storage.ResponseMeta) (runtime.Object, *uint64, error) {
newObj, err := objInfo.UpdatedObject(ctx, input)
return newObj, nil, err
},
nil); err != nil {
return nil, false, err
}
return &updated, false, nil
}
// Delete finds a resource in the storage and deletes it.
// The delete attempt is validated by the deleteValidation first.
// If options are provided, the resource will attempt to honor them or return an invalid
// request error.
// Although it can return an arbitrary error value, IsNotFound(err) is true for the
// returned error value err when the specified resource is not found.
// Delete *may* return the object that was deleted, or a status object indicating additional
// information about deletion.
// It also returns a boolean which is set to true if the resource was instantly
// deleted or false if it will be deleted asynchronously.
func (c *cbt) Delete(
ctx context.Context,
name string,
deleteValidation rest.ValidateObjectFunc,
options *metav1.DeleteOptions) (runtime.Object, bool, error) {
var (
out v1alpha1.VolumeSnapshotDelta
preconditions *storage.Preconditions
)
if options.Preconditions != nil {
preconditions = &storage.Preconditions{
UID: options.Preconditions.UID,
ResourceVersion: options.Preconditions.ResourceVersion,
}
}
klog.Infof("deleting VolumeSnapshotDelta: %s", name)
if err := c.etcd.Delete(
ctx,
keyPath(name),
&out,
preconditions,
func(ctx context.Context, obj runtime.Object) error {
return deleteValidation(ctx, obj)
},
nil); err != nil {
return nil, false, err
}
return &out, false, nil
}
// 'label' selects on labels; 'field' selects on the object's fields. Not all fields
// are supported; an error should be returned if 'field' tries to select on a field that
// isn't supported. 'resourceVersion' allows for continuing/starting a watch at a
// particular version.
func (c *cbt) Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) {
labelSelector := options.LabelSelector
if labelSelector == nil {
labelSelector = labels.Everything()
}
fieldSelector := options.FieldSelector
if fieldSelector == nil {
fieldSelector = fields.Everything()
}
opts := storage.ListOptions{
ResourceVersion: options.ResourceVersion,
ResourceVersionMatch: options.ResourceVersionMatch,
Predicate: storage.SelectionPredicate{
Label: labelSelector,
Field: fieldSelector,
Limit: options.Limit,
Continue: options.Continue,
AllowWatchBookmarks: options.AllowWatchBookmarks,
},
}
return c.etcd.Watch(ctx, v1alpha1.SchemeGroupResource.Group, opts)
}
func writeResponse(resp http.ResponseWriter, data interface{}) {
body, err := json.Marshal(data)
if err != nil {
http.Error(resp, err.Error(), http.StatusInternalServerError)
return
}
resp.Header().Set("Content-Type", "application/json")
resp.WriteHeader(http.StatusOK)
if _, err := resp.Write(body); err != nil {
klog.Error(err)
return
}
}
func keyPath(key string) string {
return fmt.Sprintf("%s/%s", v1alpha1.SchemeGroupVersion.Group, key)
}