Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Commit dcebc07

Browse files
authored
Merge pull request #197 from sofastack/delete_in_batches
delete modules in batches
2 parents ea1c8cc + 981f716 commit dcebc07

8 files changed

+150
-47
lines changed

.github/workflows/module_controller_ci_build_deploy_to_aliyun.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ jobs:
176176
exit 0
177177
else
178178
echo "等待字段值满足条件..."
179+
echo "期望状态是: $desired_field_value, 当前状态是: $field_value"
179180
sleep 5 # 等待一段时间后再次检查
180181
fi
181182
done
@@ -487,4 +488,4 @@ jobs:
487488
fi
488489
sleep 5
489490
fi
490-
done
491+
done

module-controller/api/v1alpha1/moduledeployment_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ const (
4848
ModuleDeploymentReleaseProgressPaused ReleaseProgress = "Paused"
4949
ModuleDeploymentReleaseProgressCompleted ReleaseProgress = "Completed"
5050
ModuleDeploymentReleaseProgressAborted ReleaseProgress = "Aborted"
51-
ModuleDeploymentReleaseProgressTermed ReleaseProgress = "Terminated"
51+
ModuleDeploymentReleaseProgressTerminating ReleaseProgress = "Terminating"
52+
ModuleDeploymentReleaseProgressTerminated ReleaseProgress = "Terminated"
5253
)
5354

