This is an automated email from the ASF dual-hosted git repository.

kaihsun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/submarine.git


The following commit(s) were added to refs/heads/master by this push:
     new b30d879  SUBMARINE-829. Handle errors when processing workqueue items
b30d879 is described below

commit b30d87903153357904d48d3743434d8501f91e00
Author: MortalHappiness <[email protected]>
AuthorDate: Wed Jun 30 20:50:53 2021 +0800

    SUBMARINE-829. Handle errors when processing workqueue items
    
    ### What is this PR for?
    Currently we do not handle error when processing workqueue items, we need 
to handle errors when processing workqueue items.
    
    Reference: 
https://github.com/kubernetes/sample-controller/blob/73e81dab82c945b087f7db1f1f4a7b50e7d92751/controller.go#L195-L230
    
    ### What type of PR is it?
    [Feature]
    
    ### Todos
    
    ### What is the Jira issue?
    https://issues.apache.org/jira/projects/SUBMARINE/issues/SUBMARINE-829
    
    ### How should this be tested?
    
    ### Screenshots (if appropriate)
    
    ### Questions:
    * Do the license files need updating? No
    * Are there breaking changes for older versions? No
    * Does this need new documentation? No
    
    Author: MortalHappiness <[email protected]>
    
    Signed-off-by: Kai-Hsun Chen <[email protected]>
    
    Closes #626 from MortalHappiness/SUBMARINE-829 and squashes the following 
commits:
    
    9a5bb6ac [MortalHappiness] SUBMARINE-829. Handle errors when processing 
workqueue items
---
 submarine-cloud-v2/controller.go | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/submarine-cloud-v2/controller.go b/submarine-cloud-v2/controller.go
index 2b1cbcb..1a60f84 100644
--- a/submarine-cloud-v2/controller.go
+++ b/submarine-cloud-v2/controller.go
@@ -359,12 +359,25 @@ func (c *Controller) processNextWorkItem() bool {
 
        // We wrap this block in a func so we can defer c.workqueue.Done.
        err := func(obj interface{}) error {
-               // TODO: Maintain workqueue
                defer c.workqueue.Done(obj)
                var item WorkQueueItem
-
-               item, _ = obj.(WorkQueueItem)
-               c.syncHandler(item)
+               var ok bool
+               if item, ok = obj.(WorkQueueItem); !ok {
+                       // As the item in the workqueue is actually invalid, we 
call
+                       // Forget here else we'd go into a loop of attempting to
+                       // process a work item that is invalid.
+                       c.workqueue.Forget(obj)
+                       utilruntime.HandleError(fmt.Errorf("expected 
WorkQueueItem in workqueue but got %#v", obj))
+                       return nil
+               }
+               // Run the syncHandler
+               if err := c.syncHandler(item); err != nil {
+                       // Put the item back on the workqueue to handle any 
transient errors.
+                       c.workqueue.AddRateLimited(item)
+                       return fmt.Errorf("error syncing '%s': %s, requeuing", 
item.key, err.Error())
+               }
+               // Finally, if no error occurs we Forget this item so it does 
not
+               // get queued again until another change happens.
                c.workqueue.Forget(obj)
                klog.Infof("Successfully synced '%s'", item.key)
                return nil
@@ -1340,6 +1353,7 @@ func (c *Controller) syncHandler(workqueueItem 
WorkQueueItem) error {
                                utilruntime.HandleError(fmt.Errorf("submarine 
'%s' in work queue no longer exists", key))
                                return nil
                        }
+                       return err
                }
 
                // Print out the spec of the Submarine resource
@@ -1347,7 +1361,10 @@ func (c *Controller) syncHandler(workqueueItem 
WorkQueueItem) error {
                fmt.Println(string(b))
 
                // Install subcharts
-               c.newSubCharts(namespace)
+               err = c.newSubCharts(namespace)
+               if err != nil {
+                       return err
+               }
 
                // Create submarine-server
                serverImage := submarine.Spec.Server.Image
@@ -1384,13 +1401,13 @@ func (c *Controller) syncHandler(workqueueItem 
WorkQueueItem) error {
                        return err
                }
 
+               c.recorder.Event(submarine, corev1.EventTypeNormal, 
SuccessSynced, MessageResourceSynced)
        } else { // Case: DELETE
                // Uninstall Helm charts
                for _, chart := range c.charts {
                        helm.HelmUninstall(chart)
                }
                c.charts = nil
-
        }
 
        return nil

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to