Skip to content

Commit 4994de5

Browse files
committed
Add ClusterTask new CRD
- issue 251: #251 - new type for ClusterTask - Add kind to TaskRef to differentiate between Task kinds - TaskInterface to be used by controllers, implemeted by Task and ClusterTask - update taskRun and pipelineRun to use TaskInterface - update docs Signed-off-by: Nader Ziada <[email protected]>
1 parent 054702b commit 4994de5

36 files changed

+1009
-97
lines changed

cmd/controller/main.go

+4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func main() {
112112
buildInformerFactory := buildinformers.NewSharedInformerFactory(buildClient, opt.ResyncPeriod)
113113

114114
taskInformer := pipelineInformerFactory.Pipeline().V1alpha1().Tasks()
115+
clusterTaskInformer := pipelineInformerFactory.Pipeline().V1alpha1().ClusterTasks()
115116
taskRunInformer := pipelineInformerFactory.Pipeline().V1alpha1().TaskRuns()
116117
resourceInformer := pipelineInformerFactory.Pipeline().V1alpha1().PipelineResources()
117118
buildInformer := buildInformerFactory.Build().V1alpha1().Builds()
@@ -124,13 +125,15 @@ func main() {
124125
taskrun.NewController(opt,
125126
taskRunInformer,
126127
taskInformer,
128+
clusterTaskInformer,
127129
buildInformer,
128130
resourceInformer,
129131
),
130132
pipelinerun.NewController(opt,
131133
pipelineRunInformer,
132134
pipelineInformer,
133135
taskInformer,
136+
clusterTaskInformer,
134137
taskRunInformer,
135138
resourceInformer,
136139
),
@@ -150,6 +153,7 @@ func main() {
150153
logger.Info("Waiting for informer caches to sync")
151154
for i, synced := range []cache.InformerSynced{
152155
taskInformer.Informer().HasSynced,
156+
clusterTaskInformer.Informer().HasSynced,
153157
taskRunInformer.Informer().HasSynced,
154158
buildInformer.Informer().HasSynced,
155159
resourceInformer.Informer().HasSynced,

config/200-clusterrole.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ rules:
1616
resources: ["customresourcedefinitions"]
1717
verbs: ["get", "list", "create", "update", "delete", "patch", "watch"]
1818
- apiGroups: ["pipeline.knative.dev"]
19-
resources: ["tasks", "taskruns", "pipelines", "pipelineruns", "pipelineresources"]
19+
resources: ["tasks", "clustertasks", "taskruns", "pipelines", "pipelineruns", "pipelineresources"]
2020
verbs: ["get", "list", "create", "update", "delete", "patch", "watch"]
2121
- apiGroups: ["build.knative.dev"]
2222
resources: ["builds", "buildtemplates", "clusterbuildtemplates"]

config/300-clustertask.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
creationTimestamp: null
5+
labels:
6+
controller-tools.k8s.io: "1.0"
7+
name: clustertasks.pipeline.knative.dev
8+
spec:
9+
group: pipeline.knative.dev
10+
names:
11+
kind: ClusterTask
12+
plural: clustertasks
13+
categories:
14+
- all
15+
- knative
16+
- build-pipeline
17+
scope: Cluster
18+
version: v1alpha1

docs/Concepts.md

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ pull in your environment, so we provide a way for you to configure that by edit
126126
the `image`'s value in a configmap named
127127
[`config-entrypoint`](./../config/config-entrypoint.yaml).
128128

129+
### Cluster Task
130+
131+
Similar to `Task` but with a cluster-wide scope. Cluster Tasks are available in
132+
all namespaces, typically used to conveniently provide commonly used tasks to users.
133+
129134
### Pipeline
130135

131136
`Pipelines` describes a graph of [Tasks](#Task) to execute.

docs/using.md

+23
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ To access a `Param`, replace `resources` with `params` as below:
139139
${inputs.params.NAME}
140140
```
141141

142+
## Cluster Task
143+
144+
Similar to Task, but with a cluster scope.
145+
146+
In case of using a ClusterTask, the `TaskRef` kind should be added. The default kind is Task
147+
which represents a namespaced Task
148+
149+
```yaml
150+
apiVersion: pipeline.knative.dev/v1alpha1
151+
kind: Pipeline
152+
metadata:
153+
name: demo-pipeline
154+
namespace: default
155+
spec:
156+
tasks:
157+
- name: build-skaffold-web
158+
taskRef:
159+
name: build-push
160+
kind: ClusterTask
161+
params:
162+
....
163+
```
164+
142165
## Running a Pipeline
143166

144167
In order to run a Pipeline, you will need to provide:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2018 The Knative Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"github.com/knative/pkg/apis"
21+
"github.com/knative/pkg/webhook"
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
)
24+
25+
func (t *ClusterTask) TaskSpec() TaskSpec {
26+
return t.Spec
27+
}
28+
29+
func (t *ClusterTask) TaskMetadata() metav1.ObjectMeta {
30+
return t.ObjectMeta
31+
}
32+
33+
func (t *ClusterTask) Copy() TaskInterface {
34+
return t.DeepCopy()
35+
}
36+
37+
func (t *ClusterTask) SetDefaults() {
38+
t.Spec.SetDefaults()
39+
}
40+
41+
// Check that Task may be validated and defaulted.
42+
var _ apis.Validatable = (*ClusterTask)(nil)
43+
var _ apis.Defaultable = (*ClusterTask)(nil)
44+
45+
// Assert that Task implements the GenericCRD interface.
46+
var _ webhook.GenericCRD = (*ClusterTask)(nil)
47+
48+
// +genclient
49+
// +genclient:noStatus
50+
// +genclient:nonNamespaced
51+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
52+
53+
// ClusterTask is a Task with a cluster scope
54+
type ClusterTask struct {
55+
metav1.TypeMeta `json:",inline"`
56+
// +optional
57+
metav1.ObjectMeta `json:"metadata,omitempty"`
58+
59+
// Spec holds the desired state of the Task from the client
60+
// +optional
61+
Spec TaskSpec `json:"spec,omitempty"`
62+
}
63+
64+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
65+
66+
// ClusterTaskList contains a list of ClusterTask
67+
type ClusterTaskList struct {
68+
metav1.TypeMeta `json:",inline"`
69+
// +optional
70+
metav1.ListMeta `json:"metadata,omitempty"`
71+
Items []Task `json:"items"`
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2018 The Knative Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"github.com/knative/pkg/apis"
21+
)
22+
23+
func (t *ClusterTask) Validate() *apis.FieldError {
24+
if err := validateObjectMetadata(t.GetObjectMeta()); err != nil {
25+
return err.ViaField("metadata")
26+
}
27+
return t.Spec.Validate()
28+
}

pkg/apis/pipeline/v1alpha1/pipeline_defaults.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ func (p *Pipeline) SetDefaults() {
2020
p.Spec.SetDefaults()
2121
}
2222

23-
func (ps *PipelineSpec) SetDefaults() {}
23+
func (ps *PipelineSpec) SetDefaults() {
24+
for _, pt := range ps.Tasks {
25+
if pt.TaskRef.Kind == "" {
26+
pt.TaskRef.Kind = NamespacedTaskKind
27+
}
28+
}
29+
}

pkg/apis/pipeline/v1alpha1/pipeline_types.go

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ type PipelineStatus struct {
3737
var _ apis.Validatable = (*Pipeline)(nil)
3838
var _ apis.Defaultable = (*Pipeline)(nil)
3939

40+
// TaskKind defines the type of Task used by the pipeline.
41+
type TaskKind string
42+
43+
const (
44+
// NamespacedTaskKind indicates that the task type has a namepace scope.
45+
NamespacedTaskKind TaskKind = "Task"
46+
// ClusterTaskKind indicates that task type has a cluster scope.
47+
ClusterTaskKind TaskKind = "ClusterTask"
48+
)
49+
4050
// +genclient
4151
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
4252

@@ -90,6 +100,8 @@ type ResourceDependency struct {
90100
type TaskRef struct {
91101
// Name of the referent; More info: http://kubernetes.io/docs/user-guide/identifiers#names
92102
Name string `json:"name"`
103+
// TaskKind inficates the kind of the task, namespaced or cluster scoped.
104+
Kind TaskKind `json:"kind,omitempty"`
93105
// API version of the referent
94106
// +optional
95107
APIVersion string `json:"apiVersion,omitempty"`

pkg/apis/pipeline/v1alpha1/register.go

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
4848
scheme.AddKnownTypes(SchemeGroupVersion,
4949
&Task{},
5050
&TaskList{},
51+
&ClusterTask{},
52+
&ClusterTaskList{},
5153
&TaskRun{},
5254
&TaskRunList{},
5355
&Pipeline{},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
Copyright 2018 The Knative Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
20+
21+
// TaskInterface is implemented by Task and ClusterTask
22+
type TaskInterface interface {
23+
TaskMetadata() metav1.ObjectMeta
24+
TaskSpec() TaskSpec
25+
Copy() TaskInterface
26+
}

pkg/apis/pipeline/v1alpha1/task_types.go

+13-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
)
2727

28+
func (t *Task) TaskSpec() TaskSpec {
29+
return t.Spec
30+
}
31+
32+
func (t *Task) TaskMetadata() metav1.ObjectMeta {
33+
return t.ObjectMeta
34+
}
35+
36+
func (t *Task) Copy() TaskInterface {
37+
return t.DeepCopy()
38+
}
39+
2840
// TaskSpec defines the desired state of Task
2941
type TaskSpec struct {
3042
// +optional
@@ -59,12 +71,6 @@ type TaskSpec struct {
5971
Affinity *corev1.Affinity `json:"affinity,omitempty"`
6072
}
6173

62-
// TaskStatus does not contain anything because Tasks on their own
63-
// do not have a status, they just hold data which is later used by a
64-
// TaskRun.
65-
type TaskStatus struct {
66-
}
67-
6874
// Check that Task may be validated and defaulted.
6975
var _ apis.Validatable = (*Task)(nil)
7076
var _ apis.Defaultable = (*Task)(nil)
@@ -73,6 +79,7 @@ var _ apis.Defaultable = (*Task)(nil)
7379
var _ webhook.GenericCRD = (*Task)(nil)
7480

7581
// +genclient
82+
// +genclient:noStatus
7683
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
7784

7885
// Task is the Schema for the tasks API
@@ -85,9 +92,6 @@ type Task struct {
8592
// Spec holds the desired state of the Task from the client
8693
// +optional
8794
Spec TaskSpec `json:"spec,omitempty"`
88-
// Status communicates the observed state of the Task from the controller
89-
// +optional
90-
Status TaskStatus `json:"status,omitempty"`
9195
}
9296

9397
// Inputs are the requirements that a task needs to run a Build.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright 2018 The Knative Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
func (tr *TaskRun) SetDefaults() {
20+
tr.Spec.SetDefaults()
21+
}
22+
23+
func (trs *TaskRunSpec) SetDefaults() {
24+
if trs.TaskRef.Kind == "" {
25+
trs.TaskRef.Kind = NamespacedTaskKind
26+
}
27+
}

pkg/apis/pipeline/v1alpha1/taskrun_types.go

-2
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ type TaskRunList struct {
174174
Items []TaskRun `json:"items"`
175175
}
176176

177-
func (tr *TaskRun) SetDefaults() {}
178-
179177
// GetBuildRef for task
180178
func (tr *TaskRun) GetBuildRef() corev1.ObjectReference {
181179
return corev1.ObjectReference{

0 commit comments

Comments
 (0)