Skip to content

Commit

Permalink
update status in database upon status query
Browse files Browse the repository at this point in the history
  • Loading branch information
isaaguilar committed Oct 20, 2023
1 parent 38806bd commit a8384e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (h APIHandler) RegisterRoutes() {
authenticatedTask.Use(validateTaskJWT)
authenticatedTask.POST("", h.AddTaskPod)
authenticatedTask.GET("/status", h.ResourceStatusCheckViaTask)
authenticatedTask.POST("/status", h.UpdateResourceStatus)
authenticatedTask.POST("/status", h.UpdateResourceStatusViaTask)
authenticatedTask.GET("/:task_pod_uuid/approval-status", h.GetApprovalStatusViaTaskPodUUID)

// Approval
Expand Down
32 changes: 26 additions & 6 deletions pkg/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func (h APIHandler) VClusterTFOHealth(c *gin.Context) {
c.JSON(http.StatusNoContent, nil)
}

type statusCheckResponse struct {
type StatusCheckResponse struct {
DidStart bool `json:"did_start"`
DidComplete bool `json:"did_complete"`
CurrentState string `json:"current_state"`
Expand All @@ -353,7 +353,7 @@ func (h APIHandler) ResourceStatusCheck(c *gin.Context) {
name := c.Param("name")
namespace := c.Param("namespace")

returnStatusCheck(c, h.clientset, clusterName, namespace, name)
statusCheckAndUpdate(c, h.DB, h.clientset, clusterName, namespace, name)
}

func (h APIHandler) ResourceStatusCheckViaTask(c *gin.Context) {
Expand All @@ -378,10 +378,10 @@ func (h APIHandler) ResourceStatusCheckViaTask(c *gin.Context) {
namespace := tfoResourceFromDatabase.Namespace
name := tfoResourceFromDatabase.Name

returnStatusCheck(c, h.clientset, clusterName, namespace, name)
statusCheckAndUpdate(c, h.DB, h.clientset, clusterName, namespace, name)
}

func returnStatusCheck(c *gin.Context, clientset kubernetes.Interface, clusterName, namespace, name string) {
func statusCheckAndUpdate(c *gin.Context, db *gorm.DB, clientset kubernetes.Interface, clusterName, namespace, name string) {
resource, err := getResource(clientset, clusterName, namespace, name, c)
if err != nil {
if kerrors.IsNotFound(err) {
Expand All @@ -392,18 +392,38 @@ func returnStatusCheck(c *gin.Context, clientset kubernetes.Interface, clusterNa
return
}

responseJSONData := []statusCheckResponse{
responseJSONData := []StatusCheckResponse{
{
DidStart: resource.Generation == resource.Status.Stage.Generation,
DidComplete: !IsWorkflowRunning(resource.Status),
CurrentState: string(resource.Status.Stage.State),
CurrentTask: resource.Status.Stage.TaskType.String(),
},
}

uuid := ""
OptLoop:
for _, opt := range resource.Spec.TaskOptions {
for _, env := range opt.Env {
if env.Name == "TFO_ORIGIN_UUID" {
uuid = env.Value
break OptLoop
}
}
}
if uuid != "" {
tfoResourceFromDatabase := models.TFOResource{}
result := db.Where("uuid = ?", uuid).First(&tfoResourceFromDatabase)
if result.Error == nil {
tfoResourceFromDatabase.CurrentState = models.ResourceState(responseJSONData[0].CurrentState)
db.Save(tfoResourceFromDatabase)
}
}

c.JSON(http.StatusOK, response(http.StatusOK, "", responseJSONData))
}

func (h APIHandler) UpdateResourceStatus(c *gin.Context) {
func (h APIHandler) UpdateResourceStatusViaTask(c *gin.Context) {
jsonData := struct {
Status string `json:"status"`
}{}
Expand Down

0 comments on commit a8384e9

Please sign in to comment.