astefanutti commented on a change in pull request #784: Refactor controller 
logic
URL: https://github.com/apache/camel-k/pull/784#discussion_r299403507
 
 

 ##########
 File path: pkg/controller/build/build_controller.go
 ##########
 @@ -170,33 +170,59 @@ func (r *ReconcileBuild) Reconcile(request 
reconcile.Request) (reconcile.Result,
                NewErrorRecoveryAction(),
        }
 
-       blog := rlog.ForBuild(instance)
-       for _, a := range buildActionPool {
+       var targetPhase v1alpha1.BuildPhase
+       var err error
+
+       target := instance.DeepCopy()
+       targetLog := rlog.ForBuild(target)
+
+       for _, a := range actions {
                a.InjectClient(r.client)
-               a.InjectLogger(blog)
-               if a.CanHandle(instance) {
-                       blog.Infof("Invoking action %s", a.Name())
-                       if err := a.Handle(ctx, instance); err != nil {
-                               if k8serrors.IsConflict(err) {
-                                       blog.Error(err, "conflict")
-                                       return reconcile.Result{
-                                               Requeue: true,
-                                       }, nil
+               a.InjectLogger(targetLog)
+
+               if a.CanHandle(target) {
+                       targetLog.Infof("Invoking action %s", a.Name())
+
+                       phaseFrom := target.Status.Phase
+
+                       target, err = a.Handle(ctx, target)
+                       if err != nil {
+                               return reconcile.Result{
+                                       Requeue: true,
+                               }, nil
+                       }
+
+                       if target != nil {
+                               if err := r.client.Status().Update(ctx, 
target); err != nil {
+                                       if k8serrors.IsConflict(err) {
+                                               targetLog.Error(err, "conflict")
+                                               return reconcile.Result{
+                                                       Requeue: true,
+                                               }, nil
+                                       }
+
+                                       return reconcile.Result{}, err
                                }
 
-                               return reconcile.Result{}, err
+                               targetPhase = target.Status.Phase
+
+                               if targetPhase != phaseFrom {
+                                       targetLog.Info(
+                                               "state transition",
+                                               "phase-from", phaseFrom,
+                                               "phase-to", target.Status.Phase,
+                                       )
+                               }
                        }
-               }
-       }
 
-       // Refresh the build and check the state
-       if err = r.client.Get(ctx, request.NamespacedName, instance); err != 
nil {
-               return reconcile.Result{}, err
+                       // handle one action at time so the resource
+                       // is always at its latest state
+                       break
+               }
        }
 
        // Requeue scheduling build so that it re-enters the build working queue
-       if instance.Status.Phase == v1alpha1.BuildPhaseScheduling ||
-               instance.Status.Phase == v1alpha1.BuildPhaseFailed {
+       if targetPhase == v1alpha1.BuildPhaseScheduling || targetPhase == 
v1alpha1.BuildPhaseFailed {
 
 Review comment:
   `targetPhase` may be empty if the schedule action returns `nil`, or 
possibly, when no action can handle the event, so it may be better to replace 
it with `target.Status.Phase`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to