Skip to content

Commit

Permalink
Move the allowed output resource to a map
Browse files Browse the repository at this point in the history
Add a map that defines which output resource produces an output
that should be copied to the PVC. Add unit tests for it too.
  • Loading branch information
afrittoli committed Jan 22, 2019
1 parent 8ddbc96 commit b116f07
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 24 deletions.
59 changes: 35 additions & 24 deletions pkg/reconciler/v1alpha1/taskrun/resources/output_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ import (

var (
outputDir = "/workspace/output/"

// allowedOutputResource checks if an output resource type produces
// an output that should be copied to the PVC
allowedOutputResources = map[v1alpha1.PipelineResourceType]bool{
v1alpha1.PipelineResourceTypeStorage: true,
v1alpha1.PipelineResourceTypeGit: true,
}
)

// AddOutputResources reads the output resources and adds the corresponding container steps
Expand Down Expand Up @@ -109,30 +116,27 @@ func AddOutputResources(
// outputs, or until we have metadata on the resource that declares whether
// the output should be copied to the PVC, only copy git and storage output
// resources.
if (resource.Spec.Type == v1alpha1.PipelineResourceTypeStorage ||
resource.Spec.Type == v1alpha1.PipelineResourceTypeGit) {
// copy to pvc if pvc is present
if pipelineRunpvcName != "" {
var newSteps []corev1.Container
for _, dPath := range boundResource.Paths {
newSteps = append(newSteps, []corev1.Container{{
Name: fmt.Sprintf("source-mkdir-%s", resource.GetName()),
Image: *bashNoopImage,
Args: []string{
"-args", strings.Join([]string{"mkdir", "-p", dPath}, " "),
},
VolumeMounts: []corev1.VolumeMount{getPvcMount(pipelineRunpvcName)},
}, {
Name: fmt.Sprintf("source-copy-%s", resource.GetName()),
Image: *bashNoopImage,
Args: []string{
"-args", strings.Join([]string{"cp", "-r", fmt.Sprintf("%s/.", sourcePath), dPath}, " "),
},
VolumeMounts: []corev1.VolumeMount{getPvcMount(pipelineRunpvcName)},
}}...)
}
b.Spec.Steps = append(b.Spec.Steps, newSteps...)
}
// copy to pvc if pvc is present
if allowedOutputResources[resource.Spec.Type] && pipelineRunpvcName != "" {
var newSteps []corev1.Container
for _, dPath := range boundResource.Paths {
newSteps = append(newSteps, []corev1.Container{{
Name: fmt.Sprintf("source-mkdir-%s", resource.GetName()),
Image: *bashNoopImage,
Args: []string{
"-args", strings.Join([]string{"mkdir", "-p", dPath}, " "),
},
VolumeMounts: []corev1.VolumeMount{getPvcMount(pipelineRunpvcName)},
}, {
Name: fmt.Sprintf("source-copy-%s", resource.GetName()),
Image: *bashNoopImage,
Args: []string{
"-args", strings.Join([]string{"cp", "-r", fmt.Sprintf("%s/.", sourcePath), dPath}, " "),
},
VolumeMounts: []corev1.VolumeMount{getPvcMount(pipelineRunpvcName)},
}}...)
}
b.Spec.Steps = append(b.Spec.Steps, newSteps...)
}
}

Expand Down Expand Up @@ -193,3 +197,10 @@ func addStoreUploadStep(build *buildv1alpha1.Build,
build.Spec.Steps = append(build.Spec.Steps, buildSteps...)
return nil
}

// allowedOutputResource checks if an output resource type produces
// an output that should be copied to the PVC
func allowedOutputResource(resourceType v1alpha1.PipelineResourceType) bool {

return allowedOutputResources[resourceType]
}
26 changes: 26 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/resources/output_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -763,3 +763,29 @@ func Test_InValid_OutputResources(t *testing.T) {
})
}
}

func Test_AllowedOutputResource(t *testing.T) {
for _, c := range []struct {
desc string
resourceType v1alpha1.PipelineResourceType
expectedAllowed bool
}{{
desc: "storage type is allowed",
resourceType: v1alpha1.PipelineResourceTypeStorage,
expectedAllowed: true,
}, {
desc: "git type is allowed",
resourceType: v1alpha1.PipelineResourceTypeGit,
expectedAllowed: true,
}, {
desc: "anything else is not allowed",
resourceType: "fooType",
expectedAllowed: false,
}} {
t.Run(c.desc, func(t *testing.T) {
if c.expectedAllowed != allowedOutputResources[c.resourceType] {
t.Fatalf("Test AllowedOutputResource %s expected %t but got %t", c.desc, c.expectedAllowed, allowedOutputResources[c.resourceType])
}
})
}
}

0 comments on commit b116f07

Please sign in to comment.