-
Notifications
You must be signed in to change notification settings - Fork 0
/
broker.go
389 lines (324 loc) · 10.6 KB
/
broker.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
package brokerapi
import (
"encoding/json"
"errors"
"net/http"
"code.cloudfoundry.org/lager"
xrv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/pivotal-cf/brokerapi/v8/domain"
"github.com/pivotal-cf/brokerapi/v8/domain/apiresponses"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/vshn/crossplane-service-broker/pkg/crossplane"
"github.com/vshn/crossplane-service-broker/pkg/reqcontext"
)
var (
// ErrServiceUpdateNotPermitted when updating an instance
ErrServiceUpdateNotPermitted = errors.New("service update not permitted")
// ErrPlanChangeNotPermitted when updating an instance's plan (only premium<->standard is permitted)
ErrPlanChangeNotPermitted = errors.New("plan change not permitted")
)
// Broker implements the service broker
type Broker struct {
cp *crossplane.Crossplane
planComparer crossplane.PlanUpdateChecker
}
// NewBroker sets up a new broker.
func NewBroker(cp *crossplane.Crossplane, pc crossplane.PlanUpdateChecker) *Broker {
return &Broker{
cp: cp,
planComparer: pc,
}
}
// Services retrieves registered services and plans.
func (b Broker) Services(rctx *reqcontext.ReqContext) ([]domain.Service, error) {
services := make([]domain.Service, 0)
xrds, err := b.cp.ServiceXRDs(rctx)
if err != nil {
return nil, err
}
for _, xrd := range xrds {
plans, err := b.servicePlans(rctx, []string{xrd.Labels.ServiceID})
if err != nil {
rctx.Logger.Error("plan retrieval failed", err, lager.Data{"serviceId": xrd.Labels.ServiceID})
}
services = append(services, newService(xrd, plans, rctx.Logger))
}
return services, nil
}
// servicePlans retrieves a combined view of services and their plans.
func (b Broker) servicePlans(rctx *reqcontext.ReqContext, serviceIDs []string) ([]domain.ServicePlan, error) {
plans := make([]domain.ServicePlan, 0)
compositions, err := b.cp.Plans(rctx, serviceIDs)
if err != nil {
return nil, err
}
for _, c := range compositions {
plans = append(plans, newServicePlan(c, rctx.Logger))
}
return plans, nil
}
// Provision creates a new service instance.
func (b Broker) Provision(rctx *reqcontext.ReqContext, instanceID, planID string, params json.RawMessage) (domain.ProvisionedServiceSpec, error) {
res := domain.ProvisionedServiceSpec{}
plan, err := b.cp.Plan(rctx, planID)
if err != nil {
return res, err
}
instance, exists, err := b.cp.Instance(rctx, instanceID, plan)
if err != nil {
return res, err
}
if exists {
// To avoid having to compare parameters,
// only instances without any parameters are considered to be equal to another (i.e. existing)
if params == nil {
res.AlreadyExists = true
return res, nil
}
return res, apiresponses.ErrInstanceAlreadyExists
}
ap := map[string]interface{}{}
if params != nil {
// ServiceBinderFactory is used out of convenience, however it seems the wrong approach here - might refactor later.
sb, err := crossplane.ServiceBinderFactory(b.cp, plan.Labels.ServiceName, instance, rctx.Logger)
if err != nil {
return res, err
}
if pv, ok := sb.(crossplane.ProvisionValidater); ok {
ap, err = pv.ValidateProvisionParams(rctx.Context, params)
if err != nil {
return res, apiresponses.NewFailureResponse(err, http.StatusBadRequest, "validate-provision-failed")
}
}
}
err = b.cp.CreateInstance(rctx, instanceID, plan, ap)
if err != nil {
return res, err
}
res.IsAsync = true
return res, nil
}
// Deprovision removes a provisioned instance.
func (b Broker) Deprovision(rctx *reqcontext.ReqContext, instanceID, planID string) (domain.DeprovisionServiceSpec, error) {
res := domain.DeprovisionServiceSpec{
IsAsync: false,
}
p, instance, err := b.getPlanInstance(rctx, planID, instanceID)
if err != nil {
return res, err
}
sb, err := crossplane.ServiceBinderFactory(b.cp, instance.Labels.ServiceName, instance, rctx.Logger)
if err != nil {
return res, err
}
if err := sb.Deprovisionable(rctx.Context); err != nil {
return res, err
}
if err := b.cp.DeleteInstance(rctx, instance.Composite.GetName(), p); err != nil {
return res, err
}
return res, nil
}
// Bind creates a binding between a provisioned service instance and an application.
func (b Broker) Bind(rctx *reqcontext.ReqContext, instanceID, bindingID, planID string, asyncAllowed bool) (domain.Binding, error) {
res := domain.Binding{
IsAsync: asyncAllowed,
}
_, instance, err := b.getPlanInstance(rctx, planID, instanceID)
if err != nil {
return res, err
}
if !instance.Ready() {
return res, apiresponses.ErrConcurrentInstanceAccess
}
sb, err := crossplane.ServiceBinderFactory(b.cp, instance.Labels.ServiceName, instance, rctx.Logger)
if err != nil {
return res, err
}
creds, err := sb.Bind(rctx.Context, bindingID)
if err != nil {
return res, err
}
res.Credentials = creds
return res, nil
}
// Unbind removes a binding.
func (b Broker) Unbind(rctx *reqcontext.ReqContext, instanceID, bindingID, planID string) (domain.UnbindSpec, error) {
res := domain.UnbindSpec{
IsAsync: false,
}
_, instance, err := b.getPlanInstance(rctx, planID, instanceID)
if err != nil {
return res, err
}
if !instance.Ready() {
return res, apiresponses.ErrConcurrentInstanceAccess
}
sb, err := crossplane.ServiceBinderFactory(b.cp, instance.Labels.ServiceName, instance, rctx.Logger)
if err != nil {
return res, err
}
err = sb.Unbind(rctx.Context, bindingID)
if err != nil {
if k8serrors.IsNotFound(err) {
return res, apiresponses.ErrBindingDoesNotExist
}
return res, err
}
return res, nil
}
// LastOperation retrieves an instance's status.
func (b Broker) LastOperation(rctx *reqcontext.ReqContext, instanceID, planID string) (domain.LastOperation, error) {
res := domain.LastOperation{}
_, instance, err := b.getPlanInstance(rctx, planID, instanceID)
if err != nil {
return res, err
}
condition := instance.Composite.GetCondition(xrv1.TypeReady)
res.Description = "Unknown"
if desc := string(condition.Reason); len(desc) > 0 {
res.Description = desc
}
res.State = domain.InProgress
switch condition.Reason {
case xrv1.ReasonAvailable:
res.State = domain.Succeeded
rctx.Logger.Info("provision-succeeded", lager.Data{"reason": condition.Reason, "message": condition.Message})
case xrv1.ReasonCreating:
res.State = domain.InProgress
rctx.Logger.Info("provision-in-progress", lager.Data{"reason": condition.Reason, "message": condition.Message})
case xrv1.ReasonUnavailable, xrv1.ReasonDeleting:
rctx.Logger.Info("provision-failed", lager.Data{"reason": condition.Reason, "message": condition.Message})
res.State = domain.Failed
}
return res, nil
}
// LastBindingOperation retrieves a binding's status.
func (b Broker) LastBindingOperation(rctx *reqcontext.ReqContext, instanceID, planID, bindingID string) (domain.LastOperation, error) {
res := domain.LastOperation{}
_, instance, err := b.getPlanInstance(rctx, planID, instanceID)
if err != nil {
return res, err
}
sb, err := crossplane.ServiceBinderFactory(b.cp, instance.Labels.ServiceName, instance, rctx.Logger)
if err != nil {
return res, err
}
_, err = sb.GetBinding(rctx.Context, bindingID)
if errors.Is(err, crossplane.ErrBindingNotReady) {
return domain.LastOperation{
State: domain.InProgress,
}, nil
}
if err != nil {
var apiErr *apiresponses.FailureResponse
if errors.As(err, &apiErr) {
return res, apiErr
}
return domain.LastOperation{
State: domain.Failed,
Description: err.Error(),
}, nil
}
return domain.LastOperation{
State: domain.Succeeded,
}, nil
}
// GetBinding retrieves a binding to get credentials.
func (b Broker) GetBinding(rctx *reqcontext.ReqContext, instanceID, bindingID string, details domain.FetchBindingDetails) (domain.GetBindingSpec, error) {
res := domain.GetBindingSpec{}
_, instance, err := b.getPlanInstance(rctx, details.PlanID, instanceID)
if err != nil {
return res, err
}
if !instance.Ready() {
return res, apiresponses.ErrConcurrentInstanceAccess
}
sb, err := crossplane.ServiceBinderFactory(b.cp, instance.Labels.ServiceName, instance, rctx.Logger)
if err != nil {
return res, err
}
creds, err := sb.GetBinding(rctx.Context, bindingID)
if err != nil {
return res, err
}
res.Credentials = creds
return res, nil
}
// GetInstance gets a provisioned instance.
func (b Broker) GetInstance(rctx *reqcontext.ReqContext, instanceID string, details domain.FetchInstanceDetails) (domain.GetInstanceDetailsSpec, error) {
res := domain.GetInstanceDetailsSpec{}
p, instance, err := b.getPlanInstance(rctx, details.PlanID, instanceID)
if err != nil {
return res, err
}
res.PlanID = p.Composition.GetName()
res.ServiceID = p.Labels.ServiceID
params := instance.Parameters()
if len(params) > 0 {
res.Parameters = params
}
return res, nil
}
// Update allows to change the SLA level from standard -> premium (and vice-versa).
func (b Broker) Update(rctx *reqcontext.ReqContext, instanceID, serviceID, oldPlanID, newPlanID string) (domain.UpdateServiceSpec, error) {
res := domain.UpdateServiceSpec{}
p, instance, err := b.getPlanInstance(rctx, oldPlanID, instanceID)
if err != nil {
return res, err
}
if instance.Labels.ServiceID != serviceID {
return res, ErrServiceUpdateNotPermitted
}
np, err := b.cp.Plan(rctx, newPlanID)
if err != nil {
return res, err
}
if !b.planComparer.AllowUpdate(*p, *np) {
rctx.Logger.Info("Plan change not permitted", lager.Data{
"old-plan-id": p.Labels.PlanName,
})
return res, ErrPlanChangeNotPermitted
}
instance.Composite.SetCompositionReference(&corev1.ObjectReference{
Name: np.Composition.GetName(),
})
instanceLabels := instance.Composite.GetLabels()
for _, l := range []string{
crossplane.PlanNameLabel,
crossplane.SLALabel,
} {
instanceLabels[l] = np.Composition.Labels[l]
}
instance.Composite.SetLabels(instanceLabels)
if err := b.cp.UpdateInstance(rctx, instance, np); err != nil {
return res, err
}
return res, nil
}
func (b Broker) getPlanInstance(rctx *reqcontext.ReqContext, planID, instanceID string) (*crossplane.Plan, *crossplane.Instance, error) {
if planID == "" {
rctx.Logger.Info("find-instance-without-plan", lager.Data{"instance-id": instanceID})
instance, p, exists, err := b.cp.FindInstanceWithoutPlan(rctx, instanceID)
if err != nil {
return nil, nil, err
}
if !exists {
return nil, nil, apiresponses.ErrInstanceDoesNotExist
}
return p, instance, nil
}
p, err := b.cp.Plan(rctx, planID)
if err != nil {
return nil, nil, err
}
instance, exists, err := b.cp.Instance(rctx, instanceID, p)
if err != nil {
return nil, nil, err
}
if !exists {
return nil, nil, apiresponses.ErrInstanceDoesNotExist
}
return p, instance, nil
}