-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.go
More file actions
1262 lines (1067 loc) · 33.5 KB
/
engine.go
File metadata and controls
1262 lines (1067 loc) · 33.5 KB
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package provision
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"time"
"github.com/cenkalti/backoff/v3"
"github.com/hashicorp/go-retryablehttp"
"github.com/joncrlsn/dque"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/zosbase/pkg"
"github.com/threefoldtech/zosbase/pkg/environment"
"github.com/threefoldtech/zosbase/pkg/gridtypes"
"github.com/threefoldtech/zosbase/pkg/gridtypes/zos"
"github.com/threefoldtech/zosbase/pkg/stubs"
)
// EngineOption interface
type EngineOption interface {
apply(e *NativeEngine)
}
// WithTwins sets the user key getter on the
// engine
func WithTwins(g Twins) EngineOption {
return &withUserKeyGetter{g}
}
// WithAdmins sets the admins key getter on the
// engine
func WithAdmins(g Twins) EngineOption {
return &withAdminsKeyGetter{g}
}
// WithStartupOrder forces a specific startup order of types
// any type that is not listed in this list, will get started
// in an nondeterministic order
func WithStartupOrder(t ...gridtypes.WorkloadType) EngineOption {
return &withStartupOrder{t}
}
// WithAPIGateway sets the API Gateway. If set it will
// be used by the engine to fetch (and validate) the deployment contract
// then contract with be available on the deployment context
func WithAPIGateway(node uint32, substrateGateway *stubs.SubstrateGatewayStub) EngineOption {
return &withAPIGateway{node, substrateGateway}
}
// WithRerunAll if set forces the engine to re-run all reservations
// on engine start.
func WithRerunAll(t bool) EngineOption {
return &withRerunAll{t}
}
type Callback func(twin uint32, contract uint64, delete bool)
// WithCallback sets a callback that is called when a deployment is being Created, Updated, Or Deleted
// The handler then can use the id to get current "state" of the deployment from storage and
// take proper action. A callback must not block otherwise the engine operation will get blocked
func WithCallback(cb Callback) EngineOption {
return &withCallback{cb}
}
type jobOperation int
const (
// opProvision installs a deployment
opProvision jobOperation = iota
// removes a deployment
opDeprovision
// deletes a deployment
opUpdate
// opProvisionNoValidation is used to reinstall
// a deployment on node reboot without validating
// against the chain again because 1) validation
// has already been done on first installation
// 2) hash is not granteed to match because of the
// order of the workloads doesn't have to match
// the one sent by the user
opProvisionNoValidation
// opPause, pauses a deployment
opPause
// opResume resumes a deployment
opResume
// servers default timeout
defaultHttpTimeout = 10 * time.Second
)
// engineJob is a persisted job instance that is
// stored in a queue. the queue uses a GOB encoder
// so please make sure that edits to this struct is
// ONLY by adding new fields or deleting older fields
// but never rename or change the type of a field.
type engineJob struct {
Op jobOperation
Target gridtypes.Deployment
Source *gridtypes.Deployment
Message string
}
// NativeEngine is the core of this package
// The engine is responsible to manage provision and decomission of workloads on the system
type NativeEngine struct {
storage Storage
provisioner Provisioner
queue *dque.DQue
// options
// janitor Janitor
twins Twins
admins Twins
order []gridtypes.WorkloadType
typeIndex map[gridtypes.WorkloadType]int
rerunAll bool
// substrate specific attributes
nodeID uint32
substrateGateway *stubs.SubstrateGatewayStub
callback Callback
}
var (
_ Engine = (*NativeEngine)(nil)
_ pkg.Provision = (*NativeEngine)(nil)
)
type withUserKeyGetter struct {
g Twins
}
func (o *withUserKeyGetter) apply(e *NativeEngine) {
e.twins = o.g
}
type withAdminsKeyGetter struct {
g Twins
}
func (o *withAdminsKeyGetter) apply(e *NativeEngine) {
e.admins = o.g
}
type withAPIGateway struct {
nodeID uint32
substrateGateway *stubs.SubstrateGatewayStub
}
func (o *withAPIGateway) apply(e *NativeEngine) {
e.nodeID = o.nodeID
e.substrateGateway = o.substrateGateway
}
type withStartupOrder struct {
o []gridtypes.WorkloadType
}
func (w *withStartupOrder) apply(e *NativeEngine) {
all := make(map[gridtypes.WorkloadType]struct{})
for _, typ := range e.order {
all[typ] = struct{}{}
}
ordered := make([]gridtypes.WorkloadType, 0, len(all))
for _, typ := range w.o {
if _, ok := all[typ]; !ok {
panic(fmt.Sprintf("type '%s' is not registered", typ))
}
delete(all, typ)
ordered = append(ordered, typ)
e.typeIndex[typ] = len(ordered)
}
// now move everything else
for typ := range all {
ordered = append(ordered, typ)
e.typeIndex[typ] = len(ordered)
}
e.order = ordered
}
type withRerunAll struct {
t bool
}
func (w *withRerunAll) apply(e *NativeEngine) {
e.rerunAll = w.t
}
type withCallback struct {
cb Callback
}
func (w *withCallback) apply(e *NativeEngine) {
e.callback = w.cb
}
type nullKeyGetter struct{}
func (n *nullKeyGetter) GetKey(id uint32) ([]byte, error) {
return nil, fmt.Errorf("null user key getter")
}
type (
engineKey struct{}
deploymentKey struct{}
deploymentValue struct {
twin uint32
deployment uint64
}
)
type (
contractKey struct{}
rentKey struct{}
)
// GetEngine gets engine from context
func GetEngine(ctx context.Context) Engine {
return ctx.Value(engineKey{}).(Engine)
}
// GetDeploymentID gets twin and deployment ID for current deployment
func GetDeploymentID(ctx context.Context) (twin uint32, deployment uint64) {
values := ctx.Value(deploymentKey{}).(deploymentValue)
return values.twin, values.deployment
}
// GetDeployment gets a copy of the current deployment with latest state
func GetDeployment(ctx context.Context) (gridtypes.Deployment, error) {
// we store the pointer on the context so changed to deployment object
// actually reflect into the value.
engine := GetEngine(ctx)
twin, deployment := GetDeploymentID(ctx)
// BUT we always return a copy so caller of GetDeployment can NOT manipulate
// other attributed on the object.
return engine.Storage().Get(twin, deployment)
}
// GetWorkload get the last state of the workload for the current deployment
func GetWorkload(ctx context.Context, name gridtypes.Name) (gridtypes.WorkloadWithID, error) {
// we store the pointer on the context so changed to deployment object
// actually reflect into the value.
engine := GetEngine(ctx)
twin, deployment := GetDeploymentID(ctx)
// BUT we always return a copy so caller of GetDeployment can NOT manipulate
// other attributed on the object.
wl, err := engine.Storage().Current(twin, deployment, name)
if err != nil {
return gridtypes.WorkloadWithID{}, err
}
return gridtypes.WorkloadWithID{
Workload: &wl,
ID: gridtypes.NewUncheckedWorkloadID(twin, deployment, name),
}, nil
}
func withDeployment(ctx context.Context, twin uint32, deployment uint64) context.Context {
return context.WithValue(ctx, deploymentKey{}, deploymentValue{twin, deployment})
}
// GetContract of deployment. panics if engine has no substrate set.
func GetContract(ctx context.Context) substrate.NodeContract {
return ctx.Value(contractKey{}).(substrate.NodeContract)
}
func withContract(ctx context.Context, contract substrate.NodeContract) context.Context {
return context.WithValue(ctx, contractKey{}, contract)
}
// IsRentedNode returns true if current node is rented
func IsRentedNode(ctx context.Context) bool {
v := ctx.Value(rentKey{})
if v == nil {
return false
}
return v.(bool)
}
// sets node rented flag on the ctx
func withRented(ctx context.Context, rent bool) context.Context {
return context.WithValue(ctx, rentKey{}, rent)
}
// New creates a new engine. Once started, the engine
// will continue processing all reservations from the reservation source
// and try to apply them.
// the default implementation is a single threaded worker. so it process
// one reservation at a time. On error, the engine will log the error. and
// continue to next reservation.
func New(storage Storage, provisioner Provisioner, root string, opts ...EngineOption) (*NativeEngine, error) {
e := &NativeEngine{
storage: storage,
provisioner: provisioner,
twins: &nullKeyGetter{},
admins: &nullKeyGetter{},
order: gridtypes.Types(),
typeIndex: make(map[gridtypes.WorkloadType]int),
}
for _, opt := range opts {
opt.apply(e)
}
if e.rerunAll {
os.RemoveAll(filepath.Join(root, "jobs"))
}
queue, err := dque.NewOrOpen("jobs", root, 512, func() interface{} { return &engineJob{} })
if err != nil {
// if this happens it means data types has been changed in that case we need
// to clean up the queue and start over. unfortunately any un applied changes
os.RemoveAll(filepath.Join(root, "jobs"))
return nil, errors.Wrap(err, "failed to create job queue")
}
e.queue = queue
return e, nil
}
// Storage returns
func (e *NativeEngine) Storage() Storage {
return e.storage
}
// Twins returns twins db
func (e *NativeEngine) Twins() Twins {
return e.twins
}
// Admins returns admins db
func (e *NativeEngine) Admins() Twins {
return e.admins
}
// Provision workload
func (e *NativeEngine) Provision(ctx context.Context, deployment gridtypes.Deployment) error {
if deployment.Version != 0 {
return errors.Wrap(ErrInvalidVersion, "expected version to be 0 on deployment creation")
}
if err := e.storage.Create(deployment); err != nil {
return err
}
job := engineJob{
Target: deployment,
Op: opProvision,
}
return e.queue.Enqueue(&job)
}
// Pause deployment
func (e *NativeEngine) Pause(ctx context.Context, twin uint32, id uint64) error {
deployment, err := e.storage.Get(twin, id)
if err != nil {
return err
}
log.Info().
Uint32("twin", deployment.TwinID).
Uint64("contract", deployment.ContractID).
Msg("schedule for pausing")
job := engineJob{
Target: deployment,
Op: opPause,
}
return e.queue.Enqueue(&job)
}
// Resume deployment
func (e *NativeEngine) Resume(ctx context.Context, twin uint32, id uint64) error {
deployment, err := e.storage.Get(twin, id)
if err != nil {
return err
}
log.Info().
Uint32("twin", deployment.TwinID).
Uint64("contract", deployment.ContractID).
Msg("schedule for resuming")
job := engineJob{
Target: deployment,
Op: opResume,
}
return e.queue.Enqueue(&job)
}
// Deprovision workload
func (e *NativeEngine) Deprovision(ctx context.Context, twin uint32, id uint64, reason string) error {
deployment, err := e.storage.Get(twin, id)
if err != nil {
return err
}
log.Info().
Uint32("twin", deployment.TwinID).
Uint64("contract", deployment.ContractID).
Str("reason", reason).
Msg("schedule for deprovision")
job := engineJob{
Target: deployment,
Op: opDeprovision,
Message: reason,
}
return e.queue.Enqueue(&job)
}
// Update workloads
func (e *NativeEngine) Update(ctx context.Context, update gridtypes.Deployment) error {
deployment, err := e.storage.Get(update.TwinID, update.ContractID)
if err != nil {
return err
}
// this will just calculate the update
// steps we run it here as a sort of validation
// that this update is acceptable.
upgrades, err := deployment.Upgrade(&update)
if err != nil {
return errors.Wrap(ErrDeploymentUpgradeValidationError, err.Error())
}
for _, op := range upgrades {
if op.Op == gridtypes.OpUpdate {
if !e.provisioner.CanUpdate(ctx, op.WlID.Type) {
return errors.Wrapf(
ErrDeploymentUpgradeValidationError,
"workload '%s' does not support upgrade",
op.WlID.Type.String())
}
}
}
// fields to update in storage
fields := []Field{
VersionField{update.Version},
SignatureRequirementField{update.SignatureRequirement},
}
if deployment.Description != update.Description {
fields = append(fields, DescriptionField{update.Description})
}
if deployment.Metadata != update.Metadata {
fields = append(fields, MetadataField{update.Metadata})
}
// update deployment fields, workloads will then can get updated separately
if err := e.storage.Update(update.TwinID, update.ContractID, fields...); err != nil {
return errors.Wrap(err, "failed to update deployment data")
}
// all is okay we can push the job
job := engineJob{
Op: opUpdate,
Target: update,
Source: &deployment,
}
return e.queue.Enqueue(&job)
}
// Run starts reader reservation from the Source and handle them
func (e *NativeEngine) Run(root context.Context) error {
defer e.queue.Close()
root = context.WithValue(root, engineKey{}, e)
if e.rerunAll {
if err := e.boot(root); err != nil {
log.Error().Err(err).Msg("error while setting up")
}
}
for {
obj, err := e.queue.PeekBlock()
if err != nil {
log.Error().Err(err).Msg("failed to check job queue")
<-time.After(2 * time.Second)
continue
}
job := obj.(*engineJob)
ctx := withDeployment(root, job.Target.TwinID, job.Target.ContractID)
l := log.With().
Uint32("twin", job.Target.TwinID).
Uint64("contract", job.Target.ContractID).
Logger()
// contract validation
// this should ONLY be done on provosion and update operation
if job.Op == opProvision ||
job.Op == opUpdate ||
job.Op == opProvisionNoValidation {
// otherwise, contract validation is needed
ctx, err = e.validate(ctx, &job.Target, job.Op == opProvisionNoValidation)
if err != nil {
l.Error().Err(err).Msg("contact validation fails")
// job.Target.SetError(err)
if err := e.storage.Error(job.Target.TwinID, job.Target.ContractID, err); err != nil {
l.Error().Err(err).Msg("failed to set deployment global error")
}
_, _ = e.queue.Dequeue()
continue
}
l.Debug().Msg("contact validation pass")
}
switch job.Op {
case opProvisionNoValidation:
fallthrough
case opProvision:
e.installDeployment(ctx, &job.Target)
case opDeprovision:
e.uninstallDeployment(ctx, &job.Target, job.Message)
case opPause:
e.lockDeployment(ctx, &job.Target)
case opResume:
e.unlockDeployment(ctx, &job.Target)
case opUpdate:
// update is tricky because we need to work against
// 2 versions of the object. Once that reflects the current state
// and the new one that is the target state but it does not know
// the current state of already deployed workloads
// so (1st) we need to get the difference
// this call will return 3 lists
// - things to remove
// - things to add
// - things to update (not supported atm)
// - things that is not in any of the 3 lists are basically stay as is
// the call will also make sure the Result of those workload in both the (did not change)
// and update to reflect the current result on those workloads.
update, err := job.Source.Upgrade(&job.Target)
if err != nil {
l.Error().Err(err).Msg("failed to get update procedure")
break
}
e.updateDeployment(ctx, update)
}
_, err = e.queue.Dequeue()
if err != nil {
l.Error().Err(err).Msg("failed to dequeue job")
}
e.safeCallback(&job.Target, job.Op == opDeprovision)
}
}
func (e *NativeEngine) safeCallback(d *gridtypes.Deployment, delete bool) {
if e.callback == nil {
return
}
// in case callback panics we don't want to kill the engine
defer func() {
if err := recover(); err != nil {
log.Error().Msgf("panic while processing callback: %v", err)
}
}()
e.callback(d.TwinID, d.ContractID, delete)
}
// validate validates and injects the deployment contracts is substrate is configured
// for this instance of the provision engine. If noValidation is set contracts checks is skipped
func (e *NativeEngine) validate(ctx context.Context, dl *gridtypes.Deployment, noValidation bool) (context.Context, error) {
if e.substrateGateway == nil {
return ctx, fmt.Errorf("substrate is not configured in engine")
}
contract, subErr := e.substrateGateway.GetContract(ctx, dl.ContractID)
if subErr.IsError() {
return nil, errors.Wrap(subErr.Err, "failed to get deployment contract")
}
if !contract.ContractType.IsNodeContract {
return nil, fmt.Errorf("invalid contract type, expecting node contract")
}
ctx = withContract(ctx, contract.ContractType.NodeContract)
rent, subErr := e.substrateGateway.GetNodeRentContract(ctx, e.nodeID)
if subErr.IsError() && !subErr.IsCode(pkg.CodeNotFound) {
return nil, fmt.Errorf("failed to check node rent state")
}
ctx = withRented(ctx, !subErr.IsError() && rent != 0)
if noValidation {
return ctx, nil
}
if uint32(contract.ContractType.NodeContract.Node) != e.nodeID {
return nil, fmt.Errorf("invalid node address in contract")
}
hash, err := dl.ChallengeHash()
if err != nil {
return nil, errors.Wrap(err, "failed to compute deployment hash")
}
if contract.ContractType.NodeContract.DeploymentHash.String() != hex.EncodeToString(hash) {
return nil, fmt.Errorf("contract hash does not match deployment hash")
}
return ctx, nil
}
// boot will make sure to re-deploy all stored reservation
// on boot.
func (e *NativeEngine) boot(root context.Context) error {
storage := e.Storage()
twins, err := storage.Twins()
if err != nil {
return errors.Wrap(err, "failed to list twins")
}
for _, twin := range twins {
ids, err := storage.ByTwin(twin)
if err != nil {
log.Error().Err(err).Uint32("twin", twin).Msg("failed to list deployments for twin")
continue
}
for _, id := range ids {
dl, err := storage.Get(twin, id)
if err != nil {
log.Error().Err(err).Uint32("twin", twin).Uint64("id", id).Msg("failed to load deployment")
continue
}
// unfortunately we have to inject this value here
// since the boot runs outside the engine queue.
if !dl.IsActive() {
continue
}
job := engineJob{
Target: dl,
Op: opProvisionNoValidation,
}
if err := e.queue.Enqueue(&job); err != nil {
log.Error().
Err(err).
Uint32("twin", dl.TwinID).
Uint64("dl", dl.ContractID).
Msg("failed to queue deployment for processing")
}
}
}
return nil
}
func (e *NativeEngine) uninstallWorkload(ctx context.Context, wl *gridtypes.WorkloadWithID, reason string) error {
twin, deployment, name, _ := wl.ID.Parts()
log := log.With().
Uint32("twin", twin).
Uint64("deployment", deployment).
Stringer("name", name).
Str("type", wl.Type.String()).
Logger()
_, err := e.storage.Current(twin, deployment, name)
if errors.Is(err, ErrWorkloadNotExist) || errors.Is(err, ErrDeploymentNotExists) {
// workload or deployment doesn't exist in storage, consider it already deleted
return nil
} else if err != nil {
return err
}
log.Debug().Str("workload", string(wl.Name)).Msg("de-provisioning")
result := gridtypes.Result{
State: gridtypes.StateDeleted,
Error: reason,
}
if err := e.provisioner.Deprovision(ctx, wl); err != nil {
log.Error().Err(err).Stringer("id", wl.ID).Msg("failed to uninstall workload")
result.State = gridtypes.StateError
result.Error = err.Error()
}
result.Created = gridtypes.Timestamp(time.Now().Unix())
if err := e.storage.Transaction(twin, deployment, wl.Workload.WithResults(result)); err != nil {
return err
}
if result.State == gridtypes.StateDeleted {
return e.storage.Remove(twin, deployment, name)
}
return nil
}
func (e *NativeEngine) installWorkload(ctx context.Context, wl *gridtypes.WorkloadWithID) error {
// this workload is already deleted or in error state
// we don't try again
twin, deployment, name, _ := wl.ID.Parts()
current, err := e.storage.Current(twin, deployment, name)
if errors.Is(err, ErrWorkloadNotExist) {
// this can happen if installWorkload was called upon a deployment update operation
// so this is a totally new workload that was not part of the original deployment
// hence a call to Add is needed
if err := e.storage.Add(twin, deployment, *wl.Workload); err != nil {
return errors.Wrap(err, "failed to add workload to storage")
}
} else if err != nil {
// another error
return errors.Wrapf(err, "failed to get last transaction for '%s'", wl.ID.String())
} else {
// workload exists, but we trying to re-install it so this might be
// after a reboot. hence we need to check last state.
// if it has been deleted, error state, we do nothing.
// otherwise, we-reinstall it
if current.Result.State.IsAny(gridtypes.StateDeleted, gridtypes.StateError) {
// nothing to do!
return nil
}
}
log := log.With().
Uint32("twin", twin).
Uint64("deployment", deployment).
Stringer("name", wl.Name).
Str("type", wl.Type.String()).
Logger()
log.Debug().Msg("provisioning")
result, err := e.provisioner.Provision(ctx, wl)
if errors.Is(err, ErrNoActionNeeded) {
// workload already exist, so no need to create a new transaction
return nil
} else if err != nil {
result.Created = gridtypes.Now()
result.State = gridtypes.StateError
result.Error = err.Error()
}
if result.State == gridtypes.StateError {
log.Error().Str("error", result.Error).Msg("failed to deploy workload")
}
return e.storage.Transaction(
twin,
deployment,
wl.Workload.WithResults(result))
}
func (e *NativeEngine) updateWorkload(ctx context.Context, wl *gridtypes.WorkloadWithID) error {
twin, deployment, name, _ := wl.ID.Parts()
log := log.With().
Uint32("twin", twin).
Uint64("deployment", deployment).
Stringer("name", name).
Str("type", wl.Type.String()).
Logger()
log.Debug().Msg("provisioning")
var result gridtypes.Result
var err error
if e.provisioner.CanUpdate(ctx, wl.Type) {
result, err = e.provisioner.Update(ctx, wl)
} else {
// deprecated. We should never update resources by decommission and then provision
// the check in Update method should prevent this
// #unreachable
err = fmt.Errorf("can not update this workload type")
}
if errors.Is(err, ErrNoActionNeeded) {
currentWl, err := e.storage.Current(twin, deployment, name)
if err != nil {
return err
}
result = currentWl.Result
} else if err != nil {
return err
}
return e.storage.Transaction(twin, deployment, wl.Workload.WithResults(result))
}
func (e *NativeEngine) lockWorkload(ctx context.Context, wl *gridtypes.WorkloadWithID, lock bool) error {
// this workload is already deleted or in error state
// we don't try again
twin, deployment, name, _ := wl.ID.Parts()
current, err := e.storage.Current(twin, deployment, name)
if err != nil {
// another error
return errors.Wrapf(err, "failed to get last transaction for '%s'", wl.ID.String())
} else {
if !current.Result.State.IsOkay() {
// nothing to do! it's either in error state or something else.
return nil
}
}
log := log.With().
Uint32("twin", twin).
Uint64("deployment", deployment).
Stringer("name", wl.Name).
Str("type", wl.Type.String()).
Bool("lock", lock).
Logger()
log.Debug().Msg("setting locking on workload")
action := e.provisioner.Resume
if lock {
action = e.provisioner.Pause
}
result, err := action(ctx, wl)
if errors.Is(err, ErrNoActionNeeded) {
// workload already exist, so no need to create a new transaction
return nil
} else if err != nil {
return err
}
if result.State == gridtypes.StateError {
log.Error().Str("error", result.Error).Msg("failed to set locking on workload")
}
return e.storage.Transaction(
twin,
deployment,
wl.Workload.WithResults(result))
}
func (e *NativeEngine) uninstallDeployment(ctx context.Context, dl *gridtypes.Deployment, reason string) {
var errors bool
for i := len(e.order) - 1; i >= 0; i-- {
typ := e.order[i]
workloads := dl.ByType(typ)
for _, wl := range workloads {
if err := e.uninstallWorkload(ctx, wl, reason); err != nil {
errors = true
log.Error().Err(err).Stringer("id", wl.ID).Msg("failed to un-install workload")
}
}
}
if errors {
return
}
if err := e.storage.Delete(dl.TwinID, dl.ContractID); err != nil {
log.Error().Err(err).
Uint32("twin", dl.TwinID).
Uint64("contract", dl.ContractID).
Msg("failed to delete deployment")
}
}
func getMountSize(wl *gridtypes.Workload) (gridtypes.Unit, error) {
data, err := wl.WorkloadData()
if err != nil {
return 0, err
}
switch d := data.(type) {
case *zos.ZMount:
return d.Size, nil
case *zos.Volume:
return d.Size, nil
default:
return 0, fmt.Errorf("failed to get workload as zmount or volume '%v'", data)
}
}
func sortMountWorkloads(workloads []*gridtypes.WorkloadWithID) {
sort.Slice(workloads, func(i, j int) bool {
sizeI, err := getMountSize(workloads[i].Workload)
if err != nil {
return false
}
sizeJ, err := getMountSize(workloads[j].Workload)
if err != nil {
return false
}
return sizeI > sizeJ
})
}
func (e *NativeEngine) installDeployment(ctx context.Context, getter gridtypes.WorkloadGetter) {
for _, typ := range e.order {
workloads := getter.ByType(typ)
if typ == zos.ZMountType || typ == zos.VolumeType {
sortMountWorkloads(workloads)
}
for _, wl := range workloads {
if err := e.installWorkload(ctx, wl); err != nil {
log.Error().Err(err).Stringer("id", wl.ID).Msg("failed to install workload")
}
}
}
}
func (e *NativeEngine) lockDeployment(ctx context.Context, getter gridtypes.WorkloadGetter) {
for i := len(e.order) - 1; i >= 0; i-- {
typ := e.order[i]
workloads := getter.ByType(typ)
for _, wl := range workloads {
if err := e.lockWorkload(ctx, wl, true); err != nil {
log.Error().Err(err).Stringer("id", wl.ID).Msg("failed to lock workload")
}
}
}
}
func (e *NativeEngine) unlockDeployment(ctx context.Context, getter gridtypes.WorkloadGetter) {
for _, typ := range e.order {
workloads := getter.ByType(typ)
for _, wl := range workloads {
if err := e.lockWorkload(ctx, wl, false); err != nil {
log.Error().Err(err).Stringer("id", wl.ID).Msg("failed to unlock workload")
}
}
}
}
// sortOperations sortes the operations, removes first in reverse type order, then upgrades/creates in type order
func (e *NativeEngine) sortOperations(ops []gridtypes.UpgradeOp) {
// maps an operation to an integer, less comes first in sorting
opMap := func(op gridtypes.UpgradeOp) int {
if op.Op == gridtypes.OpRemove {
// removes are negative (typeIndex starts from 1) so they are always before creations/updates
// negated to apply in reverse order
return -e.typeIndex[op.WlID.Type]
} else {
// updates/creates are considered the same
return e.typeIndex[op.WlID.Type]
}
}
sort.SliceStable(ops, func(i, j int) bool {
return opMap(ops[i]) < opMap(ops[j])
})
}
func (e *NativeEngine) updateDeployment(ctx context.Context, ops []gridtypes.UpgradeOp) (changed bool) {
e.sortOperations(ops)
for _, op := range ops {
var err error
switch op.Op {
case gridtypes.OpRemove:
err = e.uninstallWorkload(ctx, op.WlID, "deleted by an update")
case gridtypes.OpAdd:
err = e.installWorkload(ctx, op.WlID)
case gridtypes.OpUpdate:
err = e.updateWorkload(ctx, op.WlID)
}
if err != nil {
log.Error().Err(err).Stringer("id", op.WlID.ID).Stringer("operation", op.Op).Msg("error while updating deployment")
}
}
return
}
// DecommissionCached implements the zbus interface
func (e *NativeEngine) DecommissionCached(id string, reason string) error {
globalID := gridtypes.WorkloadID(id)
twin, dlID, name, err := globalID.Parts()
if err != nil {
return err
}
wl, err := e.storage.Current(twin, dlID, name)
if err != nil {
return err
}