5455
type ModuleUpgradeType string
@@ -120,7 +121,7 @@ type ModuleOperationStrategy struct {
120121

121122
BatchCount int32 `json:"batchCount,omitempty"`
122123

123-
MaxUnavailable *intstr.IntOrString `json:"maxUnavailable,omitempty"`
124+
MaxUnavailable int32 `json:"maxUnavailable,omitempty"`
124125

125126
GrayTimeBetweenBatchSeconds int32 `json:"grayTimeBetweenBatchSeconds,omitempty"`
126127

module-controller/api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 2 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

module-controller/config/crd/bases/serverless.alipay.com_moduledeployments.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ spec:
5353
format: int32
5454
type: integer
5555
maxUnavailable:
56-
anyOf:
57-
- type: integer
58-
- type: string
59-
x-kubernetes-int-or-string: true
56+
format: int32
57+
type: integer
6058
needConfirm:
6159
type: boolean
6260
serviceStrategy:

module-controller/config/crd/bases/serverless.alipay.com_modulereplicasets.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ spec:
4848
format: int32
4949
type: integer
5050
maxUnavailable:
51-
anyOf:
52-
- type: integer
53-
- type: string
54-
x-kubernetes-int-or-string: true
51+
format: int32
52+
type: integer
5553
needConfirm:
5654
type: boolean
5755
serviceStrategy:

module-controller/internal/controller/moduledeployment_controller.go

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,15 @@ func (r *ModuleDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Req
8282
}
8383

8484
if moduleDeployment.DeletionTimestamp != nil {
85-
// delete moduleDeployment
8685
event.PublishModuleDeploymentDeleteEvent(r.Client, ctx, moduleDeployment)
87-
return r.handleDeletingModuleDeployment(ctx, moduleDeployment)
86+
if !utils.HasFinalizer(&moduleDeployment.ObjectMeta, finalizer.ModuleReplicaSetExistedFinalizer) &&
87+
!utils.HasFinalizer(&moduleDeployment.ObjectMeta, finalizer.ModuleExistedFinalizer) {
88+
if moduleDeployment.Status.ReleaseStatus.Progress != v1alpha1.ModuleDeploymentReleaseProgressTerminated {
89+
moduleDeployment.Status.ReleaseStatus.Progress = v1alpha1.ModuleDeploymentReleaseProgressTerminated
90+
return ctrl.Result{}, r.Status().Update(ctx, moduleDeployment)
91+
}
92+
return ctrl.Result{}, nil
93+
}
8894
}
8995

9096
if moduleDeployment.Generation == 1 {
@@ -129,6 +135,10 @@ func (r *ModuleDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Req
129135
case v1alpha1.ModuleDeploymentReleaseProgressExecuting:
130136
return r.updateModuleReplicaSet(ctx, moduleDeployment, newRS)
131137
case v1alpha1.ModuleDeploymentReleaseProgressCompleted:
138+
if moduleDeployment.DeletionTimestamp != nil {
139+
moduleDeployment.Status.ReleaseStatus.Progress = v1alpha1.ModuleDeploymentReleaseProgressTerminating
140+
return ctrl.Result{}, r.Status().Update(ctx, moduleDeployment)
141+
}
132142
if moduleDeployment.Spec.Replicas != newRS.Spec.Replicas {
133143
moduleDeployment.Status.ReleaseStatus.Progress = v1alpha1.ModuleDeploymentReleaseProgressInit
134144
log.Log.Info("update release status progress to init when complete moduleDeployment", "moduleDeploymentName", moduleDeployment.Name)
@@ -161,6 +171,27 @@ func (r *ModuleDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Req
161171
return ctrl.Result{}, utils.Error(err, "update moduleDeployment progress from paused to executing failed")
162172
}
163173
}
174+
case v1alpha1.ModuleDeploymentReleaseProgressTerminating:
175+
// delete modules
176+
if utils.HasFinalizer(&moduleDeployment.ObjectMeta, finalizer.ModuleExistedFinalizer) {
177+
if moduleDeployment.Spec.Replicas != 0 {
178+
moduleDeployment.Spec.Replicas = 0
179+
return ctrl.Result{}, r.Update(ctx, moduleDeployment)
180+
}
181+
if newRS.Status.Replicas != 0 {
182+
handleInitModuleDeployment(moduleDeployment, newRS)
183+
return ctrl.Result{}, r.Status().Update(ctx, moduleDeployment)
184+
}
185+
utils.RemoveFinalizer(&moduleDeployment.ObjectMeta, finalizer.ModuleExistedFinalizer)
186+
return ctrl.Result{}, r.Update(ctx, moduleDeployment)
187+
}
188+
189+
// delete module replicaset
190+
if utils.HasFinalizer(&moduleDeployment.ObjectMeta, finalizer.ModuleReplicaSetExistedFinalizer) {
191+
return r.handleDeletingModuleDeployment(ctx, moduleDeployment)
192+
}
193+
case v1alpha1.ModuleDeploymentReleaseProgressTerminated:
194+
return ctrl.Result{}, nil
164195
}
165196
return ctrl.Result{}, nil
166197
}
@@ -190,10 +221,6 @@ func handleInitModuleDeployment(moduleDeployment *v1alpha1.ModuleDeployment, new
190221

191222
// handle deleting module deployment
192223
func (r *ModuleDeploymentReconciler) handleDeletingModuleDeployment(ctx context.Context, moduleDeployment *v1alpha1.ModuleDeployment) (ctrl.Result, error) {
193-
if !utils.HasFinalizer(&moduleDeployment.ObjectMeta, finalizer.ModuleReplicaSetExistedFinalizer) {
194-
return ctrl.Result{}, nil
195-
}
196-
197224
existReplicaset := true
198225
set := map[string]string{
199226
label.ModuleDeploymentLabel: moduleDeployment.Name,
@@ -218,6 +245,7 @@ func (r *ModuleDeploymentReconciler) handleDeletingModuleDeployment(ctx context.
218245
return ctrl.Result{}, utils.Error(err, "Failed to delete moduleReplicaSet", "moduleReplicaSetName", replicaSetList.Items[i].Name)
219246
}
220247
}
248+
221249
requeueAfter := utils.GetNextReconcileTime(moduleDeployment.DeletionTimestamp.Time)
222250
return ctrl.Result{RequeueAfter: requeueAfter}, nil
223251
} else {
@@ -249,6 +277,7 @@ func (r *ModuleDeploymentReconciler) updateOwnerReference(ctx context.Context, m
249277
})
250278
moduleDeployment.SetOwnerReferences(ownerReference)
251279
utils.AddFinalizer(&moduleDeployment.ObjectMeta, finalizer.ModuleReplicaSetExistedFinalizer)
280+
utils.AddFinalizer(&moduleDeployment.ObjectMeta, finalizer.ModuleExistedFinalizer)
252281
err = utils.UpdateResource(r.Client, ctx, moduleDeployment)
253282
if err != nil {
254283
return utils.Error(err, "Failed to update moduleDeployment", "moduleDeploymentName", moduleDeployment.Name)
@@ -304,6 +333,9 @@ func (r *ModuleDeploymentReconciler) createOrGetModuleReplicas(ctx context.Conte
304333
log.Log.Info("module has changed, need create a new replicaset")
305334
}
306335

336+
if moduleDeployment.DeletionTimestamp != nil {
337+
return nil, nil, false, nil
338+
}
307339
// create a new moduleReplicaset
308340
moduleReplicaSet, err := r.createNewReplicaSet(ctx, moduleDeployment, maxVersion+1)
309341
if err != nil {
@@ -381,6 +413,7 @@ func (r *ModuleDeploymentReconciler) updateModuleReplicaSet(ctx context.Context,
381413
}
382414

383415
err := r.updateModuleReplicas(ctx, replicas, moduleDeployment, newRS)
416+
384417
if err != nil {
385418
return ctrl.Result{}, err
386419
}

0 commit comments

Comments
 (0)