Skip to content

Commit 6ee09cb

Browse files
committed
Split up and refactor isCustomTask
In this change, we clean up the `isCustomTask` function: - Remove unused `context.Context`. - Remove the check that either `TaskRef` or `TaskSpec` is specified because we check that way before - see [code]. - Define the check that `TaskRef` is a `CustomTask` as a member function of `TaskRef`; and add tests for the member function. - Define the check that `TaskSpec` is a `CustomTask` as a member function of `TaskSpec`; and add tests for the member function. - Update the docstring for `Kind` and `APIVersion` in `TaskRef`. There are no user-facing changes in this commit. [code]: https://github.com/tektoncd/pipeline/blob/b7d815a9d8528547994f65da097050f472bbb8b2/pkg/apis/pipeline/v1beta1/pipeline_types.go#L216-L227
1 parent 54c0f84 commit 6ee09cb

15 files changed

+257
-39
lines changed

docs/pipeline-api.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4547,7 +4547,9 @@ TaskKind
45474547
</em>
45484548
</td>
45494549
<td>
4550-
<p>TaskKind indicates the kind of the task, namespaced or cluster scoped.</p>
4550+
<p>TaskKind indicates the Kind of the Task:
4551+
1. Namespaced Task when Kind is set to &ldquo;Task&rdquo;. If Kind is &ldquo;&rdquo;, it defaults to &ldquo;Task&rdquo;.
4552+
2. Custom Task when Kind is non-empty and APIVersion is non-empty</p>
45514553
</td>
45524554
</tr>
45534555
<tr>
@@ -4559,7 +4561,8 @@ string
45594561
</td>
45604562
<td>
45614563
<em>(Optional)</em>
4562-
<p>API version of the referent</p>
4564+
<p>API version of the referent
4565+
Note: A Task with non-empty APIVersion and Kind is considered a Custom Task</p>
45634566
</td>
45644567
</tr>
45654568
<tr>
@@ -12102,7 +12105,10 @@ TaskKind
1210212105
</em>
1210312106
</td>
1210412107
<td>
12105-
<p>TaskKind indicates the kind of the task, namespaced or cluster scoped.</p>
12108+
<p>TaskKind indicates the Kind of the Task:
12109+
1. Namespaced Task when Kind is set to &ldquo;Task&rdquo;. If Kind is &ldquo;&rdquo;, it defaults to &ldquo;Task&rdquo;.
12110+
2. Cluster-Scoped Task when Kind is set to &ldquo;ClusterTask&rdquo;
12111+
3. Custom Task when Kind is non-empty and APIVersion is non-empty</p>
1210612112
</td>
1210712113
</tr>
1210812114
<tr>
@@ -12114,7 +12120,8 @@ string
1211412120
</td>
1211512121
<td>
1211612122
<em>(Optional)</em>
12117-
<p>API version of the referent</p>
12123+
<p>API version of the referent
12124+
Note: A Task with non-empty APIVersion and Kind is considered a Custom Task</p>
1211812125
</td>
1211912126
</tr>
1212012127
<tr>

pkg/apis/pipeline/v1/openapi_generated.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/pipeline/v1/pipeline_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@ type PipelineTask struct {
217217
Timeout *metav1.Duration `json:"timeout,omitempty"`
218218
}
219219

