Skip to content

Commit bdd28a1

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 538fee3 commit bdd28a1

15 files changed

+247
-39
lines changed

docs/pipeline-api.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4487,7 +4487,10 @@ TaskKind
44874487
</em>
44884488
</td>
44894489
<td>
4490-
<p>TaskKind indicates the kind of the task, namespaced or cluster scoped.</p>
4490+
<p>TaskKind indicates the Kind of the Task:
4491+
1. Namespaced Task when Kind is set to &ldquo;Task&rdquo;
4492+
2. Cluster-Scoped Task when Kind is set to &ldquo;ClusterTask&rdquo;
4493+
3. Custom Task when Kind is non-empty and APIVersion is non-empty</p>
44914494
</td>
44924495
</tr>
44934496
<tr>
@@ -4499,7 +4502,8 @@ string
44994502
</td>
45004503
<td>
45014504
<em>(Optional)</em>
4502-
<p>API version of the referent</p>
4505+
<p>API version of the referent
4506+
Note: A Task with non-empty APIVersion and Kind is considered a Custom Task</p>
45034507
</td>
45044508
</tr>
45054509
<tr>
@@ -11953,7 +11957,9 @@ TaskKind
1195311957
</em>
1195411958
</td>
1195511959
<td>
11956-
<p>TaskKind indicates the kind of the task, namespaced or cluster scoped.</p>
11960+
<p>TaskKind indicates the Kind of the Task:
11961+
1. Namespaced Task when Kind is set to &ldquo;Task&rdquo;
11962+
2. Custom Task when Kind is non-empty and APIVersion is non-empty</p>
1195711963
</td>
1195811964
</tr>
1195911965
<tr>
@@ -11965,7 +11971,8 @@ string
1196511971
</td>
1196611972
<td>
1196711973
<em>(Optional)</em>
11968-
<p>API version of the referent</p>
11974+
<p>API version of the referent
11975+
Note: A Task with non-empty APIVersion and Kind is considered a Custom Task</p>
1196911976
</td>
1197011977
</tr>
1197111978
<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
@@ -203,6 +203,11 @@ type PipelineTask struct {
203203
Timeout *metav1.Duration `json:"timeout,omitempty"`
204204
}
205205

206+
// IsCustomTask checks whether an embedded TaskSpec is a Custom Task
207+
func (et *EmbeddedTask) IsCustomTask() bool {
208+
return et != nil && et.APIVersion != "" && et.Kind != ""
209+
}
210+
206211
// validateRefOrSpec validates at least one of taskRef or taskSpec is specified
207212
func (pt PipelineTask) validateRefOrSpec() (errs *apis.FieldError) {
208213
// 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
@@ -1654,11 +1654,11 @@
16541654
"type": "object",
16551655
"properties": {
16561656
"apiVersion": {
1657-
"description": "API version of the referent",
1657+
"description": "API version of the referent Note: A Task with non-empty APIVersion and Kind is considered a Custom Task",
16581658
"type": "string"
16591659
},
16601660
"kind": {
1661-
"description": "TaskKind indicates the kind of the task, namespaced or cluster scoped.",
1661+
"description": "TaskKind indicates the Kind of the Task: 1. Namespaced Task when Kind is set to \"Task\" 2. Cluster-Scoped Task when Kind is set to \"ClusterTask\" 3. Custom Task when Kind is non-empty and APIVersion is non-empty",
16621662
"type": "string"
16631663
},
16641664
"name": {

pkg/apis/pipeline/v1/taskref_types.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,13 @@ 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"
25+
// 2. Cluster-Scoped Task when Kind is set to "ClusterTask"
26+
// 3. Custom Task when Kind is non-empty and APIVersion is non-empty
2427
Kind TaskKind `json:"kind,omitempty"`
2528
// API version of the referent
29+
// Note: A Task with non-empty APIVersion and Kind is considered a Custom Task
2630
// +optional
2731
APIVersion string `json:"apiVersion,omitempty"`
2832

@@ -40,3 +44,8 @@ const (
4044
// NamespacedTaskKind indicates that the task type has a namespaced scope.
4145
NamespacedTaskKind TaskKind = "Task"
4246
)
47+
48+
// IsCustomTask checks whether the reference is to a Custom Task
49+
func (tr *TaskRef) IsCustomTask() bool {
50+
return tr != nil && tr.APIVersion != "" && tr.Kind != ""
51+
}
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
@@ -209,6 +209,11 @@ type PipelineTask struct {
209209
Timeout *metav1.Duration `json:"timeout,omitempty"`
210210
}
211211

212+
// IsCustomTask checks whether an embedded TaskSpec is a Custom Task
213+
func (et *EmbeddedTask) IsCustomTask() bool {
214+
return et != nil && et.APIVersion != "" && et.Kind != ""
215+
}
216+
212217
// validateRefOrSpec validates at least one of taskRef or taskSpec is specified
213218
func (pt PipelineTask) validateRefOrSpec() (errs *apis.FieldError) {
214219
// 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)