diff --git a/pkg/api/resource.go b/pkg/api/resource.go index 489b50a..6c484c7 100644 --- a/pkg/api/resource.go +++ b/pkg/api/resource.go @@ -308,11 +308,6 @@ func (h APIHandler) ResourceStatusCheck(c *gin.Context) { return } - didStart := resource.Generation == resource.Status.Stage.Generation - didComplete := resource.Status.Phase == tfv1beta1.PhaseCompleted - currentState := resource.Status.Stage.State - currentTask := resource.Status.Stage.TaskType - responseJSONData := []struct { DidStart bool `json:"did_start"` DidComplete bool `json:"did_complete"` @@ -320,10 +315,10 @@ func (h APIHandler) ResourceStatusCheck(c *gin.Context) { CurrentTask string `json:"current_task"` }{ { - DidStart: didStart, - DidComplete: didComplete, - CurrentState: string(currentState), - CurrentTask: currentTask.String(), + DidStart: resource.Generation == resource.Status.Stage.Generation, + DidComplete: !IsWorkflowRunning(resource.Status), + CurrentState: string(resource.Status.Stage.State), + CurrentTask: resource.Status.Stage.TaskType.String(), }, } @@ -331,6 +326,23 @@ func (h APIHandler) ResourceStatusCheck(c *gin.Context) { } +// Take into account pod phases as well as task state to determine if the workflow is still running. +func IsWorkflowRunning(status tfv1beta1.TerraformStatus) bool { + switch status.Stage.State { + case tfv1beta1.StateFailed, tfv1beta1.StageState(corev1.PodFailed): + // Failed states trump all, the workflow is not running after a failure + return false + case tfv1beta1.StageState(corev1.PodRunning), tfv1beta1.StageState(corev1.PodPending): + // When the pod is claimed to be running, the workflow is still running + return true + default: + if status.Phase != tfv1beta1.PhaseCompleted { + return true + } + return false + } +} + // ResourcePoll is a short poll that checks and returns resources created from the tf resource's workflow // that have the correct label and annotation value. func (h APIHandler) ResourcePoll(c *gin.Context) {