220+
// IsCustomTask checks whether an embedded TaskSpec is a Custom Task
221+
func (et *EmbeddedTask) IsCustomTask() bool {
222+
// Note that if `apiVersion` is set to `"tekton.dev/v1beta1"` and `kind` is set to `"Task"`,
223+
// the reference will be considered a Custom Task - https://github.com/tektoncd/pipeline/issues/6457
224+
return et != nil && et.APIVersion != "" && et.Kind != ""
225+
}
226+
220227
// validateRefOrSpec validates at least one of taskRef or taskSpec is specified
221228
func (pt PipelineTask) validateRefOrSpec() (errs *apis.FieldError) {
222229
// can't have both taskRef and taskSpec at the same time

pkg/apis/pipeline/v1/pipeline_types_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,3 +965,48 @@ func TestPipelineTask_IsMatrixed(t *testing.T) {
965965
})
966966
}
967967
}
968+
969+
func TestEmbeddedTask_IsCustomTask(t *testing.T) {
970+
tests := []struct {
971+
name string
972+
et *EmbeddedTask
973+
want bool
974+
}{{
975+
name: "not a custom task - APIVersion and Kind are not set",
976+
et: &EmbeddedTask{},
977+
want: false,
978+
}, {
979+
name: "not a custom task - APIVersion is not set",
980+
et: &EmbeddedTask{
981+
TypeMeta: runtime.TypeMeta{
982+
Kind: "Example",
983+
},
984+
},
985+
want: false,
986+
}, {
987+
name: "not a custom task - Kind is not set",
988+
et: &EmbeddedTask{
989+
TypeMeta: runtime.TypeMeta{
990+
APIVersion: "example/v0",
991+
},
992+
},
993+
want: false,
994+
}, {
995+
name: "custom task - APIVersion and Kind are set",
996+
et: &EmbeddedTask{
997+
TypeMeta: runtime.TypeMeta{
998+
Kind: "Example",
999+
APIVersion: "example/v0",
1000+
},
1001+
},
1002+
want: true,
1003+
},
1004+
}
1005+
for _, tt := range tests {
1006+
t.Run(tt.name, func(t *testing.T) {
1007+
if got := tt.et.IsCustomTask(); got != tt.want {
1008+
t.Errorf("IsCustomTask() = %v, want %v", got, tt.want)
1009+
}
1010+
})
1011+
}
1012+
}

