This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
controller.go
480 lines (420 loc) · 15.8 KB
/
controller.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package servicemanager
import (
"context"
"encoding/json"
"reflect"
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/vmware/dispatch/pkg/controller"
entitystore "github.com/vmware/dispatch/pkg/entity-store"
"github.com/vmware/dispatch/pkg/service-manager/clients"
"github.com/vmware/dispatch/pkg/service-manager/entities"
"github.com/vmware/dispatch/pkg/trace"
)
var serviceClassOrganizationID = "___global___"
// ControllerConfig defines the image manager controller configuration
type ControllerConfig struct {
ResyncPeriod time.Duration
ZookeeperLocation string
}
type serviceClassEntityHandler struct {
Store entitystore.EntityStore
BrokerClient clients.BrokerClient
}
// Type returns the type of the entity associated to this handler
func (h *serviceClassEntityHandler) Type() reflect.Type {
return reflect.TypeOf(&entities.ServiceClass{})
}
// Add creates new service class entities (will change once users fully manage services)
func (h *serviceClassEntityHandler) Add(ctx context.Context, obj entitystore.Entity) (err error) {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
_, err = h.Store.Add(ctx, obj)
return
}
// Update updates service class entities
func (h *serviceClassEntityHandler) Update(ctx context.Context, obj entitystore.Entity) error {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
sc := obj.(*entities.ServiceClass)
_, err := h.Store.Update(ctx, sc.Revision, sc)
return err
}
// Delete removes service class entities
func (h *serviceClassEntityHandler) Delete(ctx context.Context, obj entitystore.Entity) error {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
var deleted entities.ServiceClass
err := h.Store.Delete(ctx, obj.GetOrganizationID(), obj.GetName(), &deleted)
if err != nil {
err = errors.Wrapf(err, "error deleting service class entity %s/%s", obj.GetOrganizationID(), obj.GetName())
log.Error(err)
return err
}
return nil
}
func (h *serviceClassEntityHandler) needsUpdate(actual *entities.ServiceClass, existing *entities.ServiceClass) (*entities.ServiceClass, bool) {
if actual.Status == entitystore.StatusUNKNOWN {
return nil, false
}
// Keys are sorted, so encoding should produce a comparable result
actualJSON, _ := json.Marshal(actual.Plans)
existingJSON, _ := json.Marshal(existing.Plans)
if string(actualJSON) != string(existingJSON) ||
actual.Status != existing.Status ||
actual.Bindable != existing.Bindable {
existing.Status = actual.Status
existing.Bindable = actual.Bindable
existing.Plans = actual.Plans
return existing, true
}
return nil, false
}
// Sync reconciles the actual state from the service catalog with the dispatch state
func (h *serviceClassEntityHandler) Sync(ctx context.Context, resyncPeriod time.Duration) ([]entitystore.Entity, error) {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
classes, err := h.BrokerClient.ListServiceClasses()
if err != nil {
return nil, err
}
// actualMap maps serviceIDs (OSBAPI service IDs) to service class entities. These entities represent current state.
actualMap := make(map[string]*entities.ServiceClass)
for _, class := range classes {
sc := class.(*entities.ServiceClass)
actualMap[sc.ServiceID] = sc
}
var existing []*entities.ServiceClass
err = h.Store.List(ctx, serviceClassOrganizationID, entitystore.Options{}, &existing)
if err != nil {
return nil, errors.Wrap(err, "Sync error listing existing service classes")
}
var synced []entitystore.Entity
// Update any service classes which have been removed. This is necessary since we are not directly managing the
// service classes at this time. We are simply reflecting the current state.
for _, class := range existing {
actual, ok := actualMap[class.ServiceID]
if !ok {
class.SetDelete(true)
class.SetStatus(entitystore.StatusDELETING)
} else {
delete(actualMap, class.ServiceID)
var ok bool
class, ok = h.needsUpdate(actual, class)
if !ok {
continue
}
}
synced = append(synced, class)
}
// Add any service classes which don't exist in the database
for _, class := range actualMap {
class.OrganizationID = serviceClassOrganizationID
err := h.Add(ctx, class)
if err != nil {
return nil, err
}
}
return synced, err
}
// Error handles service class entities in the error state
func (h *serviceClassEntityHandler) Error(ctx context.Context, obj entitystore.Entity) error {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
_, err := h.Store.Update(ctx, obj.GetRevision(), obj)
return err
}
type serviceInstanceEntityHandler struct {
Store entitystore.EntityStore
BrokerClient clients.BrokerClient
}
// Type returns the type of the entity associated to this handler
func (h *serviceInstanceEntityHandler) Type() reflect.Type {
return reflect.TypeOf(&entities.ServiceInstance{})
}
// Add creates new service instance on the kubernetes service catalog according to the plan
// and parameters configured in the service instance entity. Additionally, we create a binding
// and secrets which can be used by functions
func (h *serviceInstanceEntityHandler) Add(ctx context.Context, obj entitystore.Entity) (err error) {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
si := obj.(*entities.ServiceInstance)
var sc entities.ServiceClass
if err = h.Store.Get(ctx, serviceClassOrganizationID, si.ServiceClass, entitystore.Options{}, &sc); err != nil {
return
}
defer func() { h.Store.UpdateWithError(ctx, si, err) }()
if err = h.BrokerClient.CreateService(&sc, si); err != nil {
return
}
return
}
// Update updates service instance entities
func (h *serviceInstanceEntityHandler) Update(ctx context.Context, obj entitystore.Entity) error {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
si := obj.(*entities.ServiceInstance)
_, err := h.Store.Update(ctx, si.GetRevision(), si)
return err
}
// Delete deletes service instance entities
func (h *serviceInstanceEntityHandler) Delete(ctx context.Context, obj entitystore.Entity) error {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
si := obj.(*entities.ServiceInstance)
var b entities.ServiceBinding
found, err := h.Store.Find(ctx, si.GetOrganizationID(), si.GetID(), entitystore.Options{}, &b)
if found {
log.Debugf("waiting to delete service instance %s, binding still exists", si.GetID())
return nil
}
err = h.BrokerClient.DeleteService(si)
if err != nil {
log.Error(err)
}
// TODO (bjung): We really shoudn't actually delete the entity until the the resource
// is actually deleted. As-is it works, but we are repeatedly calling delete as the controller
// thinks the resource has been orphaned (which it has)
var deleted entities.ServiceInstance
err = h.Store.Delete(ctx, si.GetOrganizationID(), si.GetName(), &deleted)
if err != nil {
err = errors.Wrapf(err, "error deleting service instance entity %s/%s", si.GetOrganizationID(), si.GetName())
log.Error(err)
return err
}
return nil
}
// Sync reconsiles the actual state from the service catalog with the dispatch state
func (h *serviceInstanceEntityHandler) Sync(ctx context.Context, resyncPeriod time.Duration) ([]entitystore.Entity, error) {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
instances, err := h.BrokerClient.ListServiceInstances()
if err != nil {
return nil, err
}
actualMap := make(map[string]*entities.ServiceInstance)
for _, instance := range instances {
si := instance.(*entities.ServiceInstance)
actualMap[si.ID] = si
}
var existing []*entities.ServiceInstance
err = h.Store.ListGlobal(ctx, entitystore.Options{}, &existing)
if err != nil {
return nil, errors.Wrap(err, "Sync error listing existing service instances")
}
var synced []entitystore.Entity
// Update any service instances which have been removed
for _, instance := range existing {
log.Debugf("Processing service instance %s [%s]", instance.Name, instance.Status)
if instance.Status == entitystore.StatusINITIALIZED {
// Hasn't been created yet, so let's do that.
synced = append(synced, instance)
continue
}
if instance.Delete {
// Marked for deletion... ignore actual status - though we need to start tracking
// actual state separately from desired stated (i.e. marked for delete, but is currently
// in ready state)
delete(actualMap, instance.ID)
synced = append(synced, instance)
continue
}
actual, ok := actualMap[instance.ID]
if !ok {
instance.SetDelete(true)
instance.SetStatus(entitystore.StatusDELETING)
log.Debugf("Setting service instance %s for deletion", instance.Name)
synced = append(synced, instance)
continue
} else {
delete(actualMap, instance.ID)
}
if actual.Status == entitystore.StatusUNKNOWN || actual.Status == instance.Status {
// If status is unknown or hasn't changed, no need to update
continue
}
instance.SetStatus(actual.Status)
if instance.Status != entitystore.StatusERROR {
instance.Reason = nil
} else {
instance.SetReason(actual.Reason)
}
log.Debugf("Syncing instance %s with status %s", instance.Name, instance.Status)
synced = append(synced, instance)
}
// Clean up any orphaned bindings
for _, s := range actualMap {
s.SetDelete(true)
s.SetStatus(entitystore.StatusDELETING)
synced = append(synced, s)
}
return synced, err
}
// Error handles service class entities in the error state
func (h *serviceInstanceEntityHandler) Error(ctx context.Context, obj entitystore.Entity) error {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
_, err := h.Store.Update(ctx, obj.GetRevision(), obj)
return err
}
type serviceBindingEntityHandler struct {
Store entitystore.EntityStore
BrokerClient clients.BrokerClient
}
// Type returns the type of the entity associated to this handler
func (h *serviceBindingEntityHandler) Type() reflect.Type {
return reflect.TypeOf(&entities.ServiceBinding{})
}
// Add creates new service class entities (will change once users fully manage services)
func (h *serviceBindingEntityHandler) Add(ctx context.Context, obj entitystore.Entity) (err error) {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
b := obj.(*entities.ServiceBinding)
var si entities.ServiceInstance
log.Debugf("Fetching service for name %s", b.ServiceInstance)
if err = h.Store.Get(ctx, b.OrganizationID, b.ServiceInstance, entitystore.Options{}, &si); err != nil {
return
}
if si.Status != entitystore.StatusREADY {
log.Debugf("Service %s not ready for binding %s", si.Name, si.Status)
return
}
defer func() { h.Store.UpdateWithError(ctx, b, err) }()
err = h.BrokerClient.CreateBinding(&si, b)
return
}
// Update updates service class entities
func (h *serviceBindingEntityHandler) Update(ctx context.Context, obj entitystore.Entity) error {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
_, err := h.Store.Update(ctx, obj.GetRevision(), obj)
return err
}
// Delete removes service binding entities
func (h *serviceBindingEntityHandler) Delete(ctx context.Context, obj entitystore.Entity) error {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
b := obj.(*entities.ServiceBinding)
log.Debugf("Deleting service binding %s", b.Name)
err := h.BrokerClient.DeleteBinding(b)
if err != nil {
log.Error(err)
return err
}
// TODO (bjung): We really shoudn't actually delete the entity until the the resource
// is actually deleted. As-is it works, but we are repeatedly calling delete as the controller
// thinks the resource has been orphaned (which it has)
var deleted entities.ServiceBinding
err = h.Store.Delete(ctx, obj.GetOrganizationID(), obj.GetName(), &deleted)
if err != nil {
err = errors.Wrapf(err, "error deleting service binding entity %s [%s]", b.Name, b.BindingID)
log.Error(err)
return err
}
return nil
}
// Sync reconciles the actual state from the service catalog with the dispatch state
func (h *serviceBindingEntityHandler) Sync(ctx context.Context, resyncPeriod time.Duration) ([]entitystore.Entity, error) {
span, ctx := trace.Trace(ctx, "")
defer span.Finish()
bindings, err := h.BrokerClient.ListServiceBindings()
actualMap := make(map[string]*entities.ServiceBinding)
for _, binding := range bindings {
b := binding.(*entities.ServiceBinding)
actualMap[b.BindingID] = b
}
var existing []*entities.ServiceBinding
err = h.Store.ListGlobal(ctx, entitystore.Options{}, &existing)
if err != nil {
return nil, errors.Wrap(err, "Sync error listing existing service bindings")
}
var existingServices []*entities.ServiceInstance
err = h.Store.ListGlobal(ctx, entitystore.Options{}, &existingServices)
if err != nil {
return nil, errors.Wrap(err, "Sync error listing existing services")
}
serviceMap := make(map[string]*entities.ServiceInstance)
for _, service := range existingServices {
serviceMap[service.Name] = service
}
var synced []entitystore.Entity
for _, binding := range existing {
log.Debugf("Processing service binding %s [%s]", binding.Name, binding.Status)
if _, ok := serviceMap[binding.ServiceInstance]; !ok {
log.Infof("Service instance for binding %s [%s] missing, delete", binding.Name, binding.ServiceInstance)
// No matching service exists... delete
binding.SetDelete(true)
binding.SetStatus(entitystore.StatusDELETING)
synced = append(synced, binding)
continue
}
if binding.Status == entitystore.StatusINITIALIZED {
// Hasn't been created yet, so let's do that.
synced = append(synced, binding)
continue
}
if binding.Delete {
// Marked for deletion... ignore actual status - though we need to start tracking
// actual state separately from desired stated (i.e. marked for delete, but is currently
// in ready state)
delete(actualMap, binding.BindingID)
synced = append(synced, binding)
continue
}
actual, ok := actualMap[binding.BindingID]
// If binding isn't present... recreate
if !ok {
log.Infof("Binding %s [%s] missing from service catalog, recreate", binding.Name, binding.ServiceInstance)
if !binding.GetDelete() {
// Since we are recreating, it's not really orphaned.
delete(actualMap, binding.BindingID)
binding.SetStatus(entitystore.StatusINITIALIZED)
}
synced = append(synced, binding)
continue
} else {
delete(actualMap, binding.BindingID)
}
if actual.Status == entitystore.StatusUNKNOWN || actual.Status == binding.Status {
// If status is unknown or hasn't changed, no need to update
continue
}
binding.SetStatus(actual.Status)
if binding.Status != entitystore.StatusERROR {
binding.Reason = nil
} else {
binding.SetReason(actual.Reason)
}
synced = append(synced, binding)
}
// Clean up any orphaned bindings
for _, b := range actualMap {
log.Infof("Binding %s [%s] orphaned from entity store, delete", b.Name, b.ServiceInstance)
b.SetDelete(true)
b.SetStatus(entitystore.StatusDELETING)
synced = append(synced, b)
}
return synced, nil
}
// Error handles service class entities in the error state
func (h *serviceBindingEntityHandler) Error(ctx context.Context, obj entitystore.Entity) error {
_, err := h.Store.Update(ctx, obj.GetRevision(), obj)
return err
}
// NewController creates a new service manager controller
func NewController(config *ControllerConfig, store entitystore.EntityStore, brokerClient clients.BrokerClient) controller.Controller {
c := controller.NewController(controller.Options{
ResyncPeriod: config.ResyncPeriod,
Workers: 10, // want more functions concurrently? add more workers // TODO configure workers
ZookeeperLocation: config.ZookeeperLocation,
})
c.AddEntityHandler(&serviceClassEntityHandler{Store: store, BrokerClient: brokerClient})
c.AddEntityHandler(&serviceInstanceEntityHandler{Store: store, BrokerClient: brokerClient})
c.AddEntityHandler(&serviceBindingEntityHandler{Store: store, BrokerClient: brokerClient})
return c
}