forked from tektoncd/pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskrun_types.go
309 lines (261 loc) · 10.1 KB
/
taskrun_types.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"fmt"
"time"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"
)
// Check that TaskRun may be validated and defaulted.
var _ apis.Validatable = (*TaskRun)(nil)
var _ apis.Defaultable = (*TaskRun)(nil)
// TaskRunSpec defines the desired state of TaskRun
type TaskRunSpec struct {
// +optional
Inputs TaskRunInputs `json:"inputs,omitempty"`
// +optional
Outputs TaskRunOutputs `json:"outputs,omitempty"`
// +optional
ServiceAccountName string `json:"serviceAccountName"`
// DeprecatedServiceAccount is a depreciated alias for ServiceAccountName.
// Deprecated: Use serviceAccountName instead.
// +optional
DeprecatedServiceAccount string `json:"serviceAccount,omitempty"`
// no more than one of the TaskRef and TaskSpec may be specified.
// +optional
TaskRef *TaskRef `json:"taskRef,omitempty"`
// +optional
TaskSpec *TaskSpec `json:"taskSpec,omitempty"`
// Used for cancelling a taskrun (and maybe more later on)
// +optional
Status TaskRunSpecStatus `json:"status,omitempty"`
// Time after which the build times out. Defaults to 10 minutes.
// Specified build timeout should be less than 24h.
// Refer Go's ParseDuration documentation for expected format: https://golang.org/pkg/time/#ParseDuration
// +optional
Timeout *metav1.Duration `json:"timeout,omitempty"`
// PodTemplate holds pod specific configuration
PodTemplate PodTemplate `json:"podTemplate,omitempty"`
}
// TaskRunSpecStatus defines the taskrun spec status the user can provide
type TaskRunSpecStatus string
const (
// TaskRunSpecStatusCancelled indicates that the user wants to cancel the task,
// if not already cancelled or terminated
TaskRunSpecStatusCancelled = "TaskRunCancelled"
)
// TaskRunInputs holds the input values that this task was invoked with.
type TaskRunInputs struct {
// +optional
Resources []TaskResourceBinding `json:"resources,omitempty"`
// +optional
Params []Param `json:"params,omitempty"`
}
// TaskResourceBinding points to the PipelineResource that
// will be used for the Task input or output called Name.
type TaskResourceBinding struct {
PipelineResourceBinding
// Paths will probably be removed in #1284, and then PipelineResourceBinding can be used instead.
// The optional Path field corresponds to a path on disk at which the Resource can be found
// (used when providing the resource via mounted volume, overriding the default logic to fetch the Resource).
// +optional
Paths []string `json:"paths,omitempty"`
}
// TaskRunOutputs holds the output values that this task was invoked with.
type TaskRunOutputs struct {
// +optional
Resources []TaskResourceBinding `json:"resources,omitempty"`
}
var taskRunCondSet = apis.NewBatchConditionSet()
// TaskRunStatus defines the observed state of TaskRun
type TaskRunStatus struct {
duckv1beta1.Status `json:",inline"`
// PodName is the name of the pod responsible for executing this task's steps.
PodName string `json:"podName"`
// StartTime is the time the build is actually started.
// +optional
StartTime *metav1.Time `json:"startTime,omitempty"`
// CompletionTime is the time the build completed.
// +optional
CompletionTime *metav1.Time `json:"completionTime,omitempty"`
// Steps describes the state of each build step container.
// +optional
Steps []StepState `json:"steps,omitempty"`
// CloudEvents describe the state of each cloud event requested via a
// CloudEventResource.
// +optional
CloudEvents []CloudEventDelivery `json:"cloudEvents,omitempty"`
// RetriesStatus contains the history of TaskRunStatus in case of a retry in order to keep record of failures.
// All TaskRunStatus stored in RetriesStatus will have no date within the RetriesStatus as is redundant.
// +optional
RetriesStatus []TaskRunStatus `json:"retriesStatus,omitempty"`
// Results from Resources built during the taskRun. currently includes
// the digest of build container images
// optional
ResourcesResult []PipelineResourceResult `json:"resourcesResult,omitempty"`
}
// GetCondition returns the Condition matching the given type.
func (tr *TaskRunStatus) GetCondition(t apis.ConditionType) *apis.Condition {
return taskRunCondSet.Manage(tr).GetCondition(t)
}
// InitializeConditions will set all conditions in taskRunCondSet to unknown for the TaskRun
// and set the started time to the current time
func (tr *TaskRunStatus) InitializeConditions() {
if tr.StartTime.IsZero() {
tr.StartTime = &metav1.Time{Time: time.Now()}
}
taskRunCondSet.Manage(tr).InitializeConditions()
}
// SetCondition sets the condition, unsetting previous conditions with the same
// type as necessary.
func (tr *TaskRunStatus) SetCondition(newCond *apis.Condition) {
if newCond != nil {
taskRunCondSet.Manage(tr).SetCondition(*newCond)
}
}
// StepState reports the results of running a step in the Task.
type StepState struct {
corev1.ContainerState
Name string `json:"name,omitempty"`
ContainerName string `json:"container,omitempty"`
ImageID string `json:"imageID,omitempty"`
}
// CloudEventDelivery is the target of a cloud event along with the state of
// delivery.
type CloudEventDelivery struct {
// Target points to an addressable
Target string `json:"target,omitempty"`
Status CloudEventDeliveryState `json:"status,omitempty"`
}
// CloudEventCondition is a string that represents the condition of the event.
type CloudEventCondition string
const (
// CloudEventConditionUnknown means that the condition for the event to be
// triggered was not met yet, or we don't know the state yet.
CloudEventConditionUnknown CloudEventCondition = "Unknown"
// CloudEventConditionSent means that the event was sent successfully
CloudEventConditionSent CloudEventCondition = "Sent"
// CloudEventConditionFailed means that there was one or more attempts to
// send the event, and none was successful so far.
CloudEventConditionFailed CloudEventCondition = "Failed"
)
// CloudEventDeliveryState reports the state of a cloud event to be sent.
type CloudEventDeliveryState struct {
// Current status
Condition CloudEventCondition `json:"condition,omitempty"`
// SentAt is the time at which the last attempt to send the event was made
// +optional
SentAt *metav1.Time `json:"sentAt,omitempty"`
// Error is the text of error (if any)
Error string `json:"message"`
// RetryCount is the number of attempts of sending the cloud event
RetryCount int32 `json:"retryCount"`
}
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// TaskRun represents a single execution of a Task. TaskRuns are how the steps
// specified in a Task are executed; they specify the parameters and resources
// used to run the steps in a Task.
//
// +k8s:openapi-gen=true
type TaskRun struct {
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ObjectMeta `json:"metadata,omitempty"`
// +optional
Spec TaskRunSpec `json:"spec,omitempty"`
// +optional
Status TaskRunStatus `json:"status,omitempty"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// TaskRunList contains a list of TaskRun
type TaskRunList struct {
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ListMeta `json:"metadata,omitempty"`
Items []TaskRun `json:"items"`
}
// GetBuildPodRef for task
func (tr *TaskRun) GetBuildPodRef() corev1.ObjectReference {
return corev1.ObjectReference{
APIVersion: "v1",
Kind: "Pod",
Namespace: tr.Namespace,
Name: tr.Name,
}
}
// GetPipelineRunPVCName for taskrun gets pipelinerun
func (tr *TaskRun) GetPipelineRunPVCName() string {
if tr == nil {
return ""
}
for _, ref := range tr.GetOwnerReferences() {
if ref.Kind == pipelineRunControllerName {
return fmt.Sprintf("%s-pvc", ref.Name)
}
}
return ""
}
// HasPipelineRunOwnerReference returns true of TaskRun has
// owner reference of type PipelineRun
func (tr *TaskRun) HasPipelineRunOwnerReference() bool {
for _, ref := range tr.GetOwnerReferences() {
if ref.Kind == pipelineRunControllerName {
return true
}
}
return false
}
// IsDone returns true if the TaskRun's status indicates that it is done.
func (tr *TaskRun) IsDone() bool {
return !tr.Status.GetCondition(apis.ConditionSucceeded).IsUnknown()
}
// HasStarted function check whether taskrun has valid start time set in its status
func (tr *TaskRun) HasStarted() bool {
return tr.Status.StartTime != nil && !tr.Status.StartTime.IsZero()
}
// IsSuccessful returns true if the TaskRun's status indicates that it is done.
func (tr *TaskRun) IsSuccessful() bool {
return tr.Status.GetCondition(apis.ConditionSucceeded).IsTrue()
}
// IsCancelled returns true if the TaskRun's spec status is set to Cancelled state
func (tr *TaskRun) IsCancelled() bool {
return tr.Spec.Status == TaskRunSpecStatusCancelled
}
// GetRunKey return the taskrun key for timeout handler map
func (tr *TaskRun) GetRunKey() string {
// The address of the pointer is a threadsafe unique identifier for the taskrun
return fmt.Sprintf("%s/%p", "TaskRun", tr)
}
func (tr *TaskRun) GetServiceAccountName() string {
name := tr.Spec.ServiceAccountName
if name == "" {
name = tr.Spec.DeprecatedServiceAccount
}
return name
}
// IsPartOfPipeline return true if TaskRun is a part of a Pipeline.
// It also return the name of Pipeline and PipelineRun
func (tr *TaskRun) IsPartOfPipeline() (bool, string, string) {
if tr == nil || len(tr.Labels) == 0 {
return false, "", ""
}
if pl, ok := tr.Labels[pipeline.GroupName+pipeline.PipelineLabelKey]; ok {
return true, pl, tr.Labels[pipeline.GroupName+pipeline.PipelineRunLabelKey]
}
return false, "", ""
}