handle ending reconcile with conditions (#1859)

Signed-off-by: roywang <seiwy2010@gmail.com>
This commit is contained in:
Yue Wang
2021-07-06 13:54:23 +08:00
committed by GitHub
parent 4930c2c71f
commit fba53cfbf3
22 changed files with 378 additions and 284 deletions
@@ -99,16 +99,16 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
if err != nil {
log.Error(err, "Failed to render a deployment")
r.record.Event(eventObj, event.Warning(errRenderDeployment, err))
return util.ReconcileWaitResult,
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errRenderDeployment)))
return ctrl.Result{},
util.EndReconcileWithNegativeCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errRenderDeployment)))
}
// server side apply
applyOpts := []client.PatchOption{client.ForceOwnership, client.FieldOwner(workload.GetUID())}
if err := r.Patch(ctx, deploy, client.Apply, applyOpts...); err != nil {
log.Error(err, "Failed to apply to a deployment")
r.record.Event(eventObj, event.Warning(errApplyDeployment, err))
return util.ReconcileWaitResult,
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errApplyDeployment)))
return ctrl.Result{},
util.EndReconcileWithNegativeCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errApplyDeployment)))
}
r.record.Event(eventObj, event.Normal("Deployment created",
fmt.Sprintf("Workload `%s` successfully patched a deployment `%s`",
@@ -132,15 +132,15 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
if err != nil {
log.Error(err, "Failed to render a service")
r.record.Event(eventObj, event.Warning(errRenderService, err))
return util.ReconcileWaitResult,
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errRenderService)))
return ctrl.Result{},
util.EndReconcileWithNegativeCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errRenderService)))
}
// server side apply the service
if err := r.Patch(ctx, service, client.Apply, applyOpts...); err != nil {
log.Error(err, "Failed to apply a service")
r.record.Event(eventObj, event.Warning(errApplyDeployment, err))
return util.ReconcileWaitResult,
util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errApplyService)))
return ctrl.Result{},
util.EndReconcileWithNegativeCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(errors.Wrap(err, errApplyService)))
}
r.record.Event(eventObj, event.Normal("Service created",
fmt.Sprintf("Workload `%s` successfully server side patched a service `%s`",
@@ -155,10 +155,11 @@ func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
})
}
workload.SetConditions(cpv1alpha1.ReconcileSuccess())
if err := r.UpdateStatus(ctx, &workload); err != nil {
return util.ReconcileWaitResult, err
return ctrl.Result{}, util.EndReconcileWithNegativeCondition(ctx, r, &workload, cpv1alpha1.ReconcileError(err))
}
return ctrl.Result{}, util.PatchCondition(ctx, r, &workload, cpv1alpha1.ReconcileSuccess())
return ctrl.Result{}, nil
}
// create a corresponding deployment