Skip to content

Commit

Permalink
Port Workspace in pipeline support to v1alpha2 🎋
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester authored and tekton-robot committed Jan 28, 2020
1 parent 45045db commit d537876
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/apis/pipeline/v1alpha2/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ type PipelineSpec struct {
// Params declares a list of input parameters that must be supplied when
// this Pipeline is run.
Params []ParamSpec `json:"params,omitempty"`
// Workspaces declares a set of named workspaces that are expected to be
// provided by a PipelineRun.
// +optional
Workspaces []WorkspacePipelineDeclaration `json:"workspaces,omitempty"`
}

// PipelineTask defines a task in a Pipeline, passing inputs from both
Expand Down Expand Up @@ -111,6 +115,11 @@ type PipelineTask struct {
// Parameters declares parameters passed to this task.
// +optional
Params []Param `json:"params,omitempty"`

// Workspaces maps workspaces from the pipeline spec to the workspaces
// declared in the Task.
// +optional
Workspaces []WorkspacePipelineTaskBinding `json:"workspaces,omitempty"`
}

func (pt PipelineTask) HashKey() string {
Expand Down
33 changes: 33 additions & 0 deletions pkg/apis/pipeline/v1alpha2/pipeline_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,39 @@ func (ps *PipelineSpec) Validate(ctx context.Context) *apis.FieldError {
return err
}

// Validate the pipeline's workspaces.
if err := validatePipelineWorkspaces(ps.Workspaces, ps.Tasks); err != nil {
return err
}

return nil
}

func validatePipelineWorkspaces(wss []WorkspacePipelineDeclaration, pts []PipelineTask) *apis.FieldError {
// Workspace names must be non-empty and unique.
wsTable := make(map[string]struct{})
for i, ws := range wss {
if ws.Name == "" {
return apis.ErrInvalidValue(fmt.Sprintf("workspace %d has empty name", i), "spec.workspaces")
}
if _, ok := wsTable[ws.Name]; ok {
return apis.ErrInvalidValue(fmt.Sprintf("workspace with name %q appears more than once", ws.Name), "spec.workspaces")
}
wsTable[ws.Name] = struct{}{}
}

// Any workspaces used in PipelineTasks should have their name declared in the Pipeline's
// Workspaces list.
for ptIdx, pt := range pts {
for wsIdx, ws := range pt.Workspaces {
if _, ok := wsTable[ws.Workspace]; !ok {
return apis.ErrInvalidValue(
fmt.Sprintf("pipeline task %q expects workspace with name %q but none exists in pipeline spec", pt.Name, ws.Workspace),
fmt.Sprintf("spec.tasks[%d].workspaces[%d]", ptIdx, wsIdx),
)
}
}
}
return nil
}

Expand Down
46 changes: 46 additions & 0 deletions pkg/apis/pipeline/v1alpha2/pipeline_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,52 @@ func TestPipeline_Validate(t *testing.T) {
},
},
failureExpected: true,
}, {
name: "unused pipeline spec workspaces do not cause an error",
p: &v1alpha2.Pipeline{
ObjectMeta: metav1.ObjectMeta{Name: "pipeline"},
Spec: v1alpha2.PipelineSpec{
Tasks: []v1alpha2.PipelineTask{{
Name: "foo", TaskRef: &v1alpha2.TaskRef{Name: "foo"},
}},
Workspaces: []v1alpha2.WorkspacePipelineDeclaration{{
Name: "foo",
}, {
Name: "bar",
}},
},
},
failureExpected: false,
}, {
name: "workspace bindings relying on a non-existent pipeline workspace cause an error",
p: &v1alpha2.Pipeline{
ObjectMeta: metav1.ObjectMeta{Name: "pipeline"},
Spec: v1alpha2.PipelineSpec{
Tasks: []v1alpha2.PipelineTask{{
Name: "foo", TaskRef: &v1alpha2.TaskRef{Name: "foo"},
Workspaces: []v1alpha2.WorkspacePipelineTaskBinding{{
Name: "taskWorkspaceName",
Workspace: "pipelineWorkspaceName",
}},
}},
Workspaces: []v1alpha2.WorkspacePipelineDeclaration{{
Name: "foo",
}},
},
},
failureExpected: true,
}, {
name: "multiple workspaces sharing the same name are not allowed",
p: &v1alpha2.Pipeline{
ObjectMeta: metav1.ObjectMeta{Name: "pipeline"}, Spec: v1alpha2.PipelineSpec{
Workspaces: []v1alpha2.WorkspacePipelineDeclaration{{
Name: "foo",
}, {
Name: "foo",
}},
},
},
failureExpected: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/pipeline/v1alpha2/zz_generated.deepcopy.go

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

0 comments on commit d537876

Please sign in to comment.