-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
390 lines (332 loc) · 10.5 KB
/
store.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
package etcd
import (
"context"
"encoding/json"
"fmt"
"path"
"reflect"
"strings"
"github.com/coreos/etcd/clientv3"
"github.com/gogo/protobuf/proto"
"github.com/sensu/sensu-go/backend/store"
"github.com/sensu/sensu-go/types"
corev2 "github.com/sensu/sensu-go/api/core/v2"
)
const (
// EtcdRoot is the root of all sensu storage.
EtcdRoot = "/sensu.io"
)
// Store is an implementation of the sensu-go/backend/store.Store iface.
type Store struct {
client *clientv3.Client
keepalivesPath string
}
// NewStore creates a new Store.
func NewStore(client *clientv3.Client, name string) *Store {
store := &Store{
client: client,
keepalivesPath: path.Join(EtcdRoot, keepalivesPathPrefix, name),
}
return store
}
// Create the given key with the serialized object.
func Create(ctx context.Context, client *clientv3.Client, key, namespace string, object proto.Message) error {
bytes, err := proto.Marshal(object)
if err != nil {
return &store.ErrEncode{Key: key, Err: err}
}
comparisons := []clientv3.Cmp{}
// If we had a namespace provided, make sure it exists
if namespace != "" {
comparisons = append(comparisons, namespaceFound(namespace))
}
// Make sure the key does not exists
comparisons = append(comparisons, keyNotFound(key))
req := clientv3.OpPut(key, string(bytes))
resp, err := client.Txn(ctx).If(comparisons...).Then(req).Else(
getNamespace(namespace), getKey(key),
).Commit()
if err != nil {
return err
}
if !resp.Succeeded {
// Check if the namespace was missing
if namespace != "" && len(resp.Responses[0].GetResponseRange().Kvs) == 0 {
return &store.ErrNamespaceMissing{Namespace: namespace}
}
// Check if the key already exists
if len(resp.Responses[1].GetResponseRange().Kvs) != 0 {
return &store.ErrAlreadyExists{Key: key}
}
// Unknown error
return &store.ErrInternal{
Message: fmt.Sprintf("could not create the key %s", key),
}
}
return nil
}
// CreateOrUpdate writes the given key with the serialized object, regarless of
// its current existence
func CreateOrUpdate(ctx context.Context, client *clientv3.Client, key, namespace string, object interface{}) error {
var bytes []byte
var err error
switch object.(type) {
case types.Wrapper:
// Supporting protobuf serialization for wrapped resources is not
// straightforward since the types.Wrapper struct holds an interface. We
// will just use JSON encoding for now since the all store functions support
// both for decoding.
bytes, err = json.Marshal(object)
if err != nil {
return &store.ErrEncode{Key: key, Err: err}
}
default:
msg, ok := object.(proto.Message)
if !ok {
return &store.ErrEncode{Key: key, Err: fmt.Errorf("%T is not proto.Message", object)}
}
bytes, err = proto.Marshal(msg)
if err != nil {
return &store.ErrEncode{Key: key, Err: err}
}
}
comparisons := []clientv3.Cmp{}
// If we had a namespace provided, make sure it exists
if namespace != "" {
comparisons = append(comparisons, namespaceFound(namespace))
}
req := clientv3.OpPut(key, string(bytes))
resp, err := client.Txn(ctx).If(comparisons...).Then(req).Commit()
if err != nil {
return err
}
if !resp.Succeeded {
// Check if the namespace was missing
if namespace != "" && len(resp.Responses[0].GetResponseRange().Kvs) == 0 {
return &store.ErrNamespaceMissing{Namespace: namespace}
}
// Unknown error
return &store.ErrInternal{
Message: fmt.Sprintf("could not update the key %s", key),
}
}
return nil
}
// Delete the given key
func Delete(ctx context.Context, client *clientv3.Client, key string) error {
resp, err := client.Delete(ctx, key)
if err != nil {
return err
}
if resp.Deleted == 0 {
return &store.ErrNotFound{Key: key}
} else if resp.Deleted > 1 {
return &store.ErrInternal{
Message: fmt.Sprintf("expected to delete exactly 1 key, deleted %d", resp.Deleted),
}
}
return nil
}
// Get retrieves an object with the given key
func Get(ctx context.Context, client *clientv3.Client, key string, object interface{}) error {
// Fetch the key from the store
resp, err := client.Get(ctx, key, clientv3.WithLimit(1))
if err != nil {
return err
}
// Ensure we only received a single item
if len(resp.Kvs) == 0 {
return &store.ErrNotFound{Key: key}
}
// Deserialize the object to the given object
if err := unmarshal(resp.Kvs[0].Value, object); err != nil {
return &store.ErrDecode{Key: key, Err: err}
}
return nil
}
// KeyBuilderFn represents a generic key builder function
type KeyBuilderFn func(context.Context, string) string
// List retrieves all keys from storage under the provided prefix key, while
// supporting all namespaces, and deserialize it into objsPtr.
func List(ctx context.Context, client *clientv3.Client, keyBuilder KeyBuilderFn, objsPtr interface{}, pred *store.SelectionPredicate) error {
// Make sure the interface is a pointer, and that the element at this address
// is a slice.
v := reflect.ValueOf(objsPtr)
if v.Kind() != reflect.Ptr {
return fmt.Errorf("expected pointer, but got %v type", v.Type())
}
if v.Elem().Kind() != reflect.Slice {
return fmt.Errorf("expected slice, but got %s", v.Elem().Kind())
}
v = v.Elem()
opts := []clientv3.OpOption{
clientv3.WithLimit(pred.Limit),
}
keyPrefix := keyBuilder(ctx, "")
rangeEnd := clientv3.GetPrefixRangeEnd(keyPrefix)
opts = append(opts, clientv3.WithRange(rangeEnd))
key := keyPrefix
if pred.Continue != "" {
key = path.Join(keyPrefix, pred.Continue)
} else {
if !strings.HasSuffix(key, "/") {
key += "/"
}
}
resp, err := client.Get(ctx, key, opts...)
if err != nil {
return err
}
for _, kv := range resp.Kvs {
var obj interface{}
if len(kv.Value) > 0 && kv.Value[0] == '{' {
obj = reflect.New(v.Type().Elem().Elem()).Interface()
if err := json.Unmarshal(kv.Value, obj); err != nil {
return &store.ErrDecode{Key: key, Err: err}
}
} else {
msg := reflect.New(v.Type().Elem().Elem()).Interface().(proto.Message)
if err := proto.Unmarshal(kv.Value, msg); err != nil {
return &store.ErrDecode{Key: key, Err: err}
}
obj = msg
}
// Initialize the annotations and labels if they are nil
objValue := reflect.ValueOf(obj)
if objValue.Kind() == reflect.Ptr {
meta := objValue.Elem().FieldByName("ObjectMeta")
if meta.CanSet() {
if meta.FieldByName("Labels").Len() == 0 && meta.FieldByName("Labels").CanSet() {
meta.FieldByName("Labels").Set(reflect.MakeMap(reflect.TypeOf(make(map[string]string))))
}
if meta.FieldByName("Annotations").Len() == 0 && meta.FieldByName("Annotations").CanSet() {
meta.FieldByName("Annotations").Set(reflect.MakeMap(reflect.TypeOf(make(map[string]string))))
}
}
}
v.Set(reflect.Append(v, reflect.ValueOf(obj)))
}
if pred.Limit != 0 && resp.Count > pred.Limit {
lastObject := v.Index(v.Len() - 1).Interface().(corev2.Resource)
pred.Continue = ComputeContinueToken(ctx, lastObject)
} else {
pred.Continue = ""
}
return nil
}
// Update a key given with the serialized object.
func Update(ctx context.Context, client *clientv3.Client, key, namespace string, object proto.Message) error {
bytes, err := proto.Marshal(object)
if err != nil {
return &store.ErrEncode{Key: key, Err: err}
}
comparisons := []clientv3.Cmp{}
// If we had a namespace provided, make sure it exists
if namespace != "" {
comparisons = append(comparisons, namespaceFound(namespace))
}
// Make sure the key already exists
comparisons = append(comparisons, keyFound(key))
req := clientv3.OpPut(key, string(bytes))
resp, err := client.Txn(ctx).If(comparisons...).Then(req).Else(
getNamespace(namespace), getKey(key),
).Commit()
if err != nil {
return err
}
if !resp.Succeeded {
// Check if the namespace was missing
if namespace != "" && len(resp.Responses[0].GetResponseRange().Kvs) == 0 {
return &store.ErrNamespaceMissing{Namespace: namespace}
}
// Check if the key was missing
if len(resp.Responses[1].GetResponseRange().Kvs) == 0 {
return &store.ErrNotFound{Key: key}
}
// Unknown error
return &store.ErrInternal{
Message: fmt.Sprintf("could not update the key %s", key),
}
}
return nil
}
// Count retrieves the count of all keys from storage under the
// provided prefix key, while supporting all namespaces.
func Count(ctx context.Context, client *clientv3.Client, key string) (int64, error) {
opts := []clientv3.OpOption{
clientv3.WithCountOnly(),
clientv3.WithRange(clientv3.GetPrefixRangeEnd(key)),
}
resp, err := client.Get(ctx, key, opts...)
if err != nil {
return 0, err
}
return resp.Count, nil
}
func getKey(key string) clientv3.Op {
return clientv3.OpGet(key)
}
func getNamespace(namespace string) clientv3.Op {
return getKey(getNamespacePath(namespace))
}
func keyFound(key string) clientv3.Cmp {
return clientv3.Compare(
clientv3.CreateRevision(key), ">", 0,
)
}
func keyNotFound(key string) clientv3.Cmp {
return clientv3.Compare(
clientv3.CreateRevision(key), "=", 0,
)
}
func namespaceFound(namespace string) clientv3.Cmp {
return keyFound(getNamespacePath(namespace))
}
// ComputeContinueToken calculates a continue token based on the given resource
func ComputeContinueToken(ctx context.Context, r corev2.Resource) string {
queriedNamespace := store.NewNamespaceFromContext(ctx)
switch resource := r.(type) {
case *corev2.Event:
// TODO(ccressent): This can surely be simplified
if queriedNamespace == "" {
// Workaround for sensu-go#2465: keepalive events do not always have
// their namespace filled in, which would break the construction of
// continue token below. To accommodate for that, when
// constructing the continue token, whevener an event has a
// namespace of "" we construct the continue token using its
// entity's namespace instead.
eventNamespace := resource.Namespace
if eventNamespace == "" {
eventNamespace = resource.Entity.Namespace
}
return "/" + eventNamespace + "/" + resource.Entity.Name + "/" + resource.Check.Name + "\x00"
}
return resource.Entity.Name + "/" + resource.Check.Name + "\x00"
case *corev2.Namespace:
return resource.Name + "\x00"
case *corev2.User:
return resource.Username + "\x00"
default:
objMeta := r.GetObjectMeta()
if queriedNamespace == "" {
return path.Join(objMeta.Namespace, objMeta.Name) + "\x00"
}
return objMeta.Name + "\x00"
}
}
func unmarshal(data []byte, v interface{}) error {
if len(data) > 0 && data[0] == '{' {
if err := json.Unmarshal(data, v); err != nil {
return err
}
} else {
msg, ok := v.(proto.Message)
if !ok {
return fmt.Errorf("%T is not proto.Message", v)
}
if err := proto.Unmarshal(data, msg); err != nil {
return err
}
}
return nil
}