Kenchu123 commented on a change in pull request #719:
URL: https://github.com/apache/submarine/pull/719#discussion_r696298282



##########
File path: submarine-cloud-v2/pkg/controller/controller.go
##########
@@ -531,3 +561,149 @@ func (c *Controller) handleObject(obj interface{}) {
                return
        }
 }
+
+func (c *Controller) getSubmarine(namespace, name string) 
(*v1alpha1.Submarine, error) {
+       submarine, err := c.submarinesLister.Submarines(namespace).Get(name)
+       if err != nil {
+               // The Submarine resource may no longer exist, in which case we 
stop
+               // processing

Review comment:
       Ok, thanks.

##########
File path: submarine-cloud-v2/pkg/controller/controller.go
##########
@@ -380,102 +383,129 @@ func (c *Controller) processNextWorkItem() bool {
 // syncHandler compares the actual state with the desired, and attempts to
 // converge the two. It then updates the Status block of the Submarine resource
 // with the current status of the resource.
+// State Machine for Submarine
+//+-----------------------------------------------------------------+
+//|      +---------+         +----------+          +----------+     |
+//|      |         |         |          |          |          |     |
+//|      |   New   +---------> Creating +----------> Running  |     |
+//|      |         |         |          |          |          |     |
+//|      +----+----+         +-----+----+          +-----+----+     |
+//|           |                    |                     |          |
+//|           |                    |                     |          |
+//|           |                    |                     |          |
+//|           |                    |               +-----v----+     |
+//|           |                    |               |          |     |
+//|           +--------------------+--------------->  Failed  |     |
+//|                                                |          |     |
+//|                                                +----------+     |
+//+-----------------------------------------------------------------+
 func (c *Controller) syncHandler(key string) error {
        // Convert the namespace/name string into a distinct namespace and name
        namespace, name, err := cache.SplitMetaNamespaceKey(key)
        if err != nil {
-               utilruntime.HandleError(fmt.Errorf("Invalid resource key: %s", 
key))
+               utilruntime.HandleError(fmt.Errorf("invalid resource key: %s", 
key))
                return nil
        }
        klog.Info("syncHandler: ", key)
 
        // Get the Submarine resource with this namespace/name
-       submarine, err := c.submarinesLister.Submarines(namespace).Get(name)
+       submarine, err := c.getSubmarine(namespace, name)
        if err != nil {
+               return err
+       }
+       if submarine == nil {
                // The Submarine resource may no longer exist, in which case we 
stop
                // processing
-               if errors.IsNotFound(err) {
-                       utilruntime.HandleError(fmt.Errorf("submarine '%s' in 
work queue no longer exists", key))
-                       return nil
-               }
-               return err
+               utilruntime.HandleError(fmt.Errorf("submarine '%s' in work 
queue no longer exists", key))
+               return nil
        }
 
        // Submarine is in the terminating process
        if !submarine.DeletionTimestamp.IsZero() {
                return nil
        }
 
-       // Print out the spec of the Submarine resource
-       b, err := json.MarshalIndent(submarine.Spec, "", "  ")
-       fmt.Println(string(b))
-
-       storageType := submarine.Spec.Storage.StorageType
-       if storageType != "nfs" && storageType != "host" {
-               utilruntime.HandleError(fmt.Errorf("Invalid storageType '%s' 
found in submarine spec, nothing will be created. Valid storage types are 'nfs' 
and 'host'", storageType))
-               return nil
-       }
-
-       var serverDeployment *appsv1.Deployment
-       var databaseDeployment *appsv1.Deployment
+       submarineCopy := submarine.DeepCopy()
 
-       if err != nil {
-               return err
+       // Take action based on submarine state
+       switch submarineCopy.Status.SubmarineState.State {
+       case v1alpha1.NewState:
+               c.recordSubmarineEvent(submarineCopy)
+               if err := c.validateSubmarine(submarineCopy); err != nil {
+                       submarineCopy.Status.SubmarineState.State = 
v1alpha1.FailedState
+                       submarineCopy.Status.SubmarineState.ErrorMessage = 
err.Error()
+                       c.recordSubmarineEvent(submarineCopy)
+               } else {
+                       submarineCopy.Status.SubmarineState.State = 
v1alpha1.CreatingState
+                       c.recordSubmarineEvent(submarineCopy)
+               }
+       case v1alpha1.CreatingState:
+               if err := c.createSubmarine(submarineCopy); err != nil {
+                       submarineCopy.Status.SubmarineState.State = 
v1alpha1.FailedState
+                       submarineCopy.Status.SubmarineState.ErrorMessage = 
err.Error()
+                       c.recordSubmarineEvent(submarineCopy)
+               }
+               ok, err := c.checkSubmarineDependentsReady(submarineCopy)
+               if err != nil {
+                       submarineCopy.Status.SubmarineState.State = 
v1alpha1.FailedState
+                       submarineCopy.Status.SubmarineState.ErrorMessage = 
err.Error()
+                       c.recordSubmarineEvent(submarineCopy)
+               }
+               if ok {
+                       submarineCopy.Status.SubmarineState.State = 
v1alpha1.RunningState
+                       c.recordSubmarineEvent(submarineCopy)
+               }
+       case v1alpha1.RunningState:
+               if err := c.createSubmarine(submarineCopy); err != nil {
+                       submarineCopy.Status.SubmarineState.State = 
v1alpha1.FailedState
+                       submarineCopy.Status.SubmarineState.ErrorMessage = 
err.Error()
+                       c.recordSubmarineEvent(submarineCopy)
+               }
        }
 
-       serverDeployment, err = c.createSubmarineServer(submarine)
-       if err != nil {
-               return err
+       // update submarine status
+       if submarineCopy != nil {

Review comment:
       Ok, thanks.

##########
File path: submarine-cloud-v2/pkg/controller/controller.go
##########
@@ -531,3 +561,149 @@ func (c *Controller) handleObject(obj interface{}) {
                return
        }
 }
+
+func (c *Controller) getSubmarine(namespace, name string) 
(*v1alpha1.Submarine, error) {
+       submarine, err := c.submarinesLister.Submarines(namespace).Get(name)
+       if err != nil {
+               // The Submarine resource may no longer exist, in which case we 
stop
+               // processing
+               if errors.IsNotFound(err) {
+                       return nil, nil
+               }
+               return nil, err
+       }
+       return submarine, nil
+}
+
+func (c *Controller) getDeployment(namespace, name string) 
(*appsv1.Deployment, error) {
+       deployment, err := c.deploymentLister.Deployments(namespace).Get(name)
+       if err != nil {
+               if errors.IsNotFound(err) {
+                       return nil, nil
+               }
+               return nil, err
+       }
+       return deployment, nil
+}
+
+func (c *Controller) validateSubmarine(submarine *v1alpha1.Submarine) error {
+
+       // Print out the spec of the Submarine resource
+       b, err := json.MarshalIndent(submarine.Spec, "", "  ")
+       fmt.Println(string(b))
+
+       if err != nil {
+               return err
+       }
+
+       // Check storage type
+       storageType := submarine.Spec.Storage.StorageType
+       if storageType != "nfs" && storageType != "host" {
+               utilruntime.HandleError(fmt.Errorf("invalid storageType '%s' 
found in submarine spec, nothing will be created. Valid storage types are 'nfs' 
and 'host'", storageType))
+               return nil
+       }

Review comment:
       Ok, thanks.




-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to