Skip to content

Commit

Permalink
updates to implementation of TaskSpec in taskRun
Browse files Browse the repository at this point in the history
- remove support for using sources in the taskSpec
- use TaskResolver
- refactor tests

Signed-off-by: Nader Ziada <[email protected]>
  • Loading branch information
nader-ziada committed Nov 27, 2018
1 parent 27e8c8e commit 17de7e2
Show file tree
Hide file tree
Showing 13 changed files with 381 additions and 485 deletions.
40 changes: 40 additions & 0 deletions docs/using.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,46 @@ See [the example PipelineRun](../examples/invocations/kritis-pipeline-run.yaml).
that the `Task` needs to run.
2. The `TaskRun` will also serve as a record of the history of the invocations of the
`Task`.
3. Another way of running a Task is embedding the TaskSpec in the taskRun yaml as shown in the following example

```yaml
apiVersion: pipeline.knative.dev/v1alpha1
kind: PipelineResource
metadata:
name: go-example-git
spec:
type: git
params:
- name: url
value: https://github.com/pivotal-nader-ziada/gohelloworld
---
apiVersion: pipeline.knative.dev/v1alpha1
kind: TaskRun
metadata:
name: build-push-task-run-2
spec:
trigger:
triggerRef:
type: manual
inputs:
resources:
- name: workspace
resourceRef:
name: go-example-git
taskSpec:
inputs:
resources:
- name: workspace
type: git
steps:
- name: build-and-push
image: gcr.io/kaniko-project/executor
command:
- /kaniko/executor
args:
- --destination=gcr.io/my-project/gohelloworld
```
If the TaskSpec is provided, TaskRef is not allowed.

See [the example TaskRun](../examples/invocations/run-kritis-test.yaml).