pkg/apis/pipeline/v1/swagger.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,11 +1670,11 @@
16701670
"type": "object",
16711671
"properties": {
16721672
"apiVersion": {
1673-
"description": "API version of the referent",
1673+
"description": "API version of the referent Note: A Task with non-empty APIVersion and Kind is considered a Custom Task",
16741674
"type": "string"
16751675
},
16761676
"kind": {
1677-
"description": "TaskKind indicates the kind of the task, namespaced or cluster scoped.",
1677+
"description": "TaskKind indicates the Kind of the Task: 1. Namespaced Task when Kind is set to \"Task\". If Kind is \"\", it defaults to \"Task\". 2. Custom Task when Kind is non-empty and APIVersion is non-empty",
16781678
"type": "string"
16791679
},
16801680
"name": {

pkg/apis/pipeline/v1/taskref_types.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ package v1
2020
type TaskRef struct {
2121
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
2222
Name string `json:"name,omitempty"`
23-
// TaskKind indicates the kind of the task, namespaced or cluster scoped.
23+
// TaskKind indicates the Kind of the Task:
24+
// 1. Namespaced Task when Kind is set to "Task". If Kind is "", it defaults to "Task".
25+
// 2. Custom Task when Kind is non-empty and APIVersion is non-empty
2426
Kind TaskKind `json:"kind,omitempty"`
2527
// API version of the referent
28+
// Note: A Task with non-empty APIVersion and Kind is considered a Custom Task
2629
// +optional
2730
APIVersion string `json:"apiVersion,omitempty"`
2831

@@ -40,3 +43,10 @@ const (
4043
// NamespacedTaskKind indicates that the task type has a namespaced scope.
4144
NamespacedTaskKind TaskKind = "Task"
4245
)
46+
47+
// IsCustomTask checks whether the reference is to a Custom Task
48+
func (tr *TaskRef) IsCustomTask() bool {
49+
// Note that if `apiVersion` is set to `"tekton.dev/v1beta1"` and `kind` is set to `"Task"`,
50+
// the reference will be considered a Custom Task - https://github.com/tektoncd/pipeline/issues/6457
51+
return tr != nil && tr.APIVersion != "" && tr.Kind != ""
52+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package v1
2+
3+
import "testing"
4+
5+
func TestTaskRef_IsCustomTask(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
tr *TaskRef
9+
want bool
10+
}{{
11+
name: "not a custom task - apiVersion and Kind are not set",
12+
tr: &TaskRef{
13+
Name: "foo",
14+
},
15+
want: false,
16+
}, {
17+
// related issue: https://github.com/tektoncd/pipeline/issues/6459
18+
name: "not a custom task - apiVersion is not set",
19+
tr: &TaskRef{
20+
Name: "foo",
21+
Kind: "Example",
22+
},
23+
want: false,
24+
}, {
25+
name: "not a custom task - kind is not set",
26+
tr: &TaskRef{
27+
Name: "foo",
28+
APIVersion: "example/v0",
29+
},
30+
want: false,
31+
}, {
32+
name: "custom task with name",
33+
tr: &TaskRef{
34+
Name: "foo",
35+
Kind: "Example",
36+
APIVersion: "example/v0",
37+
},
38+
want: true,
39+
}, {
40+
name: "custom task without name",
41+
tr: &TaskRef{
42+
Kind: "Example",
43+
APIVersion: "example/v0",
44+
},
45+
want: true,
46+
},
47+
}
48+
for _, tt := range tests {
49+
t.Run(tt.name, func(t *testing.T) {
50+
if got := tt.tr.IsCustomTask(); got != tt.want {
51+
t.Errorf("IsCustomTask() = %v, want %v", got, tt.want)
52+
}
53+
})
54+
}
55+
}

pkg/apis/pipeline/v1beta1/openapi_generated.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/pipeline/v1beta1/pipeline_types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ type PipelineTask struct {
223223
Timeout *metav1.Duration `json:"timeout,omitempty"`
224224
}
225225

226+
// IsCustomTask checks whether an embedded TaskSpec is a Custom Task
227+
func (et *EmbeddedTask) IsCustomTask() bool {
228+
// Note that if `apiVersion` is set to `"tekton.dev/v1beta1"` and `kind` is set to `"Task"`,
229+
// the reference will be considered a Custom Task - https://github.com/tektoncd/pipeline/issues/6457
230+
return et != nil && et.APIVersion != "" && et.Kind != ""
231+
}
232+
226233
// validateRefOrSpec validates at least one of taskRef or taskSpec is specified
227234
func (pt PipelineTask) validateRefOrSpec() (errs *apis.FieldError) {
228235
// can't have both taskRef and taskSpec at the same time

pkg/apis/pipeline/v1beta1/pipeline_types_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,3 +939,48 @@ func TestPipelineTask_IsMatrixed(t *testing.T) {
939939
})
940940
}
941941
}
942+
943+
func TestEmbeddedTask_IsCustomTask(t *testing.T) {
944+
tests := []struct {
945+
name string
946+
et *EmbeddedTask
947+
want bool
948+
}{{
949+
name: "not a custom task - APIVersion and Kind are not set",
950+
et: &EmbeddedTask{},
951+
want: false,
952+
}, {
953+
name: "not a custom task - APIVersion is not set",
954+
et: &EmbeddedTask{
955+
TypeMeta: runtime.TypeMeta{
956+
Kind: "Example",
957+
},
958+
},
959+
want: false,
960+
}, {
961+
name: "not a custom task - Kind is not set",
962+
et: &EmbeddedTask{
963+
TypeMeta: runtime.TypeMeta{
964+
APIVersion: "example/v0",
965+
},
966+
},
967+
want: false,
968+
}, {
969+
name: "custom task - APIVersion and Kind are set",
970+
et: &EmbeddedTask{
971+
TypeMeta: runtime.TypeMeta{
972+
Kind: "Example",
973+
APIVersion: "example/v0",
974+
},
975+
},
976+
want: true,
977+
},
978+
}
979+
for _, tt := range tests {
980+
t.Run(tt.name, func(t *testing.T) {
981+
if got := tt.et.IsCustomTask(); got != tt.want {
982+
t.Errorf("IsCustomTask() = %v, want %v", got, tt.want)
983+
}
984+
})
985+
}
986+
}

0 commit comments

Comments
 (0)