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

Disallow the Cluster and Git resource types as Output resources. #1109

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 18 additions & 4 deletions pkg/apis/pipeline/v1alpha1/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,29 @@ type PipelineResourceList struct {
Items []PipelineResource `json:"items"`
}

// ResourceFromType returns a PipelineResourceInterface from a PipelineResource's type.
func ResourceFromType(r *PipelineResource) (PipelineResourceInterface, error) {
// InputResourceFromType returns a PipelineResourceInterface from a PipelineResource's type.
func InputResourceFromType(r *PipelineResource) (PipelineResourceInterface, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

would it be crazy in Go to define a top level like map of types to New functions? e.g. something like:

PipelineResourceTypeGit: NewGitResource,
PipelineResourceTypeImage: NewImageResource,

If case statements are clearer, just ignore me

switch r.Spec.Type {
case PipelineResourceTypeGit:
return NewGitResource(r)
case PipelineResourceTypeImage:
return NewImageResource(r)
case PipelineResourceTypeCluster:
return NewClusterResource(r)
default:
return InputOutputResourceFromType(r)
Copy link
Collaborator

Choose a reason for hiding this comment

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

i found it confusing that the default for this function calls InputOutputResourceFromType - id find it a bit easier to see explicitly which types are allowed for input and output, and always default to an error

}
}

// OutputResourceFromType returns a PipelineResourceInterface from a PipelineResource's type.
func OutputResourceFromType(r *PipelineResource) (PipelineResourceInterface, error) {
// We currently have no output-only resources.
return InputOutputResourceFromType(r)
}

// InputOutputResourceFromType returns a PipelineResourceInterface from a PipelineResource's type.
func InputOutputResourceFromType(r *PipelineResource) (PipelineResourceInterface, error) {
switch r.Spec.Type {
case PipelineResourceTypeImage:
return NewImageResource(r)
case PipelineResourceTypeStorage:
return NewStorageResource(r)
case PipelineResourceTypePullRequest:
Expand Down
6 changes: 3 additions & 3 deletions pkg/reconciler/v1alpha1/taskrun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ var outputs = map[string]v1alpha1.PipelineResourceInterface{
"bucket": gcsResource,
}

var gitResource, _ = v1alpha1.ResourceFromType(&v1alpha1.PipelineResource{
var gitResource, _ = v1alpha1.InputResourceFromType(&v1alpha1.PipelineResource{
ObjectMeta: metav1.ObjectMeta{
Name: "git-resource",
},
Expand All @@ -173,7 +173,7 @@ var gitResource, _ = v1alpha1.ResourceFromType(&v1alpha1.PipelineResource{
},
})

var imageResource, _ = v1alpha1.ResourceFromType(&v1alpha1.PipelineResource{
var imageResource, _ = v1alpha1.InputResourceFromType(&v1alpha1.PipelineResource{
ObjectMeta: metav1.ObjectMeta{
Name: "image-resource",
},
Expand All @@ -186,7 +186,7 @@ var imageResource, _ = v1alpha1.ResourceFromType(&v1alpha1.PipelineResource{
},
})

var gcsResource, _ = v1alpha1.ResourceFromType(&v1alpha1.PipelineResource{
var gcsResource, _ = v1alpha1.InputResourceFromType(&v1alpha1.PipelineResource{
ObjectMeta: metav1.ObjectMeta{
Name: "gcs-resource",
},
Expand Down
101 changes: 4 additions & 97 deletions pkg/reconciler/v1alpha1/taskrun/resources/input_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func setUp(t *testing.T) {
}}
inputResourceInterfaces = make(map[string]v1alpha1.PipelineResourceInterface)
for _, r := range rs {
ri, _ := v1alpha1.ResourceFromType(r)
ri, _ := v1alpha1.InputResourceFromType(r)
inputResourceInterfaces[r.Name] = ri
}
}
Expand Down Expand Up @@ -408,52 +408,6 @@ func TestAddResourceToTask(t *testing.T) {
WorkingDir: "/workspace",
}},
},
}, {
desc: "git resource as input from previous task",
task: task,
taskRun: &v1alpha1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "get-from-git",
Namespace: "marshmallow",
OwnerReferences: []metav1.OwnerReference{{
Kind: "PipelineRun",
Name: "pipelinerun",
}},
},
Spec: v1alpha1.TaskRunSpec{
Inputs: v1alpha1.TaskRunInputs{
Resources: []v1alpha1.TaskResourceBinding{{
ResourceRef: v1alpha1.PipelineResourceRef{
Name: "the-git",
},
Name: "gitspace",
Paths: []string{"prev-task-path"},
}},
},
},
},
wantErr: false,
want: &v1alpha1.TaskSpec{
Inputs: gitInputs,
Steps: []corev1.Container{{
Name: "create-dir-gitspace-mz4c7",
Image: "override-with-bash-noop:latest",
Command: []string{"/ko-app/bash"},
Args: []string{"-args", "mkdir -p /workspace/gitspace"},
}, {
Name: "source-copy-gitspace-9l9zj",
Image: "override-with-bash-noop:latest",
Command: []string{"/ko-app/bash"},
Args: []string{"-args", "cp -r prev-task-path/. /workspace/gitspace"},
VolumeMounts: []corev1.VolumeMount{{MountPath: "/pvc", Name: "pipelinerun-pvc"}},
}},
Volumes: []corev1.Volume{{
Name: "pipelinerun-pvc",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ClaimName: "pipelinerun-pvc"},
},
}},
},
}, {
desc: "storage resource as input with target path",
task: taskWithTargetPath,
Expand Down Expand Up @@ -878,15 +832,6 @@ func TestStorageInputResource(t *testing.T) {
}

func TestAddStepsToTaskWithBucketFromConfigMap(t *testing.T) {
task := &v1alpha1.Task{
ObjectMeta: metav1.ObjectMeta{
Name: "build-from-repo",
Namespace: "marshmallow",
},
Spec: v1alpha1.TaskSpec{
Inputs: gitInputs,
},
}
taskWithTargetPath := &v1alpha1.Task{
ObjectMeta: metav1.ObjectMeta{
Name: "task-with-targetpath",
Expand All @@ -903,44 +848,6 @@ func TestAddStepsToTaskWithBucketFromConfigMap(t *testing.T) {
taskRun *v1alpha1.TaskRun
want *v1alpha1.TaskSpec
}{{
desc: "git resource as input from previous task - copy to bucket",
task: task,
taskRun: &v1alpha1.TaskRun{
ObjectMeta: metav1.ObjectMeta{
Name: "get-from-git",
Namespace: "marshmallow",
OwnerReferences: []metav1.OwnerReference{{
Kind: "PipelineRun",
Name: "pipelinerun",
}},
},
Spec: v1alpha1.TaskRunSpec{
Inputs: v1alpha1.TaskRunInputs{
Resources: []v1alpha1.TaskResourceBinding{{
ResourceRef: v1alpha1.PipelineResourceRef{
Name: "the-git",
},
Name: "gitspace",
Paths: []string{"prev-task-path"},
}},
},
},
},
want: &v1alpha1.TaskSpec{
Inputs: gitInputs,
Steps: []corev1.Container{{
Name: "artifact-dest-mkdir-gitspace-mssqb",
Image: "override-with-bash-noop:latest",
Command: []string{"/ko-app/bash"},
Args: []string{"-args", "mkdir -p /workspace/gitspace"},
}, {
Name: "artifact-copy-from-gitspace-78c5n",
Image: "override-with-gsutil-image:latest",
Command: []string{"/ko-app/gsutil"},
Args: []string{"-args", "cp -P -r gs://fake-bucket/prev-task-path/* /workspace/gitspace"},
}},
},
}, {
desc: "storage resource as input from previous task - copy from bucket",
task: taskWithTargetPath,
taskRun: &v1alpha1.TaskRun{
Expand All @@ -967,12 +874,12 @@ func TestAddStepsToTaskWithBucketFromConfigMap(t *testing.T) {
want: &v1alpha1.TaskSpec{
Inputs: gcsInputs,
Steps: []corev1.Container{{
Name: "artifact-dest-mkdir-workspace-6nl7g",
Name: "artifact-dest-mkdir-workspace-mssqb",
Image: "override-with-bash-noop:latest",
Command: []string{"/ko-app/bash"},
Args: []string{"-args", "mkdir -p /workspace/gcs-dir"},
}, {
Name: "artifact-copy-from-workspace-j2tds",
Name: "artifact-copy-from-workspace-78c5n",
Image: "override-with-gsutil-image:latest",
Command: []string{"/ko-app/gsutil"},
Args: []string{"-args", "cp -P -r gs://fake-bucket/prev-task-path/* /workspace/gcs-dir"},
Expand Down Expand Up @@ -1011,7 +918,7 @@ func mockResolveTaskResources(taskRun *v1alpha1.TaskRun) map[string]v1alpha1.Pip
i = inputResourceInterfaces[name]
resolved[r.Name] = i
} else if r.ResourceSpec != nil {
i, _ = v1alpha1.ResourceFromType(&v1alpha1.PipelineResource{
i, _ = v1alpha1.InputResourceFromType(&v1alpha1.PipelineResource{
ObjectMeta: metav1.ObjectMeta{
Name: r.Name,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ var (
// an output that should be copied to the PVC
allowedOutputResources = map[v1alpha1.PipelineResourceType]bool{
v1alpha1.PipelineResourceTypeStorage: true,
v1alpha1.PipelineResourceTypeGit: true,
}
)

Expand Down
Loading