Skip to content

Commit cc606ca

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 34f35ec commit cc606ca

15 files changed

+253
-39
lines changed

docs/pipeline-api.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,7 +4550,9 @@ TaskKind
45504550
</em>
45514551
</td>
45524552
<td>
4553-
<p>TaskKind indicates the kind of the task, namespaced or cluster scoped.</p>
4553+
<p>TaskKind indicates the Kind of the Task:
4554+
1. Namespaced Task when Kind is set to &ldquo;Task&rdquo;. If Kind is &ldquo;&rdquo;, it defaults to &ldquo;Task&rdquo;.
4555+
2. Custom Task when Kind is non-empty and APIVersion is non-empty</p>
45544556
</td>
45554557
</tr>
45564558
<tr>
@@ -4562,7 +4564,8 @@ string
45624564
</td>
45634565
<td>
45644566
<em>(Optional)</em>
4565-
<p>API version of the referent</p>
4567+
<p>API version of the referent
4568+
Note: A Task with non-empty APIVersion and Kind is considered a Custom Task</p>
45664569
</td>
45674570
</tr>
45684571
<tr>
@@ -12105,7 +12108,10 @@ TaskKind
1210512108
</em>
1210612109
</td>
1210712110
<td>
12108-
<p>TaskKind indicates the kind of the task, namespaced or cluster scoped.</p>
12111+
<p>TaskKind indicates the Kind of the Task:
12112+
1. Namespaced Task when Kind is set to &ldquo;Task&rdquo;. If Kind is &ldquo;&rdquo;, it defaults to &ldquo;Task&rdquo;.
12113+
2. Cluster-Scoped Task when Kind is set to &ldquo;ClusterTask&rdquo;
12114+
3. Custom Task when Kind is non-empty and APIVersion is non-empty</p>
1210912115
</td>
1211012116
</tr>
1211112117
<tr>
@@ -12117,7 +12123,8 @@ string
1211712123
</td>
1211812124
<td>
1211912125
<em>(Optional)</em>
12120-
<p>API version of the referent</p>
12126+
<p>API version of the referent
12127+
Note: A Task with non-empty APIVersion and Kind is considered a Custom Task</p>
1212112128
</td>
1212212129
</tr>
1212312130
<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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,11 @@ 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+
return et != nil && et.APIVersion != "" && et.Kind != ""
223+
}
224+
220225
// validateRefOrSpec validates at least one of taskRef or taskSpec is specified
221226
func (pt PipelineTask) validateRefOrSpec() (errs *apis.FieldError) {
222227
// 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: 12 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,11 @@ 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
51+
// Issue : https://github.com/tektoncd/pipeline/issues/6457
52+
return tr != nil && tr.APIVersion != "" && tr.Kind != ""
53+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
name: "not a custom task - apiVersion is not set",
18+
tr: &TaskRef{
19+
Name: "foo",
20+
Kind: "Example",
21+
},
22+
want: false,
23+
}, {
24+
name: "not a custom task - kind is not set",
25+
tr: &TaskRef{
26+
Name: "foo",
27+
APIVersion: "example/v0",
28+
},
29+
want: false,
30+
}, {
31+
name: "custom task with name",
32+
tr: &TaskRef{
33+
Name: "foo",
34+
Kind: "Example",
35+
APIVersion: "example/v0",
36+
},
37+
want: true,
38+
}, {
39+
name: "custom task without name",
40+
tr: &TaskRef{
41+
Kind: "Example",
42+
APIVersion: "example/v0",
43+
},
44+
want: true,
45+
},
46+
}
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
if got := tt.tr.IsCustomTask(); got != tt.want {
50+
t.Errorf("IsCustomTask() = %v, want %v", got, tt.want)
51+
}
52+
})
53+
}
54+
}

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@ 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+
return et != nil && et.APIVersion != "" && et.Kind != ""
229+
}
230+
226231
// validateRefOrSpec validates at least one of taskRef or taskSpec is specified
227232
func (pt PipelineTask) validateRefOrSpec() (errs *apis.FieldError) {
228233
// 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)