Skip to content

Commit

Permalink
better handling of did_complete logic
Browse files Browse the repository at this point in the history
  • Loading branch information
isaaguilar committed Aug 24, 2023
1 parent dc0c7ae commit 47a69fe
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions pkg/api/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,29 +308,41 @@ 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"`
CurrentState string `json:"current_state"`
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(),
},
}

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

}

// 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) {
Expand Down

0 comments on commit 47a69fe

Please sign in to comment.