Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split up and refactor isCustomTask #6447

Merged
merged 1 commit into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions docs/pipeline-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4547,7 +4547,9 @@ TaskKind
</em>
</td>
<td>
<p>TaskKind indicates the kind of the task, namespaced or cluster scoped.</p>
<p>TaskKind indicates the Kind of the Task:
1. Namespaced Task when Kind is set to &ldquo;Task&rdquo;. If Kind is &ldquo;&rdquo;, it defaults to &ldquo;Task&rdquo;.
2. Custom Task when Kind is non-empty and APIVersion is non-empty</p>
</td>
</tr>
<tr>
Expand All @@ -4559,7 +4561,8 @@ string
</td>
<td>
<em>(Optional)</em>
<p>API version of the referent</p>
<p>API version of the referent
Note: A Task with non-empty APIVersion and Kind is considered a Custom Task</p>
</td>
</tr>
<tr>
Expand Down Expand Up @@ -12102,7 +12105,10 @@ TaskKind
</em>
</td>
<td>
<p>TaskKind indicates the kind of the task, namespaced or cluster scoped.</p>
<p>TaskKind indicates the Kind of the Task:
1. Namespaced Task when Kind is set to &ldquo;Task&rdquo;. If Kind is &ldquo;&rdquo;, it defaults to &ldquo;Task&rdquo;.
2. Cluster-Scoped Task when Kind is set to &ldquo;ClusterTask&rdquo;
3. Custom Task when Kind is non-empty and APIVersion is non-empty</p>
</td>
</tr>
<tr>
Expand All @@ -12114,7 +12120,8 @@ string
</td>
<td>
<em>(Optional)</em>
<p>API version of the referent</p>
<p>API version of the referent
Note: A Task with non-empty APIVersion and Kind is considered a Custom Task</p>
</td>
</tr>
<tr>
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/apis/pipeline/v1/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ type PipelineTask struct {
Timeout *metav1.Duration `json:"timeout,omitempty"`
}

// IsCustomTask checks whether an embedded TaskSpec is a Custom Task
func (et *EmbeddedTask) IsCustomTask() bool {
// Note that if `apiVersion` is set to `"tekton.dev/v1beta1"` and `kind` is set to `"Task"`,
// the reference will be considered a Custom Task - https://github.com/tektoncd/pipeline/issues/6457
return et != nil && et.APIVersion != "" && et.Kind != ""
}

// validateRefOrSpec validates at least one of taskRef or taskSpec is specified
func (pt PipelineTask) validateRefOrSpec() (errs *apis.FieldError) {
// can't have both taskRef and taskSpec at the same time
Expand Down
45 changes: 45 additions & 0 deletions pkg/apis/pipeline/v1/pipeline_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,3 +965,48 @@ func TestPipelineTask_IsMatrixed(t *testing.T) {
})
}
}

