-
Notifications
You must be signed in to change notification settings - Fork 10
/
deployflow_controller.go
989 lines (797 loc) · 29 KB
/
deployflow_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
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
/*
Copyright 2021 The Triton Authors.
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 deployflow
import (
"context"
"encoding/json"
"flag"
"fmt"
"reflect"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/pkg/errors"
terrors "github.com/triton-io/triton/pkg/errors"
"github.com/triton-io/triton/pkg/kube/fetcher"
internaldeploy "github.com/triton-io/triton/pkg/kube/types/deploy"
internalpod "github.com/triton-io/triton/pkg/kube/types/pod"
"github.com/triton-io/triton/pkg/kube/types/workload"
"github.com/triton-io/triton/pkg/services/deployflow"
"github.com/triton-io/triton/pkg/setting"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
kruiseappsv1alpha1 "github.com/openkruise/kruise-api/apps/v1alpha1"
"github.com/sirupsen/logrus"
tritonappsv1alpha1 "github.com/triton-io/triton/apis/apps/v1alpha1"
"github.com/triton-io/triton/pkg/log"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
func init() {
flag.IntVar(&concurrentReconciles, "deployflow-workers", concurrentReconciles, "Max concurrent workers for deployflow controller.")
}
var (
concurrentReconciles = 3
)
// Add creates a new DeployFlow Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
// and Start it when the Manager is Started.
func Add(mgr manager.Manager) error {
return add(mgr, newReconciler(mgr))
}
// DeployFlowReconciler reconciles a DeployFlow object
type DeployFlowReconciler struct {
client.Client
Scheme *runtime.Scheme
reader client.Reader
logger *logrus.Entry
reconcileFunc func(ctx context.Context, request reconcile.Request) (reconcile.Result, error)
recorder record.EventRecorder
}
func newReconciler(mgr ctrl.Manager) reconcile.Reconciler {
logger := log.WithField("controller", "DeployFlow")
if err := mgr.GetFieldIndexer().IndexField(context.TODO(), &tritonappsv1alpha1.DeployFlow{}, "spec.Application.AppName", func(rawObj runtime.Object) []string {
d, ok := rawObj.(*tritonappsv1alpha1.DeployFlow)
if !ok {
logger.Errorf("failed to get deployflow resource")
return nil
}
return []string{d.Spec.Application.AppName}
}); err != nil {
logger.Errorf("failed to get filed indexer")
return nil
}
recorder := mgr.GetEventRecorderFor("DeployFlow Controller")
reconciler := &DeployFlowReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
reader: mgr.GetAPIReader(),
logger: logger,
recorder: recorder,
}
reconciler.reconcileFunc = reconciler.doReconcile
return reconciler
}
// add adds a new Controller to mgr with r as the reconcile.Reconciler
func add(mgr manager.Manager, r reconcile.Reconciler) error {
err := ctrl.NewControllerManagedBy(mgr).
For(&tritonappsv1alpha1.DeployFlow{}).
Owns(&kruiseappsv1alpha1.CloneSet{}).
//Watches(&source.Kind{Type: &kruiseappsv1alpha1.CloneSet{}}, &handler.EnqueueRequestForOwner{}, builder.WithPredicates(CloneSetStatusChangedPredicate{})).
Complete(r)
if err != nil {
return err
}
log.Info("DeployFlow Controller created")
return nil
}
var _ reconcile.Reconciler = &DeployFlowReconciler{}
//+kubebuilder:rbac:groups=apps.triton.io,resources=deployflows,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps.triton.io,resources=deployflows/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=apps.triton.io,resources=deployflows/finalizers,verbs=update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// the DeployFlow object against the actual cluster state, and then
// perform operations to make the cluster state reflect the state specified by
// the user.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
// Reconcile reads that state of the cluster for a CloneSet object and makes changes based on the state read
// and what is in the CloneSet.Spec
func (r *DeployFlowReconciler) Reconcile(req reconcile.Request) (reconcile.Result, error) {
return r.reconcileFunc(context.TODO(), req)
}
func (r *DeployFlowReconciler) doReconcile(ctx context.Context, req reconcile.Request) (res reconcile.Result, retErr error) {
// your logic here
logger := r.logger.WithField("deploy", req.NamespacedName)
deploy := &tritonappsv1alpha1.DeployFlow{}
// get deploy from API Server instead of cache
if err := r.reader.Get(ctx, req.NamespacedName, deploy); err != nil {
logger.WithError(err).Error("unable to fetch Deploy")
// we'll ignore not-found errors, since they can't be fixed by an immediate
// requeue (we'll need to wait for a new notification), and we can get them
// on deleted requests.
return ctrl.Result{}, client.IgnoreNotFound(err)
}
idl := internaldeploy.FromDeploy(deploy)
if idl.Finished() {
// If a deploy is finished, never touch it again
return ctrl.Result{}, nil
}
// logger.Info("Start to reconcile")
status := *deploy.Status.DeepCopy()
defer func() {
if !reflect.DeepEqual(status, idl.Status) {
if err := r.updateDeployStatus(idl); err != nil {
logger.WithError(err).Error("unable to update Deploy status")
}
}
}()
defer func() {
if idl.Finished() {
r.postDeploy(idl)
}
if err := r.DeleteCloneSetWhenActionIsScaleInZero(deploy); err != nil {
logger.WithError(err).Error("unable delete cloneSet which replicas is 0 when action is scale in")
}
}()
if err := r.process(idl); err != nil {
if e, ok := err.(terrors.RequeueError); ok {
return ctrl.Result{RequeueAfter: e.RequeueAfter()}, nil
}
logger.WithError(err).Error("failed to process deploy")
r.recorder.Event(idl.Unwrap(), corev1.EventTypeWarning, eventReasonUnhealthy, err.Error())
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func (r *DeployFlowReconciler) updateDeployStatus(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
now := metav1.Now()
logger.Infof("Updating deploy status at %s", now)
idl.SetUpdatedAt(now)
ctx := context.Background()
if err := r.Status().Update(ctx, idl.Unwrap()); err != nil {
if !apierrors.IsConflict(err) {
return errors.Wrap(err, "failed to update Deploy status")
}
logger.Warn("can not update deploy status due to a conflict, fetch latest version and try again")
updated := &tritonappsv1alpha1.DeployFlow{}
if err := r.reader.Get(ctx, types.NamespacedName{Namespace: idl.Namespace, Name: idl.Name}, updated); err != nil {
return errors.Wrap(err, "failed to fetch Deploy from API Server")
}
if idl.CurrentBatchSmoking() || !idl.CurrentBatchIsPodReady() {
// save the latest pod status updated by CloneSet controller
populatedPods := updated.Status.Pods
currentBatchInfo := internaldeploy.FromDeploy(updated).CurrentBatchInfo()
currentBatch := currentBatchInfo.Batch
currentBatchPods := currentBatchInfo.Pods
failedReplicas := currentBatchInfo.FailedReplicas
updated.Status = idl.Status
// restore the latest pod status
updated.Status.Pods = populatedPods
if len(currentBatchPods) > 0 {
updated.Status.Conditions[currentBatch-1].Pods = currentBatchPods
updated.Status.Conditions[currentBatch-1].FailedReplicas = failedReplicas
}
}
//if idl.CurrentBatchPhase() == tritonappsv1alpha1.BatchSmoking {
// err := mergeDeployStatus(idl.Unwrap(), updated, logger)
// if err != nil {
// return errors.Wrap(err, "failed to merge Deploy status")
// }
//}
//
//idl.SetResourceVersion(updated.ResourceVersion)
data, _ := json.Marshal(updated.Status)
logger.Debugf("the current status is %s", data)
return r.Status().Update(ctx, updated)
}
return nil
}
func (r *DeployFlowReconciler) process(idl *internaldeploy.Deploy) error {
if err := r.processPausedOrCanceledDeploy(idl); err != nil {
return err
}
switch idl.Status.Phase {
case "":
return r.processNewDeploy(idl)
case tritonappsv1alpha1.Pending:
if !idl.Paused() {
return r.processPendingDeploy(idl)
}
case tritonappsv1alpha1.Initializing:
return r.processInitializingDeploy(idl)
case tritonappsv1alpha1.BatchStarted:
if !idl.Paused() {
return r.processBatch(idl)
}
case tritonappsv1alpha1.BatchFinished:
return r.processBatchFinishedDeploy(idl)
}
return nil
}
func (r *DeployFlowReconciler) processNewDeploy(idl *internaldeploy.Deploy) error {
esLogger := r.logger.WithField("deploy", idl)
esLogger.Info("Start to process new deploy")
idl.PrepareNewDeploy()
return nil
}
func (r *DeployFlowReconciler) processPendingDeploy(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
cs, found, err := fetcher.GetCloneSetInCacheByDeploy(idl.Unwrap(), r.Client)
if err != nil {
logger.WithError(err).Errorf("failed to fetch cloneSet %s", idl.Spec.Application.InstanceName)
return err
} else if !found {
if idl.Spec.Action != setting.Create {
idl.MarkAsFailed()
return nil
}
logger.Infof("cloneSet %s does not exist, start to create it", idl.Spec.Application.InstanceName)
err := r.createCloneSet(idl)
if err != nil {
logger.WithError(err).Error("failed to create cloneSet")
return err
}
logger.Infof("cloneSet created, start to process the deploy")
} else {
if idl.Spec.Action == setting.Create {
idl.MarkAsFailed()
return nil
}
finished, paused, err := lastDeployFinishedOrPaused(cs, r.Client)
if err != nil {
logger.WithError(err).Error("failed to check last deploy status")
return err
}
if !finished {
// if last deploy is not paused, never move forward,
// if it is paused, move forward if it is a update.
if !paused || !idl.RevisionChanged() {
logger.Info("last deploy in progress, sleep 5 seconds and try again")
return terrors.NewLastDeployInProgressError(5 * time.Second)
}
logger.Info("last deploy is paused, abort it.")
if err := abortPausedDeploy(cs, r.Client); err != nil {
logger.WithError(err).Error("Failed to update deploy status")
return err
}
}
if idl.RevisionChanged() {
logger.Infof("cloneSet %s is found, start to update it", idl.Spec.Application.InstanceName)
if err := r.updateCloneSet(idl); err != nil {
logger.WithError(err).Error("Failed to update cloneSet")
return err
}
} else {
logger.Infof("Taking ownership of cloneSet %s", idl.Spec.Application.InstanceName)
if err := r.takeOwnershipOfCloneSet(idl); err != nil {
logger.WithError(err).Errorf("Failed to set owner for cloneSet %s", idl.Spec.Application.InstanceName)
return err
}
}
}
logger.Infof("Resource updated")
idl.StartProgress()
return nil
}
func lastDeployFinishedOrPaused(cs *kruiseappsv1alpha1.CloneSet, cl client.Client) (finished, paused bool, err error) {
deploy, found, err := fetcher.GetDeployInCacheOwningCloneSet(cs, cl)
if err != nil || !found {
return true, false, err
}
idl := internaldeploy.FromDeploy(deploy)
return idl.Finished(), idl.Paused(), nil
}
func abortPausedDeploy(cs *kruiseappsv1alpha1.CloneSet, cl client.Client) error {
deploy, _, err := fetcher.GetDeployInCacheOwningCloneSet(cs, cl)
if err != nil {
return err
}
if deploy == nil {
return nil
}
idl := internaldeploy.FromDeploy(deploy)
idl.MarkAsAborted()
// use update instead of patch here to make sure deploy is not changed yet.
return cl.Status().Update(context.TODO(), idl.Unwrap())
}
func (r *DeployFlowReconciler) processPausedOrCanceledDeploy(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
if !idl.Interruptible() {
return nil
}
// pause the CloneSet when deploy is canceled.
if idl.ShouldCancel() {
//TODO if cancel batch is canary, rollback cloneset
logger.Info("Deploy canceled, pause the CloneSet")
if err := r.pauseCloneSet(idl); err != nil {
logger.WithError(err).Error("Failed to pause CloneSet")
return err
}
logger.Info("Deploy canceled")
idl.MarkAsCanceled()
err := r.ResetReplicasAfterCanceled(idl)
if err != nil {
logger.WithError(err).Error("Failed to reset replicas of cloneset")
return err
}
return nil
}
paused := idl.DesiredPausedState()
if paused != nil && *paused != idl.Paused() {
if !*paused && idl.CurrentBatchFailed() {
logger.Warn("There are failure pods in current batch, you need to fix them before resuming the Deploy")
return nil
}
if err := r.pauseOrResumeCloneSet(idl, *paused); err != nil {
logger.WithError(err).Error("Failed to pause or resume CloneSet")
return err
}
logger.Infof("Deploy paused is %t", *paused)
idl.SetPaused(*paused)
}
return nil
}
func (r *DeployFlowReconciler) ResetReplicasAfterCanceled(idl *internaldeploy.Deploy) error {
if idl.Spec.Action == setting.Create {
return nil
}
replicas := int(*idl.Spec.Application.Replicas)
patchBytes := []byte(fmt.Sprintf(`{"spec":{"replicas":%d}}`, replicas))
return PatchCloneSet(idl.Unwrap(), patchBytes, r.Client)
}
func (r *DeployFlowReconciler) processInitializingDeploy(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
// do not move forward until fields like updateRevision, updatedReplicas...
// in .status is updated
if !CloneSetStatusSynced(idl.Unwrap(), r.Client) {
logger.Info("CloneSet status is not updated yet, skip and wait for next try.")
return nil
}
if err := r.populateReplicasStatus(idl); err != nil {
return err
}
logger.Info("Deploy initialized")
idl.StartBatch()
return nil
}
func CloneSetStatusSynced(deploy *tritonappsv1alpha1.DeployFlow, cl client.Client) bool {
cs, found, err := fetcher.GetCloneSetInCacheOwnedByDeploy(deploy, cl)
if err != nil || !found {
return false
}
return cs.GetGeneration() == cs.Status.ObservedGeneration
}
func (r *DeployFlowReconciler) populateReplicasStatus(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
cs, found, err := fetcher.GetCloneSetInCacheOwnedByDeploy(idl.Unwrap(), r.Client)
if err != nil || !found {
logger.WithError(err).Error("unable to fetch CloneSet")
return fmt.Errorf("unable to fetch CloneSet: %w", err)
}
idl.DeployFlow.Status.AvailableReplicas = cs.Status.AvailableReplicas
idl.DeployFlow.Status.UpdatedReadyReplicas = cs.Status.UpdatedReadyReplicas
idl.DeployFlow.Status.UpdatedReplicas = cs.Status.UpdatedReplicas
idl.DeployFlow.Status.Replicas = cs.Status.Replicas
idl.DeployFlow.Status.UpdateRevision = cs.Status.UpdateRevision
return nil
}
func (r *DeployFlowReconciler) processBatch(idl *internaldeploy.Deploy) error {
if err := r.populateReplicasStatus(idl); err != nil {
return err
}
switch idl.CurrentBatchPhase() {
case tritonappsv1alpha1.BatchPending:
// the first pending batch should be processed no matter MoveForward is true or false
if idl.CurrentBatchNumber() == 1 || idl.MoveForward() {
return r.processNewBatch(idl)
}
case tritonappsv1alpha1.BatchSmoking:
return r.processSmokingBatch(idl)
case tritonappsv1alpha1.BatchSmoked:
// move forward if:
// 1. it is not a canary
// 2. it is a canary and MoveForward is true
if !idl.CurrentBatchIsCanary() || idl.MoveForward() {
return r.processSmokedBatch(idl)
}
case tritonappsv1alpha1.BatchBaking:
// move forward if:
// 1. it is not a canary
// 2. it is a canary and MoveForward is true
if !idl.CurrentBatchIsCanary() || idl.MoveForward() {
return r.processBakingBatch(idl)
}
case tritonappsv1alpha1.BatchBaked:
return r.processFinishedBatch(idl)
case "":
return r.processEmptyBatch(idl)
}
return nil
}
func (r *DeployFlowReconciler) processBatchFinishedDeploy(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
if err := r.populateReplicasStatus(idl); err != nil {
return err
}
if idl.ShouldCancel() {
idl.MarkAsCanceled()
}
// if a deployflow has no change and all pods are in ContainersReady status, mark as success directly
if idl.NoChangedDeploy() && len(idl.GetNotContainerReadyPods()) == 0 {
logger.Info("All batches are finished, the deploy is done")
idl.MarkAsSuccess()
r.recorder.Event(idl.Unwrap(), corev1.EventTypeNormal, eventReasonDeployed, eventMessageDeployed)
return nil
}
// if a deployfolw has no change and has pod not ready, patch the cloneset use scale strategy
if idl.NoChangedDeploy() && len(idl.GetNotContainerReadyPods()) != 0 {
// update cloneset to
pods := idl.GetNotContainerReadyPods()
patchBytes := []byte(fmt.Sprintf(`{"spec":{"scaleStrategy":{"podsToDelete":%v}}}`, pods))
err := PatchCloneSet(idl.Unwrap(), patchBytes, r.Client)
if err != nil {
logger.Errorf("patch cloneset %v failed, error is %v", idl.Unwrap().Spec.Application.InstanceName, err)
return err
}
statusBytes := []byte(fmt.Sprintf(`{"status":{"phase":"%s"}`, tritonappsv1alpha1.Initializing))
_, err = deployflow.PatchDeployStatus(idl.Namespace, idl.Name, r.reader, r.Client, statusBytes)
if err != nil {
logger.Errorf("patch deployflow %v failed, error is %v", idl.Name, err)
return err
}
}
if idl.Status.AvailableReplicas != *idl.Spec.Application.Replicas ||
*idl.Spec.Application.Replicas != idl.Status.Replicas ||
*idl.Spec.Application.Replicas != idl.Status.UpdatedReplicas {
logger.Warn("Final state mismatch, skip and retry later")
return fmt.Errorf("final state mismatch")
}
logger.Info("All batches are finished, the deploy is done")
idl.MarkAsSuccess()
r.recorder.Event(idl.Unwrap(), corev1.EventTypeNormal, eventReasonDeployed, eventMessageDeployed)
return nil
}
func (r *DeployFlowReconciler) processNewBatch(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
logger.Info("Start to process new batch")
startedAt := metav1.Now()
if err := r.processCloneSet(idl); err != nil {
logger.WithError(err).Error("failed to process cloneSet")
return err
}
idl.MarkCurrentBatchAsStarted(startedAt)
logger.Infof("Batch %d is started", idl.CurrentBatchNumber())
return nil
}
func (r *DeployFlowReconciler) processSmokingBatch(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
logger.Info("Processing smoking batch.")
if idl.CurrentBatchFailed() {
logger.Info("Current batch failed, pause the CloneSet")
if err := r.pauseCloneSet(idl); err != nil {
return err
}
logger.Info("Paused deploy due to batch failure")
idl.MarkAsPaused()
return nil
}
if idl.CurrentBatchIsContainersReady() {
return r.processContainersReadyBatch(idl)
}
return nil
}
// processSmokedBatch enables new pods by:
// 1. set pod readiness gate to True.
// 2. when all pods are ready, pull in from registry.
// 3. check periodically to make sure all instances are enabled.
func (r *DeployFlowReconciler) processSmokedBatch(idl *internaldeploy.Deploy) error {
pulledInAt := idl.CurrentBatchPullInAt()
if pulledInAt.IsZero() {
return r.prepareToBakeBatch(idl)
}
return r.checkBakingGate(idl)
}
func (r *DeployFlowReconciler) processFinishedBatch(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
if idl.AllBatchFinished() {
idl.MarkAsBatchFinished()
return nil
}
batch := idl.CurrentBatchInfo()
// if mode is auto and current batch is canary, do not wait batch interval
if !(idl.Auto() && idl.CurrentBatchIsCanary()) {
if time.Since(batch.FinishedAt.Time) < time.Duration(idl.BatchIntervalSeconds())*time.Second {
logger.Info("batch time interval not reached, sleep 3 seconds and try again")
return terrors.NewTimeIntervalNotReachedError(3 * time.Second)
}
}
logger.Info("Current batch is finished, prepare a new one")
idl.PrepareNewBatch()
return nil
}
func (r *DeployFlowReconciler) processEmptyBatch(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
if idl.AllBatchFinished() {
idl.MarkAsBatchFinished()
return nil
}
logger.Info("no batches is initialized, prepare a new one")
idl.PrepareNewBatch()
return nil
}
func (r *DeployFlowReconciler) processContainersReadyBatch(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
logger.Info("All Pods in current batch are ready")
// only canary pods need to be registered to gateway.
if idl.CurrentBatchIsCanary() {
if err := r.registerPods(idl); err != nil {
logger.WithError(err).Error("failed to register pods")
// move forward even it is failed
//return err
}
}
logger.Info("Marking current batch as smoked")
idl.MarkCurrentBatchAsSmoked()
return nil
}
func (r *DeployFlowReconciler) prepareToBakeBatch(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
logger.Info("Preparing to bake the batch")
if !idl.SkipPullIn() {
logger.Info("Pulling in new pods")
if !idl.CurrentBatchIsPodReady() {
return r.setPodReadinessGates(idl)
}
// pull in new pods
//if err := r.pullIn(idl); err != nil {
// logger.WithError(err).Error("failed to pull in new pods")
// return err
//}
} else {
logger.Info("skip pulling in")
}
idl.SetPulledInAt()
return nil
}
func (r *DeployFlowReconciler) processBakingBatch(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
logger.Info("Baking is done, remove old pods if any and mark current batch as baked")
// pull out old pods
if err := r.pullOut(idl); err != nil {
logger.WithError(err).Error("failed to pull out old pods")
return err
}
logger.Infof("Batch %d is finished", idl.CurrentBatchNumber())
idl.MarkCurrentBatchAsFinished()
return nil
}
func (r *DeployFlowReconciler) pullOut(idl *internaldeploy.Deploy) error {
logger := log.WithField("deploy", idl.Name)
if idl.Spec.Action == setting.Create {
return nil
}
if idl.Spec.Action == setting.Restart || (idl.Spec.Action == setting.ScaleIn && len(idl.NonUpdateStrategy().PodsToDelete) > 0) {
if err := r.pullOutPodsForRestart(idl); err != nil {
logger.WithError(err).Error("Failed to pull out pods for restart")
return err
}
}
logger.Info("Start to pull out old pods.")
return r.processCloneSet(idl)
}
func (r *DeployFlowReconciler) pullOutPodsForRestart(idl *internaldeploy.Deploy) error {
logger := log.WithField("deploy", idl.Name)
ptd, err := r.getPodsForDeletion(idl)
if err != nil {
return err
}
batchSize := idl.CurrentBatchSize()
var p string
for i := 0; i < batchSize; i++ {
if len(ptd) == 0 {
break
}
p, ptd = ptd[0], ptd[1:]
logger.Infof("Deleting pod %s", p)
if err := DeletePod(idl.Namespace, p, r.Client); err != nil {
logger.WithError(err).Errorf("Failed to delete pod %s", p)
}
}
return nil
}
func (r *DeployFlowReconciler) getPodsForDeletion(idl *internaldeploy.Deploy) ([]string, error) {
s := workload.GetDefaultSelector(idl.Spec.Application.AppID, idl.Spec.Application.GroupID)
pods := &corev1.PodList{}
if err := r.List(context.TODO(), pods, client.InNamespace(idl.Namespace), client.MatchingLabelsSelector{Selector: s.AsSelector()}); err != nil {
return nil, errors.Wrap(err, "failed to fetch pods")
}
newPods := sets.NewString(idl.Status.Pods...)
ptd := sets.NewString(idl.NonUpdateStrategy().PodsToDelete...)
readyPods := make([]string, 0, len(pods.Items))
notReadyPods := make([]string, 0, len(pods.Items))
for _, p := range pods.Items {
if !p.DeletionTimestamp.IsZero() {
continue
}
if ptd.Len() > 0 {
if !ptd.Has(p.Name) {
continue
}
} else if newPods.Has(p.Name) {
continue
}
ip := internalpod.FromPod(&p)
if ip.Ready() {
readyPods = append(readyPods, ip.Name)
} else {
notReadyPods = append(notReadyPods, p.Name)
}
}
return append(notReadyPods, readyPods...), nil
}
func (r *DeployFlowReconciler) checkBakingGate(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
if !idl.SkipPullIn() {
logger.Info("Checking if all pods are pulled in")
if time.Since(idl.CurrentBatchPullInAt().Time) > 20*time.Second {
pods := idl.CurrentBatchPods()
failedPods := make([]string, 0, len(pods))
for i := range pods {
if pods[i].PullInStatus != setting.PodPullInSucceeded {
pods[i].PullInStatus = setting.PodPullInFailed
failedPods = append(failedPods, pods[i].Name)
}
}
if len(failedPods) > 0 {
logger.Infof("Timeout waiting for all pods to be enabled in nacos, failed pods are %s", strings.Join(failedPods, ", "))
batch := idl.CurrentBatchInfo()
batch.Pods = pods
idl.SetCondition(*batch)
}
logger.Info("Marking current batch as baking")
idl.MarkCurrentBatchAsBaking()
return nil
}
enabledPods, ok := r.isAllInstancesUp(idl)
if len(enabledPods) > 0 {
ep := sets.NewString(enabledPods...)
pods := idl.CurrentBatchPods()
for i := range pods {
if ep.Has(pods[i].Name) {
pods[i].PullInStatus = setting.PodPullInSucceeded
}
}
batch := idl.CurrentBatchInfo()
batch.Pods = pods
idl.SetCondition(*batch)
}
if !ok {
logger.Info("Not all instances are pulled in, checking again")
return terrors.NewInstanceNotUpError(500 * time.Millisecond)
}
}
logger.Info("Marking current batch as baking")
idl.MarkCurrentBatchAsBaking()
return nil
}
func (r *DeployFlowReconciler) setPodReadinessGates(idl *internaldeploy.Deploy) error {
logger := r.logger.WithField("deploy", idl)
for _, p := range idl.CurrentBatchPods() {
logger.Infof("Setting readiness gate for pod %s as True", p.Name)
err := internalpod.SetPodReadinessGate(idl.Namespace, p.Name, r.Client)
if err != nil {
logger.WithError(err).Errorf("Failed to update pod %s", p.Name)
return err
}
}
return nil
}
func (r *DeployFlowReconciler) isAllInstancesUp(idl *internaldeploy.Deploy) ([]string, bool) {
logger := r.logger.WithField("deploy", idl)
logger.Info("Checking instances status")
var wg sync.WaitGroup
pods := idl.CurrentBatchPods()
var finishedPods int32
mu := &sync.Mutex{}
enabledPods := make([]string, 0, len(pods))
for i := range pods {
wg.Add(1)
go func(i int) {
defer wg.Done()
ip := pods[i].IP
phase := pods[i].Phase
podLogger := logger.WithField("ip", ip).WithField("name", pods[i].Name)
if pods[i].PullInStatus == setting.PodPullInSucceeded || phase == setting.PodFailed {
atomic.AddInt32(&finishedPods, 1)
return
}
if phase != setting.PodReady {
podLogger.Warnf("Exception: pod is in an unexpected phase %s", pods[i].Phase)
return
}
// TODO replace with readinessGate
//if r.nacosClient.IsInstanceEnabled(idl.Spec.AppName, ip) {
podLogger.Info("All pods in current batch pull in nacos success")
mu.Lock()
enabledPods = append(enabledPods, pods[i].Name)
mu.Unlock()
atomic.AddInt32(&finishedPods, 1)
return
//}
}(i)
}
wg.Wait()
return enabledPods, finishedPods == int32(len(pods))
}
func (r *DeployFlowReconciler) registerPods(idl *internaldeploy.Deploy) error {
logger := log.WithField("deploy_name", idl.Name)
batch := idl.CurrentBatchInfo()
if batch == nil {
return fmt.Errorf("current batch is missing, batches are %v", idl.Status.Conditions)
}
var lastErr error
for i := range batch.Pods {
logger.Infof("Start to register pod %s.", batch.Pods[i].Name)
//if err := r.gatewayClient.Add(idl.Spec.AppID, batch.Pods[i].IP, batch.Pods[i].Port); err != nil {
// logger.WithError(err).Errorf("failed to resister pod %s", batch.Pods[i].Name)
// lastErr = err
//}
logger.Infof("Success register pod %s, podIp is %s", batch.Pods[i].Name, batch.Pods[i].IP)
}
return lastErr
}
func (r *DeployFlowReconciler) postDeploy(idl *internaldeploy.Deploy) {
logger := r.logger.WithField("deploy", idl)
logger.Info("Deploy is finished, releasing resources")
if err := r.removeCloneSetOwnerWithRetry(idl); err != nil {
logger.WithError(err).Error("Failed to remove CloneSet owner")
}
}
// DeleteCloneSetWhenActionIsScaleInZero handle zero replicas cloneset
func (r *DeployFlowReconciler) DeleteCloneSetWhenActionIsScaleInZero(dl *tritonappsv1alpha1.DeployFlow) error {
idl := internaldeploy.FromDeploy(dl)
if idl.Spec.Action == setting.ScaleIn && idl.Finished() && *idl.Spec.Application.Replicas == 0 {
// get cloneSet owned by deployflow
cs, found, err := fetcher.GetCloneSetInCacheByDeploy(dl, r.Client)
if err != nil || !found {
r.logger.Errorf("failed to found cloneSet %s, error is %v", idl.Spec.Application.InstanceName, err)
return fmt.Errorf("failed to found cloneSet %s, error is %v", idl.Spec.Application.InstanceName, err)
}
err = r.Client.Delete(context.TODO(), cs)
if err != nil {
r.logger.Errorf("failed to delete cloneSet %s", idl.Spec.Application.InstanceName)
return err
}
}
return nil
}
func DeletePod(ns, name string, cl client.Client) error {
err := cl.Delete(context.TODO(), &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
},
})
return client.IgnoreNotFound(err)
}