Expand Down
5 changes: 0 additions & 5 deletions pkg/apis/pipeline/v1alpha1/task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ type TaskSpec struct {
// +optional
Generation int64 `json:"generation,omitempty"`

// Following fields are from build spec for a Build resource.
// Sources specifies the inputs to the build.
Sources []buildv1alpha1.SourceSpec `json:"sources,omitempty"`

// Steps are the steps of the build; each step is run sequentially with the
// source mounted into /workspace.
Steps []corev1.Container `json:"steps,omitempty"`
Expand Down Expand Up @@ -151,7 +147,6 @@ type TaskList struct {

func (ts *TaskSpec) GetBuildSpec() *buildv1alpha1.BuildSpec {
return &buildv1alpha1.BuildSpec{
Sources: ts.Sources,
Steps: ts.Steps,
Volumes: ts.Volumes,
ServiceAccountName: ts.ServiceAccountName,
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/pipeline/v1alpha1/taskrun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func (ts *TaskRunSpec) Validate() *apis.FieldError {

// can't have both taskRef and taskSpec at the same time
if (ts.TaskRef != nil && ts.TaskRef.Name != "") && ts.TaskSpec != nil {
return apis.ErrDisallowedFields("spec.taskSpec")
fields := []string{"spec.taskref", "spec.taskspec"}
return apis.ErrDisallowedFields(fields...)
}

// Check that one of TaskRef and TaskSpec is present
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1alpha1/taskrun_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestTaskRunSpec_Invalidate(t *testing.T) {
},
},
},
wantErr: apis.ErrDisallowedFields("spec.taskSpec"),
wantErr: apis.ErrDisallowedFields("spec.taskspec", "spec.taskref"),
},
}

Expand Down
8 changes: 0 additions & 8 deletions pkg/apis/pipeline/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func TestAddResourceToBuild(t *testing.T) {
}} {
t.Run(c.desc, func(t *testing.T) {
setUp()
got, err := AddInputResource(c.build, c.task.Name, c.task.Spec, c.taskRun, pipelineResourceLister, logger)
got, err := AddInputResource(c.build, c.task.Name, &c.task.Spec, c.taskRun, pipelineResourceLister, logger)
if (err != nil) != c.wantErr {
t.Errorf("Test: %q; NewControllerConfigFromConfigMap() error = %v, WantErr %v", c.desc, err, c.wantErr)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func getBoundResource(resourceName string, boundResources []v1alpha1.TaskRunReso
func AddInputResource(
build *buildv1alpha1.Build,
taskName string,
taskSpec v1alpha1.TaskSpec,
taskSpec *v1alpha1.TaskSpec,
taskRun *v1alpha1.TaskRun,
pipelineResourceLister listers.PipelineResourceLister,
logger *zap.SugaredLogger,
Expand Down
34 changes: 28 additions & 6 deletions pkg/reconciler/v1alpha1/taskrun/resources/taskrunresolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
// ResolvedTaskRun contains all the data that is needed to execute
// the TaskRun: the TaskRun, it's Task and the PipelineResources it needs.
type ResolvedTaskRun struct {
Task *v1alpha1.Task
TaskName string
TaskSpec *v1alpha1.TaskSpec
// Inputs is a map from the name of the input required by the Task
// to the actual Resource to use for it
Inputs map[string]*v1alpha1.PipelineResource
Expand All @@ -43,25 +44,26 @@ type GetTask func(string) (*v1alpha1.Task, error)
// ResolveTaskRun looks up CRDs referenced by the TaskRun and returns an instance
// of TaskRunResource with all of the relevant data populated. If referenced CRDs can't
// be found, an error is returned.
func ResolveTaskRun(tr *v1alpha1.TaskRunSpec, getTask GetTask, gr GetResource) (*ResolvedTaskRun, error) {
func ResolveTaskRun(tr *v1alpha1.TaskRun, getTask GetTask, gr GetResource) (*ResolvedTaskRun, error) {
var err error
rtr := ResolvedTaskRun{
Inputs: map[string]*v1alpha1.PipelineResource{},
Outputs: map[string]*v1alpha1.PipelineResource{},
}

rtr.Task, err = getTask(tr.TaskRef.Name)
rtr.TaskSpec, rtr.TaskName, err = getTaskSpec(tr, getTask)
if err != nil {
return nil, fmt.Errorf("couldn't retrieve referenced Task %q: %s", tr.TaskRef.Name, err)
return nil, fmt.Errorf("couldn't retrieve referenced Task %q: %s", tr.Spec.TaskRef.Name, err)
}
for _, r := range tr.Inputs.Resources {

for _, r := range tr.Spec.Inputs.Resources {
rr, err := gr(r.ResourceRef.Name)
if err != nil {
return nil, fmt.Errorf("couldn't retrieve referenced input PipelineResource %q: %s", r.ResourceRef.Name, err)
}
rtr.Inputs[r.Name] = rr
}
for _, r := range tr.Outputs.Resources {
for _, r := range tr.Spec.Outputs.Resources {
rr, err := gr(r.ResourceRef.Name)
if err != nil {
return nil, fmt.Errorf("couldn't retrieve referenced output PipelineResource %q: %s", r.ResourceRef.Name, err)
Expand All @@ -70,3 +72,23 @@ func ResolveTaskRun(tr *v1alpha1.TaskRunSpec, getTask GetTask, gr GetResource) (
}
return &rtr, nil
}

func getTaskSpec(taskRun *v1alpha1.TaskRun, getTask GetTask) (*v1alpha1.TaskSpec, string, error) {
taskSpec := &v1alpha1.TaskSpec{}
taskName := ""
if taskRun.Spec.TaskRef != nil && taskRun.Spec.TaskRef.Name != "" {
// Get related task for taskrun
t, err := getTask(taskRun.Spec.TaskRef.Name)
if err != nil {
return nil, taskName, fmt.Errorf("error when listing tasks for taskRun %s %v", taskRun.Name, err)
}
taskSpec = &t.Spec
taskName = t.Name
} else if taskRun.Spec.TaskSpec != nil {
taskSpec = taskRun.Spec.TaskSpec
taskName = taskRun.Name
} else {
return taskSpec, taskName, fmt.Errorf("TaskRun %s not providing TaskRef or TaskSpec", taskRun.Name)
}
return taskSpec, taskName, nil
}
Loading

0 comments on commit 17de7e2

Please sign in to comment.