forked from kyma-project/control-plane
/
operation.go
390 lines (350 loc) · 12.9 KB
/
operation.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 postsql
import (
"encoding/json"
"fmt"
"time"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/storage/dbsession/dbmodel"
"github.com/pivotal-cf/brokerapi/v7/domain"
log "github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/storage/dberr"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/storage/dbsession"
"github.com/pkg/errors"
)
type operations struct {
dbsession.Factory
}
func NewOperation(sess dbsession.Factory) *operations {
return &operations{
Factory: sess,
}
}
// InsertProvisioningOperation insert new operation
func (s *operations) InsertProvisioningOperation(operation internal.ProvisioningOperation) error {
session := s.NewWriteSession()
dto, err := provisioningOperationToDTO(&operation)
if err != nil {
return errors.Wrapf(err, "while inserting provisioning operation (id: %s)", operation.ID)
}
var lastErr error
_ = wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
lastErr = session.InsertOperation(dto)
if lastErr != nil {
log.Warn(errors.Wrap(err, "while insert operation"))
return false, nil
}
return true, nil
})
return lastErr
}
// GetProvisioningOperationByID fetches the ProvisioningOperation by given ID, returns error if not found
func (s *operations) GetProvisioningOperationByID(operationID string) (*internal.ProvisioningOperation, error) {
session := s.NewReadSession()
operation := dbmodel.OperationDTO{}
var lastErr error
err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
operation, lastErr = session.GetOperationByID(operationID)
if lastErr != nil {
if dberr.IsNotFound(lastErr) {
lastErr = dberr.NotFound("Operation with id %s not exist", operationID)
return false, lastErr
}
log.Warn(errors.Wrapf(lastErr, "while reading Operation from the storage"))
return false, nil
}
return true, nil
})
if err != nil {
return nil, errors.Wrap(err, "while getting operation by ID")
}
ret, err := toProvisioningOperation(&operation)
if err != nil {
return nil, errors.Wrapf(err, "while converting DTO to Operation")
}
return ret, nil
}
// GetProvisioningOperationByInstanceID fetches the ProvisioningOperation by given instanceID, returns error if not found
func (s *operations) GetProvisioningOperationByInstanceID(instanceID string) (*internal.ProvisioningOperation, error) {
session := s.NewReadSession()
operation := dbmodel.OperationDTO{}
var lastErr dberr.Error
err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
operation, lastErr = session.GetOperationByTypeAndInstanceID(instanceID, dbmodel.OperationTypeProvision)
if lastErr != nil {
if dberr.IsNotFound(lastErr) {
lastErr = dberr.NotFound("operation does not exist")
return false, lastErr
}
log.Warn(errors.Wrapf(lastErr, "while reading Operation from the storage").Error())
return false, nil
}
return true, nil
})
if err != nil {
return nil, lastErr
}
ret, err := toProvisioningOperation(&operation)
if err != nil {
return nil, errors.Wrapf(err, "while converting DTO to Operation")
}
return ret, nil
}
// UpdateProvisioningOperation updates ProvisioningOperation, fails if not exists or optimistic locking failure occurs.
func (s *operations) UpdateProvisioningOperation(op internal.ProvisioningOperation) (*internal.ProvisioningOperation, error) {
session := s.NewWriteSession()
op.UpdatedAt = time.Now()
dto, err := provisioningOperationToDTO(&op)
if err != nil {
return nil, errors.Wrapf(err, "while converting Operation to DTO")
}
var lastErr error
_ = wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
lastErr = session.UpdateOperation(dto)
if lastErr != nil && dberr.IsNotFound(lastErr) {
_, lastErr = s.NewReadSession().GetOperationByID(op.ID)
if lastErr != nil {
log.Warn(errors.Wrapf(lastErr, "while getting Operation").Error())
return false, nil
}
// the operation exists but the version is different
lastErr = dberr.Conflict("operation update conflict, operation ID: %s", op.ID)
log.Warn(lastErr.Error())
return false, lastErr
}
return true, nil
})
op.Version = op.Version + 1
return &op, lastErr
}
// InsertProvisioningOperation insert new operation
func (s *operations) InsertDeprovisioningOperation(operation internal.DeprovisioningOperation) error {
session := s.NewWriteSession()
dto, err := deprovisioningOperationToDTO(&operation)
if err != nil {
return errors.Wrapf(err, "while converting Operation to DTO")
}
var lastErr error
_ = wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
lastErr = session.InsertOperation(dto)
if lastErr != nil {
log.Warn(errors.Wrap(err, "while insert operation"))
return false, nil
}
return true, nil
})
return lastErr
}
// GetProvisioningOperationByID fetches the ProvisioningOperation by given ID, returns error if not found
func (s *operations) GetDeprovisioningOperationByID(operationID string) (*internal.DeprovisioningOperation, error) {
session := s.NewReadSession()
operation := dbmodel.OperationDTO{}
var lastErr error
err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
operation, lastErr = session.GetOperationByID(operationID)
if lastErr != nil {
if dberr.IsNotFound(lastErr) {
lastErr = dberr.NotFound("Operation with id %s not exist", operationID)
return false, lastErr
}
log.Warn(errors.Wrapf(lastErr, "while reading Operation from the storage"))
return false, nil
}
return true, nil
})
if err != nil {
return nil, errors.Wrap(err, "while getting operation by ID")
}
ret, err := toDeprovisioningOperation(&operation)
if err != nil {
return nil, errors.Wrapf(err, "while converting DTO to Operation")
}
return ret, nil
}
// GetProvisioningOperationByInstanceID fetches the ProvisioningOperation by given instanceID, returns error if not found
func (s *operations) GetDeprovisioningOperationByInstanceID(instanceID string) (*internal.DeprovisioningOperation, error) {
session := s.NewReadSession()
operation := dbmodel.OperationDTO{}
var lastErr dberr.Error
err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
operation, lastErr = session.GetOperationByTypeAndInstanceID(instanceID, dbmodel.OperationTypeDeprovision)
if lastErr != nil {
if dberr.IsNotFound(lastErr) {
lastErr = dberr.NotFound("operation does not exist")
return false, lastErr
}
log.Warn(errors.Wrapf(lastErr, "while reading Operation from the storage").Error())
return false, nil
}
return true, nil
})
if err != nil {
return nil, lastErr
}
ret, err := toDeprovisioningOperation(&operation)
if err != nil {
return nil, errors.Wrapf(err, "while converting DTO to Operation")
}
return ret, nil
}
// UpdateProvisioningOperation updates ProvisioningOperation, fails if not exists or optimistic locking failure occurs.
func (s *operations) UpdateDeprovisioningOperation(operation internal.DeprovisioningOperation) (*internal.DeprovisioningOperation, error) {
session := s.NewWriteSession()
operation.UpdatedAt = time.Now()
dto, err := deprovisioningOperationToDTO(&operation)
if err != nil {
return nil, errors.Wrapf(err, "while converting Operation to DTO")
}
var lastErr error
_ = wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
lastErr = session.UpdateOperation(dto)
if lastErr != nil && dberr.IsNotFound(lastErr) {
_, lastErr = s.NewReadSession().GetOperationByID(operation.ID)
if lastErr != nil {
log.Warn(errors.Wrapf(lastErr, "while getting Operation").Error())
return false, nil
}
// the operation exists but the version is different
lastErr = dberr.Conflict("operation update conflict, operation ID: %s", operation.ID)
log.Warn(lastErr.Error())
return false, lastErr
}
return true, nil
})
operation.Version = operation.Version + 1
return &operation, lastErr
}
// GetOperationByID returns Operation with given ID. Returns an error if the operation does not exists.
func (s *operations) GetOperationByID(operationID string) (*internal.Operation, error) {
session := s.NewReadSession()
operation := dbmodel.OperationDTO{}
var lastErr dberr.Error
err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
operation, lastErr = session.GetOperationByID(operationID)
if lastErr != nil {
if dberr.IsNotFound(lastErr) {
lastErr = dberr.NotFound("Operation with id %s not exist", operationID)
return false, lastErr
}
log.Warn(errors.Wrapf(lastErr, "while reading Operation from the storage").Error())
return false, nil
}
return true, nil
})
if err != nil {
return nil, lastErr
}
op := toOperation(&operation)
return &op, nil
}
func (s *operations) GetOperationsInProgressByType(operationType dbmodel.OperationType) ([]internal.Operation, error) {
session := s.NewReadSession()
operations := make([]dbmodel.OperationDTO, 0)
err := wait.PollImmediate(defaultRetryInterval, defaultRetryTimeout, func() (bool, error) {
dto, err := session.GetOperationsInProgressByType(operationType)
if err != nil {
log.Warn(errors.Wrapf(err, "while getting Operations from the storage").Error())
return false, nil
}
operations = dto
return true, nil
})
if err != nil {
return nil, err
}
return toOperations(operations), nil
}
func (s *operations) GetOperationStats() (internal.OperationStats, error) {
entries, err := s.NewReadSession().GetOperationStats()
if err != nil {
return internal.OperationStats{}, err
}
result := internal.OperationStats{
Provisioning: make(map[domain.LastOperationState]int),
Deprovisioning: make(map[domain.LastOperationState]int),
}
for _, e := range entries {
switch dbmodel.OperationType(e.Type) {
case dbmodel.OperationTypeProvision:
result.Provisioning[domain.LastOperationState(e.State)] = e.Total
case dbmodel.OperationTypeDeprovision:
result.Deprovisioning[domain.LastOperationState(e.State)] = e.Total
}
}
return result, nil
}
func toOperation(op *dbmodel.OperationDTO) internal.Operation {
return internal.Operation{
ID: op.ID,
CreatedAt: op.CreatedAt,
UpdatedAt: op.UpdatedAt,
ProvisionerOperationID: op.TargetOperationID,
State: domain.LastOperationState(op.State),
InstanceID: op.InstanceID,
Description: op.Description,
Version: op.Version,
}
}
func toOperations(op []dbmodel.OperationDTO) []internal.Operation {
operations := make([]internal.Operation, 0)
for _, o := range op {
operations = append(operations, toOperation(&o))
}
return operations
}
func toProvisioningOperation(op *dbmodel.OperationDTO) (*internal.ProvisioningOperation, error) {
if op.Type != dbmodel.OperationTypeProvision {
return nil, errors.New(fmt.Sprintf("expected operation type Provisioning, but was %s", op.Type))
}
var operation internal.ProvisioningOperation
err := json.Unmarshal([]byte(op.Data), &operation)
if err != nil {
return nil, errors.New("unable to unmarshall provisioning data")
}
operation.Operation = toOperation(op)
return &operation, nil
}
func provisioningOperationToDTO(op *internal.ProvisioningOperation) (dbmodel.OperationDTO, error) {
serialized, err := json.Marshal(op)
if err != nil {
return dbmodel.OperationDTO{}, errors.Wrapf(err, "while serializing provisioning data %v", op)
}
ret := operationToDB(&op.Operation)
ret.Data = string(serialized)
ret.Type = dbmodel.OperationTypeProvision
return ret, nil
}
func toDeprovisioningOperation(op *dbmodel.OperationDTO) (*internal.DeprovisioningOperation, error) {
if op.Type != dbmodel.OperationTypeDeprovision {
return nil, errors.New(fmt.Sprintf("expected operation type Provisioning, but was %s", op.Type))
}
var operation internal.DeprovisioningOperation
err := json.Unmarshal([]byte(op.Data), &operation)
if err != nil {
return nil, errors.New("unable to unmarshall provisioning data")
}
operation.Operation = toOperation(op)
return &operation, nil
}
func deprovisioningOperationToDTO(op *internal.DeprovisioningOperation) (dbmodel.OperationDTO, error) {
serialized, err := json.Marshal(op)
if err != nil {
return dbmodel.OperationDTO{}, errors.Wrapf(err, "while serializing deprovisioning data %v", op)
}
ret := operationToDB(&op.Operation)
ret.Data = string(serialized)
ret.Type = dbmodel.OperationTypeDeprovision
return ret, nil
}
func operationToDB(op *internal.Operation) dbmodel.OperationDTO {
return dbmodel.OperationDTO{
ID: op.ID,
TargetOperationID: op.ProvisionerOperationID,
State: string(op.State),
Description: op.Description,
UpdatedAt: op.UpdatedAt,
CreatedAt: op.CreatedAt,
Version: op.Version,
InstanceID: op.InstanceID,
}
}