Skip to content

Commit

Permalink
Refactor TaskRun to support processing of TaskSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
nader-ziada committed Nov 22, 2018
1 parent 4af81da commit 1495fbe
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ func TestUpdateTaskRunsState(t *testing.T) {
},
Spec: v1alpha1.TaskRunSpec{
ServiceAccount: "test-sa",
TaskRef: v1alpha1.TaskRef{
TaskRef: &v1alpha1.TaskRef{
Name: "unit-test-task",
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,9 @@ func TestAddResourceToBuild(t *testing.T) {
Name: "simpleTask",
},
Inputs: v1alpha1.TaskRunInputs{

Resources: []v1alpha1.TaskRunResource{{
ResourceRef: v1alpha1.PipelineResourceRef{
Name: "the-git",
Name: "the-git-with-branch",
},
Name: "workspace",
}},
Expand Down Expand Up @@ -474,7 +473,7 @@ func TestAddResourceToBuild(t *testing.T) {
}} {
t.Run(c.desc, func(t *testing.T) {
setUp()
got, err := AddInputResource(c.build, c.task, c.taskRun, pipelineResourceLister, logger)
got, err := AddInputResource(c.build, c.task.Name, c.task.Spec, c.taskRun, pipelineResourceLister, logger)
if (err != nil) != c.wantErr {
t.Errorf("Test: %q; NewControllerConfigFromConfigMap() error = %v, WantErr %v", c.desc, err, c.wantErr)
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/reconciler/v1alpha1/taskrun/resources/input_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,35 @@ func getBoundResource(resourceName string, boundResources []v1alpha1.TaskRunReso
// AddInputResource will update the input build with the input resource from the task
func AddInputResource(
build *buildv1alpha1.Build,
task *v1alpha1.Task,
taskName string,
taskSpec v1alpha1.TaskSpec,
taskRun *v1alpha1.TaskRun,
pipelineResourceLister listers.PipelineResourceLister,
logger *zap.SugaredLogger,
) (*buildv1alpha1.Build, error) {

if task.Spec.Inputs == nil {
if taskSpec.Inputs == nil {
return build, nil
}

var gitResource *v1alpha1.GitResource
for _, input := range task.Spec.Inputs.Resources {
for _, input := range taskSpec.Inputs.Resources {
boundResource, err := getBoundResource(input.Name, taskRun.Spec.Inputs.Resources)
if err != nil {
return nil, fmt.Errorf("Failed to get bound resource: %s", err)
}

resource, err := pipelineResourceLister.PipelineResources(task.Namespace).Get(boundResource.ResourceRef.Name)
resource, err := pipelineResourceLister.PipelineResources(taskRun.Namespace).Get(boundResource.ResourceRef.Name)
if err != nil {
return nil, fmt.Errorf("task %q failed to Get Pipeline Resource: %q", task.Name, boundResource)
return nil, fmt.Errorf("task %q failed to Get Pipeline Resource: %q", taskName, boundResource)
}

switch resource.Spec.Type {
case v1alpha1.PipelineResourceTypeGit:
{
gitResource, err = v1alpha1.NewGitResource(resource)
if err != nil {
return nil, fmt.Errorf("task %q invalid Pipeline Resource: %q", task.Name, boundResource.ResourceRef.Name)
return nil, fmt.Errorf("task %q invalid Pipeline Resource: %q", taskName, boundResource.ResourceRef.Name)
}
gitSourceSpec := &buildv1alpha1.GitSourceSpec{
Url: gitResource.URL,
Expand All @@ -80,7 +81,7 @@ func AddInputResource(
case v1alpha1.PipelineResourceTypeCluster:
clusterResource, err := v1alpha1.NewClusterResource(resource)
if err != nil {
return nil, fmt.Errorf("task %q invalid Pipeline Resource: %q", task.Name, boundResource.ResourceRef.Name)
return nil, fmt.Errorf("task %q invalid Pipeline Resource: %q", taskName, boundResource.ResourceRef.Name)
}
addClusterBuildStep(build, clusterResource)
}
Expand Down
54 changes: 39 additions & 15 deletions pkg/reconciler/v1alpha1/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,17 @@ func (c *Reconciler) reconcile(ctx context.Context, tr *v1alpha1.TaskRun) error
build, err = c.createBuild(ctx, tr, pvc.Name)
if err != nil {
// This Run has failed, so we need to mark it as failed and stop reconciling it
var msg string
if tr.Spec.TaskRef != nil {
msg = fmt.Sprintf("References a Task %s that doesn't exist: ", fmt.Sprintf("%s/%s", tr.Namespace, tr.Spec.TaskRef.Name))
} else {
msg = fmt.Sprintf("References a TaskSpec with missing information: ")
}
tr.Status.SetCondition(&duckv1alpha1.Condition{
Type: duckv1alpha1.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: ReasonCouldntGetTask,
Message: fmt.Sprintf("References a Task %s that doesn't exist: %v",
fmt.Sprintf("%s/%s", tr.Namespace, tr.Spec.TaskRef.Name), err),
Type: duckv1alpha1.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: ReasonCouldntGetTask,
Message: fmt.Sprintf("%s %v", msg, err),
})
c.Recorder.Eventf(tr, corev1.EventTypeWarning, "BuildCreationFailed", "Failed to create build %q: %v", tr.Name, err)
c.Logger.Errorf("Failed to create build for task %q :%v", err, tr.Name)
Expand Down Expand Up @@ -317,19 +322,38 @@ func (c *Reconciler) createPVC(tr *v1alpha1.TaskRun) (*corev1.PersistentVolumeCl
return v, nil
}

func (c *Reconciler) getTaskSpec(tr *v1alpha1.TaskRun) (v1alpha1.TaskSpec, string, error) {
taskSpec := v1alpha1.TaskSpec{}
taskName := ""
if tr.Spec.TaskRef != nil && tr.Spec.TaskRef.Name != "" {
// Get related task for taskrun
t, err := c.taskLister.Tasks(tr.Namespace).Get(tr.Spec.TaskRef.Name)
if err != nil {
return taskSpec, taskName, fmt.Errorf("error when listing tasks %v", err)
}
taskSpec = t.Spec
taskName = t.Name
} else if tr.Spec.TaskSpec != nil {
taskSpec = *tr.Spec.TaskSpec
taskName = tr.Name
} else {
return taskSpec, taskName, fmt.Errorf("TaskRun %s not providing TaskRef or TaskSpec", tr.Name)
}
return taskSpec, taskName, nil
}

// createBuild creates a build from the task, using the task's buildspec
// with pvcName as a volumeMount
func (c *Reconciler) createBuild(ctx context.Context, tr *v1alpha1.TaskRun, pvcName string) (*buildv1alpha1.Build, error) {
// Get related task for taskrun
t, err := c.taskLister.Tasks(tr.Namespace).Get(tr.Spec.TaskRef.Name)
ts, taskName, err := c.getTaskSpec(tr)
if err != nil {
return nil, fmt.Errorf("error when listing tasks %v", err)
return nil, fmt.Errorf("taskRun %s has nil BuildSpec", tr.Name)
}

// TODO: Preferably use Validate on task.spec to catch validation error
bs := t.Spec.GetBuildSpec()
bs := ts.GetBuildSpec()
if bs == nil {
return nil, fmt.Errorf("task %s has nil BuildSpec", t.Name)
return nil, fmt.Errorf("task %s has nil BuildSpec", taskName)
}

// For each step with no entrypoint set, try to populate it with the info
Expand All @@ -352,25 +376,25 @@ func (c *Reconciler) createBuild(ctx context.Context, tr *v1alpha1.TaskRun, pvcN
return nil, fmt.Errorf("couldn't create redirected Build: %v", err)
}

build, err := resources.AddInputResource(b, t, tr, c.resourceLister, c.Logger)
build, err := resources.AddInputResource(b, taskName, ts, tr, c.resourceLister, c.Logger)
if err != nil {
c.Logger.Errorf("Failed to create a build for taskrun: %s due to input resource error %v", tr.Name, err)
return nil, err
}

var defaults []v1alpha1.TaskParam
if t.Spec.Inputs != nil {
defaults = append(defaults, t.Spec.Inputs.Params...)
if ts.Inputs != nil {
defaults = append(defaults, ts.Inputs.Params...)
}
// Apply parameter templating from the taskrun.
build = resources.ApplyParameters(build, tr, defaults...)

// Apply bound resource templating from the taskrun.
build, err = resources.ApplyResources(build, tr.Spec.Inputs.Resources, c.resourceLister.PipelineResources(t.Namespace), "inputs")
build, err = resources.ApplyResources(build, tr.Spec.Inputs.Resources, c.resourceLister.PipelineResources(tr.Namespace), "inputs")
if err != nil {
return nil, fmt.Errorf("couldnt apply input resource templating: %s", err)
}
build, err = resources.ApplyResources(build, tr.Spec.Outputs.Resources, c.resourceLister.PipelineResources(t.Namespace), "outputs")
build, err = resources.ApplyResources(build, tr.Spec.Outputs.Resources, c.resourceLister.PipelineResources(tr.Namespace), "outputs")
if err != nil {
return nil, fmt.Errorf("couldnt apply output resource templating: %s", err)
}
Expand Down
Loading

0 comments on commit 1495fbe

Please sign in to comment.