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

Port Workspace in pipeline support to v1alpha2 🎋 #1949

Merged
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
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.