Skip to content

Commit

Permalink
Use MarkStatus* helpers in pkg/pod/status
Browse files Browse the repository at this point in the history
Use MarkStatus* helper functions in pkg/pod/status.go
Closes #2770
  • Loading branch information
adshmh authored and tekton-robot committed Jul 16, 2020
1 parent d8caa70 commit 76aff13
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 30 deletions.
65 changes: 35 additions & 30 deletions pkg/pod/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,7 @@ func MakeTaskRunStatus(logger *zap.SugaredLogger, tr v1beta1.TaskRun, pod *corev
trs := &tr.Status
if trs.GetCondition(apis.ConditionSucceeded) == nil || trs.GetCondition(apis.ConditionSucceeded).Status == corev1.ConditionUnknown {
// If the taskRunStatus doesn't exist yet, it's because we just started running
trs.SetCondition(&apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: v1beta1.TaskRunReasonRunning.String(),
Message: "Not all Steps in the Task have finished executing",
})
MarkStatusRunning(trs, v1beta1.TaskRunReasonRunning.String(), "Not all Steps in the Task have finished executing")
}

trs.PodName = pod.Name
Expand Down Expand Up @@ -190,19 +185,9 @@ func removeStartInfoFromTerminationMessage(s corev1.ContainerStatus) (string, *m
func updateCompletedTaskRun(trs *v1beta1.TaskRunStatus, pod *corev1.Pod) {
if DidTaskRunFail(pod) {
msg := getFailureMessage(pod)
trs.SetCondition(&apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: v1beta1.TaskRunReasonFailed.String(),
Message: msg,
})
MarkStatusFailure(trs, msg)
} else {
trs.SetCondition(&apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: v1beta1.TaskRunReasonSuccessful.String(),
Message: "All Steps have completed executing",
})
MarkStatusSuccess(trs)
}

// update tr completed time
Expand All @@ -212,12 +197,7 @@ func updateCompletedTaskRun(trs *v1beta1.TaskRunStatus, pod *corev1.Pod) {
func updateIncompleteTaskRun(trs *v1beta1.TaskRunStatus, pod *corev1.Pod) {
switch pod.Status.Phase {
case corev1.PodRunning:
trs.SetCondition(&apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: v1beta1.TaskRunReasonRunning.String(),
Message: "Not all Steps in the Task have finished executing",
})
MarkStatusRunning(trs, v1beta1.TaskRunReasonRunning.String(), "Not all Steps in the Task have finished executing")
case corev1.PodPending:
var reason, msg string
switch {
Expand All @@ -231,12 +211,7 @@ func updateIncompleteTaskRun(trs *v1beta1.TaskRunStatus, pod *corev1.Pod) {
reason = ReasonPending
msg = getWaitingMessage(pod)
}
trs.SetCondition(&apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: reason,
Message: msg,
})
MarkStatusRunning(trs, reason, msg)
}
}

Expand Down Expand Up @@ -365,6 +340,36 @@ func getWaitingMessage(pod *corev1.Pod) string {
return "Pending"
}

// MarkStatusRunning sets taskrun status to running
func MarkStatusRunning(trs *v1beta1.TaskRunStatus, reason, message string) {
trs.SetCondition(&apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: reason,
Message: message,
})
}

// MarkStatusFailure sets taskrun status to failure
func MarkStatusFailure(trs *v1beta1.TaskRunStatus, message string) {
trs.SetCondition(&apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: v1beta1.TaskRunReasonFailed.String(),
Message: message,
})
}

// MarkStatusSuccess sets taskrun status to success
func MarkStatusSuccess(trs *v1beta1.TaskRunStatus) {
trs.SetCondition(&apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: v1beta1.TaskRunReasonSuccessful.String(),
Message: "All Steps have completed executing",
})
}

// sortTaskRunStepOrder sorts the StepStates in the same order as the original
// TaskSpec steps.
func sortTaskRunStepOrder(taskRunSteps []v1beta1.StepState, taskSpecSteps []v1beta1.Step) []v1beta1.StepState {
Expand Down
49 changes: 49 additions & 0 deletions pkg/pod/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/logging"
"github.com/tektoncd/pipeline/test/diff"
Expand Down Expand Up @@ -922,3 +923,51 @@ func TestSortContainerStatuses(t *testing.T) {
}

}

func TestMarkStatusRunning(t *testing.T) {
trs := v1beta1.TaskRunStatus{}
MarkStatusRunning(&trs, v1beta1.TaskRunReasonRunning.String(), "Not all Steps in the Task have finished executing")

expected := &apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionUnknown,
Reason: v1beta1.TaskRunReasonRunning.String(),
Message: "Not all Steps in the Task have finished executing",
}

if d := cmp.Diff(expected, trs.GetCondition(apis.ConditionSucceeded), cmpopts.IgnoreTypes(apis.Condition{}.LastTransitionTime.Inner.Time)); d != "" {
t.Errorf("Unexpected status: %s", diff.PrintWantGot(d))
}
}

func TestMarkStatusFailure(t *testing.T) {
trs := v1beta1.TaskRunStatus{}
MarkStatusFailure(&trs, "failure message")

expected := &apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: v1beta1.TaskRunReasonFailed.String(),
Message: "failure message",
}

if d := cmp.Diff(expected, trs.GetCondition(apis.ConditionSucceeded), cmpopts.IgnoreTypes(apis.Condition{}.LastTransitionTime.Inner.Time)); d != "" {
t.Errorf("Unexpected status: %s", diff.PrintWantGot(d))
}
}

func TestMarkStatusSuccess(t *testing.T) {
trs := v1beta1.TaskRunStatus{}
MarkStatusSuccess(&trs)

expected := &apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionTrue,
Reason: v1beta1.TaskRunReasonSuccessful.String(),
Message: "All Steps have completed executing",
}

if d := cmp.Diff(expected, trs.GetCondition(apis.ConditionSucceeded), cmpopts.IgnoreTypes(apis.Condition{}.LastTransitionTime.Inner.Time)); d != "" {
t.Errorf("Unexpected status: %s", diff.PrintWantGot(d))
}
}

0 comments on commit 76aff13

Please sign in to comment.