-
Notifications
You must be signed in to change notification settings - Fork 20
/
run.go
102 lines (92 loc) · 2.79 KB
/
run.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package runctl
import (
steward "github.com/SAP/stewardci-core/pkg/apis/steward/v1alpha1"
run "github.com/SAP/stewardci-core/pkg/run"
tekton "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
termination "github.com/tektoncd/pipeline/pkg/termination"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
knativeapis "knative.dev/pkg/apis"
)
type tektonRun struct {
tektonTaskRun *tekton.TaskRun
}
// NewRun returns new Run
func NewRun(tektonTaskRun *tekton.TaskRun) run.Run {
return &tektonRun{tektonTaskRun: tektonTaskRun}
}
// GetStartTime returns start time of run if already started
func (r *tektonRun) GetStartTime() *metav1.Time {
return r.tektonTaskRun.Status.StartTime
}
// GetContainerInfo returns the state of the Jenkinsfile Runner container
// as reported in the Tekton TaskRun status.
func (r *tektonRun) GetContainerInfo() *corev1.ContainerState {
stepState := r.getJenkinsfileRunnerStepState()
if stepState == nil {
return nil
}
return &stepState.ContainerState
}
func (r *tektonRun) getSucceededCondition() *knativeapis.Condition {
return r.tektonTaskRun.Status.GetCondition(knativeapis.ConditionSucceeded)
}
// IsFinished returns true if run is finished
func (r *tektonRun) IsFinished() (bool, steward.Result) {
condition := r.getSucceededCondition()
if condition.IsUnknown() {
return false, steward.ResultUndefined
}
if condition.IsTrue() {
return true, steward.ResultSuccess
}
// TaskRun finished unsuccessfully, check reason...
switch condition.Reason {
case tekton.TaskRunReasonTimedOut.String():
return true, steward.ResultTimeout
case tekton.TaskRunReasonFailed.String():
jfrStepState := r.getJenkinsfileRunnerStepState()
if jfrStepState != nil && jfrStepState.Terminated != nil && jfrStepState.Terminated.ExitCode != 0 {
return true, steward.ResultErrorContent
}
default:
// TODO handle other failure reasons like quota exceedance
}
return true, steward.ResultErrorInfra
}
// GetMessage returns the termination message
func (r *tektonRun) GetMessage() string {
var msg string
containerInfo := r.GetContainerInfo()
if containerInfo != nil && containerInfo.Terminated != nil {
msg = containerInfo.Terminated.Message
}
if msg == "" {
cond := r.getSucceededCondition()
if cond != nil {
return cond.Message
}
} else {
allMessages, err := termination.ParseMessage(msg)
if err != nil {
return msg
}
for _, singleMessage := range allMessages {
if singleMessage.Key == jfrResultKey {
return singleMessage.Value
}
}
}
return "internal error"
}
func (r *tektonRun) getJenkinsfileRunnerStepState() *tekton.StepState {
steps := r.tektonTaskRun.Status.Steps
if steps != nil {
for _, stepState := range steps {
if stepState.Name == tektonClusterTaskJenkinsfileRunnerStep {
return &stepState
}
}
}
return nil
}