func TestEmbeddedTask_IsCustomTask(t *testing.T) {
tests := []struct {
name string
et *EmbeddedTask
want bool
}{{
name: "not a custom task - APIVersion and Kind are not set",
et: &EmbeddedTask{},
want: false,
}, {
name: "not a custom task - APIVersion is not set",
et: &EmbeddedTask{
TypeMeta: runtime.TypeMeta{
Kind: "Example",
},
},
want: false,
}, {
name: "not a custom task - Kind is not set",
et: &EmbeddedTask{
TypeMeta: runtime.TypeMeta{
APIVersion: "example/v0",
},
},
want: false,
}, {
name: "custom task - APIVersion and Kind are set",
et: &EmbeddedTask{
TypeMeta: runtime.TypeMeta{
Kind: "Example",
APIVersion: "example/v0",
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.et.IsCustomTask(); got != tt.want {
t.Errorf("IsCustomTask() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1670,11 +1670,11 @@
"type": "object",
"properties": {
"apiVersion": {
"description": "API version of the referent",
"description": "API version of the referent Note: A Task with non-empty APIVersion and Kind is considered a Custom Task",
"type": "string"
},
"kind": {
"description": "TaskKind indicates the kind of the task, namespaced or cluster scoped.",
"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",
"type": "string"
},
"name": {
Expand Down
12 changes: 11 additions & 1 deletion pkg/apis/pipeline/v1/taskref_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ package v1
type TaskRef struct {
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
Name string `json:"name,omitempty"`
// TaskKind indicates the kind of the task, namespaced or cluster scoped.
// 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
Kind TaskKind `json:"kind,omitempty"`
// API version of the referent
// Note: A Task with non-empty APIVersion and Kind is considered a Custom Task
// +optional
APIVersion string `json:"apiVersion,omitempty"`

Expand All @@ -40,3 +43,10 @@ const (
// NamespacedTaskKind indicates that the task type has a namespaced scope.
NamespacedTaskKind TaskKind = "Task"
)

// IsCustomTask checks whether the reference is to a Custom Task
func (tr *TaskRef) IsCustomTask() bool {
// Note that if `apiVersion` is set to `"tekton.dev/v1beta1"` and `kind` is set to `"Task"`,
// the reference will be considered a Custom Task - https://github.com/tektoncd/pipeline/issues/6457
return tr != nil && tr.APIVersion != "" && tr.Kind != ""
jerop marked this conversation as resolved.
Show resolved Hide resolved
}
55 changes: 55 additions & 0 deletions pkg/apis/pipeline/v1/taskref_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package v1

import "testing"

func TestTaskRef_IsCustomTask(t *testing.T) {
tests := []struct {
name string
tr *TaskRef
want bool
}{{
name: "not a custom task - apiVersion and Kind are not set",
tr: &TaskRef{
Name: "foo",
},
want: false,
}, {
// related issue: https://github.com/tektoncd/pipeline/issues/6459
name: "not a custom task - apiVersion is not set",
tr: &TaskRef{
Name: "foo",
Kind: "Example",
jerop marked this conversation as resolved.
Show resolved Hide resolved
},
want: false,
}, {
name: "not a custom task - kind is not set",
tr: &TaskRef{
Name: "foo",
APIVersion: "example/v0",
},
want: false,
}, {
name: "custom task with name",
tr: &TaskRef{
Name: "foo",
Kind: "Example",
APIVersion: "example/v0",
},
want: true,
}, {
name: "custom task without name",
tr: &TaskRef{
Kind: "Example",
APIVersion: "example/v0",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.tr.IsCustomTask(); got != tt.want {
t.Errorf("IsCustomTask() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1beta1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ type PipelineTask struct {
Timeout *metav1.Duration `json:"timeout,omitempty"`
}

// IsCustomTask checks whether an embedded TaskSpec is a Custom Task
func (et *EmbeddedTask) IsCustomTask() bool {
// Note that if `apiVersion` is set to `"tekton.dev/v1beta1"` and `kind` is set to `"Task"`,
// the reference will be considered a Custom Task - https://github.com/tektoncd/pipeline/issues/6457
return et != nil && et.APIVersion != "" && et.Kind != ""
jerop marked this conversation as resolved.
Show resolved Hide resolved
}

// validateRefOrSpec validates at least one of taskRef or taskSpec is specified
func (pt PipelineTask) validateRefOrSpec() (errs *apis.FieldError) {
// can't have both taskRef and taskSpec at the same time
Expand Down
45 changes: 45 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipeline_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,3 +939,48 @@ func TestPipelineTask_IsMatrixed(t *testing.T) {
})
}
}

func TestEmbeddedTask_IsCustomTask(t *testing.T) {
tests := []struct {
name string
et *EmbeddedTask
want bool
}{{
name: "not a custom task - APIVersion and Kind are not set",
et: &EmbeddedTask{},
want: false,
}, {
name: "not a custom task - APIVersion is not set",
et: &EmbeddedTask{
TypeMeta: runtime.TypeMeta{
Kind: "Example",
},
},
want: false,
}, {
name: "not a custom task - Kind is not set",
et: &EmbeddedTask{
TypeMeta: runtime.TypeMeta{
APIVersion: "example/v0",
},
},
want: false,
}, {
name: "custom task - APIVersion and Kind are set",
et: &EmbeddedTask{
TypeMeta: runtime.TypeMeta{
Kind: "Example",
APIVersion: "example/v0",
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.et.IsCustomTask(); got != tt.want {
t.Errorf("IsCustomTask() = %v, want %v", got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1beta1/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2184,15 +2184,15 @@
"type": "object",
"properties": {
"apiVersion": {
"description": "API version of the referent",
"description": "API version of the referent Note: A Task with non-empty APIVersion and Kind is considered a Custom Task",
"type": "string"
},
"bundle": {
"description": "Bundle url reference to a Tekton Bundle.\n\nDeprecated: Please use ResolverRef with the bundles resolver instead.",
"type": "string"
},
"kind": {
"description": "TaskKind indicates the kind of the task, namespaced or cluster scoped.",
"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. Cluster-Scoped Task when Kind is set to \"ClusterTask\" 3. Custom Task when Kind is non-empty and APIVersion is non-empty",
"type": "string"
},
"name": {
Expand Down
13 changes: 12 additions & 1 deletion pkg/apis/pipeline/v1beta1/taskref_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ package v1beta1
type TaskRef struct {
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
Name string `json:"name,omitempty"`
// TaskKind indicates the kind of the task, namespaced or cluster scoped.
// TaskKind indicates the Kind of the Task:
// 1. Namespaced Task when Kind is set to "Task". If Kind is "", it defaults 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
Kind TaskKind `json:"kind,omitempty"`
// API version of the referent
// Note: A Task with non-empty APIVersion and Kind is considered a Custom Task
// +optional
APIVersion string `json:"apiVersion,omitempty"`
// Bundle url reference to a Tekton Bundle.
Expand All @@ -49,3 +53,10 @@ const (
// ClusterTaskKind indicates that task type has a cluster scope.
ClusterTaskKind TaskKind = "ClusterTask"
)

// IsCustomTask checks whether the reference is to a Custom Task
func (tr *TaskRef) IsCustomTask() bool {
// Note that if `apiVersion` is set to `"tekton.dev/v1beta1"` and `kind` is set to `"Task"`,
// the reference will be considered a Custom Task - https://github.com/tektoncd/pipeline/issues/6457
return tr != nil && tr.APIVersion != "" && tr.Kind != ""
}
55 changes: 55 additions & 0 deletions pkg/apis/pipeline/v1beta1/taskref_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package v1beta1

import "testing"

func TestTaskRef_IsCustomTask(t *testing.T) {
tests := []struct {
name string
tr *TaskRef
want bool
}{{
name: "not a custom task - apiVersion and Kind are not set",
tr: &TaskRef{
Name: "foo",
},
want: false,
}, {
// related issue: https://github.com/tektoncd/pipeline/issues/6459
name: "not a custom task - apiVersion is not set",
tr: &TaskRef{
Name: "foo",
Kind: "Example",
},
want: false,
}, {
name: "not a custom task - kind is not set",
tr: &TaskRef{
Name: "foo",
APIVersion: "example/v0",
},
want: false,
}, {
name: "custom task with name",
tr: &TaskRef{
Name: "foo",
Kind: "Example",
APIVersion: "example/v0",
},
want: true,
}, {
name: "custom task without name",
tr: &TaskRef{
Kind: "Example",
APIVersion: "example/v0",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.tr.IsCustomTask(); got != tt.want {
t.Errorf("IsCustomTask() = %v, want %v", got, tt.want)
}
})
}
}
Loading