Skip to content

Commit

Permalink
fix: the pipelinerun never done due to repeated workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
cugykw authored and tekton-robot committed Nov 11, 2022
1 parent ca7c70c commit 38c739a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/apis/pipeline/v1/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,13 @@ func validateExecutionStatusVariablesExpressions(expressions []string, ptNames s
}

func (pt *PipelineTask) validateWorkspaces(workspaceNames sets.String) (errs *apis.FieldError) {
workspaceBindingNames := sets.NewString()
for i, ws := range pt.Workspaces {
if workspaceBindingNames.Has(ws.Name) {
errs = errs.Also(apis.ErrGeneric(
fmt.Sprintf("workspace name %q must be unique", ws.Name), "").ViaFieldIndex("workspaces", i))
}

if ws.Workspace == "" {
if !workspaceNames.Has(ws.Name) {
errs = errs.Also(apis.ErrInvalidValue(
Expand All @@ -434,6 +440,8 @@ func (pt *PipelineTask) validateWorkspaces(workspaceNames sets.String) (errs *ap
"",
).ViaFieldIndex("workspaces", i))
}

workspaceBindingNames.Insert(ws.Name)
}
return errs
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/apis/pipeline/v1/pipeline_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,29 @@ func TestValidatePipelineWorkspacesUsage_Failure(t *testing.T) {
Message: `invalid value: pipeline task "foo" expects workspace with name "taskWorkspaceName" but none exists in pipeline spec`,
Paths: []string{"tasks[0].workspaces[0]"},
},
}, {
name: "invalid pipeline task use duplicate workspace binding name",
workspaces: []PipelineWorkspaceDeclaration{{
Name: "foo",
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo"},
Workspaces: []WorkspacePipelineTaskBinding{
{
Name: "repo",
Workspace: "foo",
},
{
Name: "repo",
Workspace: "foo",
},
},
}},
expectedError: apis.FieldError{
Message: `workspace name "repo" must be unique`,
Paths: []string{"tasks[0].workspaces[1]"},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipeline_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,13 @@ func (pt *PipelineTask) validateExecutionStatusVariablesAllowed(ptNames sets.Str
}

func (pt *PipelineTask) validateWorkspaces(workspaceNames sets.String) (errs *apis.FieldError) {
workspaceBindingNames := sets.NewString()
for i, ws := range pt.Workspaces {
if workspaceBindingNames.Has(ws.Name) {
errs = errs.Also(apis.ErrGeneric(
fmt.Sprintf("workspace name %q must be unique", ws.Name), "").ViaFieldIndex("workspaces", i))
}

if ws.Workspace == "" {
if !workspaceNames.Has(ws.Name) {
errs = errs.Also(apis.ErrInvalidValue(
Expand All @@ -420,6 +426,8 @@ func (pt *PipelineTask) validateWorkspaces(workspaceNames sets.String) (errs *ap
"",
).ViaFieldIndex("workspaces", i))
}

workspaceBindingNames.Insert(ws.Name)
}
return errs
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipeline_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2314,6 +2314,29 @@ func TestValidatePipelineWorkspacesUsage_Failure(t *testing.T) {
Message: `invalid value: pipeline task "foo" expects workspace with name "taskWorkspaceName" but none exists in pipeline spec`,
Paths: []string{"tasks[0].workspaces[0]"},
},
}, {
name: "invalid pipeline task use duplicate workspace binding name",
workspaces: []PipelineWorkspaceDeclaration{{
Name: "foo",
}},
tasks: []PipelineTask{{
Name: "foo",
TaskRef: &TaskRef{Name: "foo"},
Workspaces: []WorkspacePipelineTaskBinding{
{
Name: "repo",
Workspace: "foo",
},
{
Name: "repo",
Workspace: "foo",
},
},
}},
expectedError: apis.FieldError{
Message: `workspace name "repo" must be unique`,
Paths: []string{"tasks[0].workspaces[1]"},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 38c739a

Please sign in to comment.