-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
generic.go
318 lines (269 loc) · 9.46 KB
/
generic.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
/*
Copyright 2023 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package generic
import (
"context"
"strings"
"time"
"github.com/gravitational/trace"
"github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/services"
)
// MarshalFunc is a type signature for a marshaling function.
type MarshalFunc[T types.Resource] func(T, ...services.MarshalOption) ([]byte, error)
// UnmarshalFunc is a type signature for an unmarshalling function.
type UnmarshalFunc[T types.Resource] func([]byte, ...services.MarshalOption) (T, error)
// ServiceConfig is the configuration for the service configuration.
type ServiceConfig[T types.Resource] struct {
Backend backend.Backend
ResourceKind string
PageLimit uint
BackendPrefix string
MarshalFunc MarshalFunc[T]
UnmarshalFunc UnmarshalFunc[T]
}
func (c *ServiceConfig[T]) CheckAndSetDefaults() error {
if c.Backend == nil {
return trace.BadParameter("backend is missing")
}
if c.ResourceKind == "" {
return trace.BadParameter("resource kind is missing")
}
// We should allow page limit to be 0 for services that don't use pagination. Some services are
// intended to be internally facing only, and those services may not need to set this limit.
if c.PageLimit == 0 {
c.PageLimit = defaults.DefaultChunkSize
}
if c.BackendPrefix == "" {
return trace.BadParameter("backend prefix is missing")
}
if c.MarshalFunc == nil {
return trace.BadParameter("marshal func is missing")
}
if c.UnmarshalFunc == nil {
return trace.BadParameter("unmarshal func is missing")
}
return nil
}
// Service is a generic service for interacting with resources in the backend.
type Service[T types.Resource] struct {
backend backend.Backend
resourceKind string
pageLimit uint
backendPrefix string
marshalFunc MarshalFunc[T]
unmarshalFunc UnmarshalFunc[T]
}
// NewService will return a new generic service with the given config. This will
// panic if the configuration is invalid.
func NewService[T types.Resource](cfg *ServiceConfig[T]) (*Service[T], error) {
if err := cfg.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return &Service[T]{
backend: cfg.Backend,
resourceKind: cfg.ResourceKind,
pageLimit: cfg.PageLimit,
backendPrefix: cfg.BackendPrefix,
marshalFunc: cfg.MarshalFunc,
unmarshalFunc: cfg.UnmarshalFunc,
}, nil
}
// WithPrefix will return a service with the given parts appended to the backend prefix.
func (s *Service[T]) WithPrefix(parts ...string) *Service[T] {
if len(parts) == 0 {
return s
}
return &Service[T]{
backend: s.backend,
resourceKind: s.resourceKind,
pageLimit: s.pageLimit,
backendPrefix: strings.Join(append([]string{s.backendPrefix}, parts...), string(backend.Separator)),
marshalFunc: s.marshalFunc,
unmarshalFunc: s.unmarshalFunc,
}
}
// GetResources returns a list of all resources.
func (s *Service[T]) GetResources(ctx context.Context) ([]T, error) {
rangeStart := backend.ExactKey(s.backendPrefix)
rangeEnd := backend.RangeEnd(rangeStart)
// no filter provided get the range directly
result, err := s.backend.GetRange(ctx, rangeStart, rangeEnd, backend.NoLimit)
if err != nil {
return nil, trace.Wrap(err)
}
out := make([]T, 0, len(result.Items))
for _, item := range result.Items {
resource, err := s.unmarshalFunc(item.Value, services.WithRevision(item.Revision))
if err != nil {
return nil, trace.Wrap(err)
}
out = append(out, resource)
}
return out, nil
}
// ListResources returns a paginated list of resources.
func (s *Service[T]) ListResources(ctx context.Context, pageSize int, pageToken string) ([]T, string, error) {
rangeStart := backend.Key(s.backendPrefix, pageToken)
rangeEnd := backend.RangeEnd(backend.ExactKey(s.backendPrefix))
// Adjust page size, so it can't be too large.
if pageSize <= 0 || pageSize > int(s.pageLimit) {
pageSize = int(s.pageLimit)
}
limit := pageSize + 1
// no filter provided get the range directly
result, err := s.backend.GetRange(ctx, rangeStart, rangeEnd, limit)
if err != nil {
return nil, "", trace.Wrap(err)
}
out := make([]T, 0, len(result.Items))
for _, item := range result.Items {
resource, err := s.unmarshalFunc(item.Value, services.WithRevision(item.Revision))
if err != nil {
return nil, "", trace.Wrap(err)
}
out = append(out, resource)
}
var nextKey string
if len(out) > pageSize {
nextKey = backend.GetPaginationKey(out[len(out)-1])
// Truncate the last item that was used to determine next row existence.
out = out[:pageSize]
}
return out, nextKey, nil
}
// GetResource returns the specified resource.
func (s *Service[T]) GetResource(ctx context.Context, name string) (resource T, err error) {
item, err := s.backend.Get(ctx, s.MakeKey(name))
if err != nil {
if trace.IsNotFound(err) {
return resource, trace.NotFound("%s %q doesn't exist", s.resourceKind, name)
}
return resource, trace.Wrap(err)
}
resource, err = s.unmarshalFunc(item.Value,
services.WithResourceID(item.ID), services.WithExpires(item.Expires), services.WithRevision(item.Revision))
return resource, trace.Wrap(err)
}
// CreateResource creates a new resource.
func (s *Service[T]) CreateResource(ctx context.Context, resource T) error {
item, err := s.MakeBackendItem(resource, resource.GetName())
if err != nil {
return trace.Wrap(err)
}
_, err = s.backend.Create(ctx, item)
if trace.IsAlreadyExists(err) {
return trace.AlreadyExists("%s %q already exists", s.resourceKind, resource.GetName())
}
return trace.Wrap(err)
}
// UpdateResource updates an existing resource.
func (s *Service[T]) UpdateResource(ctx context.Context, resource T) error {
item, err := s.MakeBackendItem(resource, resource.GetName())
if err != nil {
return trace.Wrap(err)
}
_, err = s.backend.Update(ctx, item)
if trace.IsNotFound(err) {
return trace.NotFound("%s %q doesn't exist", s.resourceKind, resource.GetName())
}
return trace.Wrap(err)
}
// UpsertResource upserts a resource.
func (s *Service[T]) UpsertResource(ctx context.Context, resource T) error {
item, err := s.MakeBackendItem(resource, resource.GetName())
if err != nil {
return trace.Wrap(err)
}
_, err = s.backend.Put(ctx, item)
return trace.Wrap(err)
}
// DeleteResource removes the specified resource.
func (s *Service[T]) DeleteResource(ctx context.Context, name string) error {
err := s.backend.Delete(ctx, s.MakeKey(name))
if err != nil {
if trace.IsNotFound(err) {
return trace.NotFound("%s %q doesn't exist", s.resourceKind, name)
}
return trace.Wrap(err)
}
return nil
}
// DeleteAllResources removes all resources.
func (s *Service[T]) DeleteAllResources(ctx context.Context) error {
startKey := backend.ExactKey(s.backendPrefix)
return trace.Wrap(s.backend.DeleteRange(ctx, startKey, backend.RangeEnd(startKey)))
}
// UpdateAndSwapResource will get the resource from the backend, modify it, and swap the new value into the backend.
func (s *Service[T]) UpdateAndSwapResource(ctx context.Context, name string, modify func(T) error) error {
existingItem, err := s.backend.Get(ctx, s.MakeKey(name))
if err != nil {
if trace.IsNotFound(err) {
return trace.NotFound("%s %q doesn't exist", s.resourceKind, name)
}
return trace.Wrap(err)
}
resource, err := s.unmarshalFunc(existingItem.Value,
services.WithResourceID(existingItem.ID), services.WithExpires(existingItem.Expires), services.WithRevision(existingItem.Revision))
if err != nil {
return trace.Wrap(err)
}
err = modify(resource)
if err != nil {
return trace.Wrap(err)
}
replacementItem, err := s.MakeBackendItem(resource, name)
if err != nil {
return trace.Wrap(err)
}
_, err = s.backend.CompareAndSwap(ctx, *existingItem, replacementItem)
return trace.Wrap(err)
}
// MakeBackendItem will check and make the backend item.
func (s *Service[T]) MakeBackendItem(resource T, name string) (backend.Item, error) {
if err := resource.CheckAndSetDefaults(); err != nil {
return backend.Item{}, trace.Wrap(err)
}
rev := resource.GetRevision()
value, err := s.marshalFunc(resource)
if err != nil {
return backend.Item{}, trace.Wrap(err)
}
item := backend.Item{
Key: s.MakeKey(name),
Value: value,
Expires: resource.Expiry(),
ID: resource.GetResourceID(),
Revision: rev,
}
return item, nil
}
// MakeKey will make a key for the service given a name.
func (s *Service[T]) MakeKey(name string) []byte {
return backend.Key(s.backendPrefix, name)
}
// RunWhileLocked will run the given function in a backend lock. This is a wrapper around the backend.RunWhileLocked function.
func (s *Service[T]) RunWhileLocked(ctx context.Context, lockName string, ttl time.Duration, fn func(context.Context, backend.Backend) error) error {
return trace.Wrap(backend.RunWhileLocked(ctx,
backend.RunWhileLockedConfig{
LockConfiguration: backend.LockConfiguration{
Backend: s.backend,
LockName: lockName,
TTL: ttl,
},
}, func(ctx context.Context) error {
return fn(ctx, s.backend)
}))
}