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

Validate missing volume configs in Workspace Bindings #3096

Merged
merged 1 commit into from Aug 13, 2020
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
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1alpha1/taskrun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func validateWorkspaceBindings(ctx context.Context, wb []WorkspaceBinding) *apis
}
seen.Insert(w.Name)

if err := w.Validate(ctx); err != nil {
if err := w.Validate(ctx).ViaField("workspace"); err != nil {
return err
}
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipelinerun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func (ps *PipelineRunSpec) Validate(ctx context.Context) *apis.FieldError {
if ps.Workspaces != nil {
wsNames := make(map[string]int)
for idx, ws := range ps.Workspaces {
field := fmt.Sprintf("spec.workspaces[%d]", idx)
if err := ws.Validate(ctx).ViaField(field); err != nil {
return err
}
if prevIdx, alreadyExists := wsNames[ws.Name]; alreadyExists {
return &apis.FieldError{
Message: fmt.Sprintf("workspace %q provided by pipelinerun more than once, at index %d and %d", ws.Name, prevIdx, idx),
Expand Down
20 changes: 20 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipelinerun_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,26 @@ func TestPipelineRunSpec_Invalidate(t *testing.T) {
Message: `workspace "ws" provided by pipelinerun more than once, at index 0 and 1`,
Paths: []string{"spec.workspaces"},
},
}, {
name: "workspaces must contain a valid volume config",
spec: v1beta1.PipelineRunSpec{
PipelineRef: &v1beta1.PipelineRef{
Name: "pipelinerefname",
},
Workspaces: []v1beta1.WorkspaceBinding{{
Name: "ws",
}},
},
wantErr: &apis.FieldError{
Message: "expected exactly one, got neither",
Paths: []string{
"spec.workspaces[0].configmap",
"spec.workspaces[0].emptydir",
"spec.workspaces[0].persistentvolumeclaim",
"spec.workspaces[0].secret",
"spec.workspaces[0].volumeclaimtemplate",
},
},
}}
for _, ps := range tests {
t.Run(ps.name, func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/taskrun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func validateWorkspaceBindings(ctx context.Context, wb []WorkspaceBinding) *apis
}
seen.Insert(w.Name)

if err := w.Validate(ctx); err != nil {
if err := w.Validate(ctx).ViaField("workspace"); err != nil {
return err
}
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/apis/pipeline/v1beta1/workspace_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import (
// allVolumeSourceFields is a list of all the volume source field paths that a
// WorkspaceBinding may include.
var allVolumeSourceFields []string = []string{
"workspace.persistentvolumeclaim",
"workspace.volumeclaimtemplate",
"workspace.emptydir",
"workspace.configmap",
"workspace.secret",
"persistentvolumeclaim",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch!

"volumeclaimtemplate",
"emptydir",
"configmap",
"secret",
}

// Validate looks at the Volume provided in wb and makes sure that it is valid.
Expand All @@ -53,17 +53,17 @@ func (b *WorkspaceBinding) Validate(ctx context.Context) *apis.FieldError {

// For a PersistentVolumeClaim to work, you must at least provide the name of the PVC to use.
if b.PersistentVolumeClaim != nil && b.PersistentVolumeClaim.ClaimName == "" {
return apis.ErrMissingField("workspace.persistentvolumeclaim.claimname")
return apis.ErrMissingField("persistentvolumeclaim.claimname")
}

// For a ConfigMap to work, you must provide the name of the ConfigMap to use.
if b.ConfigMap != nil && b.ConfigMap.LocalObjectReference.Name == "" {
return apis.ErrMissingField("workspace.configmap.name")
return apis.ErrMissingField("configmap.name")
}

// For a Secret to work, you must provide the name of the Secret to use.
if b.Secret != nil && b.Secret.SecretName == "" {
return apis.ErrMissingField("workspace.secret.secretName")
return apis.ErrMissingField("secret.secretName")
}

return nil
Expand Down