pipeline/pkg/reconciler/pipelinerun/resources/input_output_steps.go
Christie Wilson 73ba02be1b Use the same logic to resolve spec vs ref in Pipelines + Tasks
In #1324 we updated PipelineRuns to allow for embedding ResourceSpecs in
PipelineRuns. This commit makes it so that the logic for resolving (i.e.
deciding if PipelineResources are specified by Spec or Ref) is shared by
PipelineRuns + TaskRuns. This is done by making it so that the "binding"
uses the same type underneath. The only reason they can't be the exact
same type is that TaskRuns additionally need the "path" attribute, which
is actually only used for PVC copying, which will be removed in #1284,
and then we should be able to remove paths entirely and the type can be
the same.

Also added some additional comments around the use of `SelfLink`, and
made sure it was well covered in the reconciler test.
2019-10-01 09:44:14 -05:00

109 lines
4.2 KiB
Go

/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package resources
import (
"path/filepath"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
)
// GetOutputSteps will add the correct `path` to the input resources for pt
func GetOutputSteps(outputs map[string]*v1alpha1.PipelineResource, taskName, storageBasePath string) []v1alpha1.TaskResourceBinding {
var taskOutputResources []v1alpha1.TaskResourceBinding
for name, outputResource := range outputs {
taskOutputResource := v1alpha1.TaskResourceBinding{
PipelineResourceBinding: v1alpha1.PipelineResourceBinding{
Name: name,
},
Paths: []string{filepath.Join(storageBasePath, taskName, name)},
}
// SelfLink is being checked there to determine if this PipelineResource is an instance that
// exists in the cluster (in which case Kubernetes will populate this field) or is specified by Spec
if outputResource.SelfLink != "" {
taskOutputResource.ResourceRef = v1alpha1.PipelineResourceRef{
Name: outputResource.Name,
APIVersion: outputResource.APIVersion,
}
} else if outputResource.Spec.Type != "" {
taskOutputResource.ResourceSpec = &v1alpha1.PipelineResourceSpec{
Type: outputResource.Spec.Type,
Params: outputResource.Spec.Params,
SecretParams: outputResource.Spec.SecretParams,
}
}
taskOutputResources = append(taskOutputResources, taskOutputResource)
}
return taskOutputResources
}
// GetInputSteps will add the correct `path` to the input resources for pt. If the resources are provided by
// a previous task, the correct `path` will be used so that the resource provided by that task will be used.
func GetInputSteps(inputs map[string]*v1alpha1.PipelineResource, pt *v1alpha1.PipelineTask, storageBasePath string) []v1alpha1.TaskResourceBinding {
var taskInputResources []v1alpha1.TaskResourceBinding
for name, inputResource := range inputs {
taskInputResource := v1alpha1.TaskResourceBinding{
PipelineResourceBinding: v1alpha1.PipelineResourceBinding{
Name: name,
},
}
// SelfLink is being checked there to determine if this PipelineResource is an instance that
// exists in the cluster (in which case Kubernetes will populate this field) or is specified by Spec
if inputResource.SelfLink != "" {
taskInputResource.ResourceRef = v1alpha1.PipelineResourceRef{
Name: inputResource.Name,
APIVersion: inputResource.APIVersion,
}
} else if inputResource.Spec.Type != "" {
taskInputResource.ResourceSpec = &v1alpha1.PipelineResourceSpec{
Type: inputResource.Spec.Type,
Params: inputResource.Spec.Params,
SecretParams: inputResource.Spec.SecretParams,
}
}
var stepSourceNames []string
if pt.Resources != nil {
for _, pipelineTaskInput := range pt.Resources.Inputs {
if pipelineTaskInput.Name == name {
for _, constr := range pipelineTaskInput.From {
stepSourceNames = append(stepSourceNames, filepath.Join(storageBasePath, constr, name))
}
}
}
}
if len(stepSourceNames) > 0 {
taskInputResource.Paths = append(taskInputResource.Paths, stepSourceNames...)
}
taskInputResources = append(taskInputResources, taskInputResource)
}
return taskInputResources
}
// WrapSteps will add the correct `paths` to all of the inputs and outputs for pt
func WrapSteps(tr *v1alpha1.TaskRunSpec, pt *v1alpha1.PipelineTask, inputs, outputs map[string]*v1alpha1.PipelineResource, storageBasePath string) {
if pt == nil {
return
}
// Add presteps to setup updated input
tr.Inputs.Resources = append(tr.Inputs.Resources, GetInputSteps(inputs, pt, storageBasePath)...)
// Add poststeps to setup outputs
tr.Outputs.Resources = append(tr.Outputs.Resources, GetOutputSteps(outputs, pt.Name, storageBasePath)...)
}