Skip to content

Commit

Permalink
Add Description and Default fields to TaskParams
Browse files Browse the repository at this point in the history
This commit adds the `Description` and `Default` fields to the
TaskParams object. Description is a mandatory field to be used for
describing what the Param is. Default is optional, and may be used to
specify a default value for the param.

Adding these fields will make the pipeline easier to understand.

Also fixes a bug in `resources.AddInputResources` where if the Task has
Inputs defined, AddInputResources assumes there *must* be one of type
GitResource, which is not necessarily the case.

Fixes tektoncd#190
  • Loading branch information
tannerb authored and Tanner Bruce committed Oct 30, 2018
1 parent a4a9277 commit e268e1e
Show file tree
Hide file tree
Showing 16 changed files with 258 additions and 21 deletions.
1 change: 1 addition & 0 deletions examples/build_task.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ spec:
type: git
params:
- name: pathToDockerFile
description: The path to the dockerfile to build
outputs:
resources:
- name: builtImage
Expand Down
5 changes: 5 additions & 0 deletions examples/deploy_tasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ spec:
type: git
params:
- name: pathToHelmCharts
description: Path to where the Helm charts live
- name: helmArgs
description: Extra arguments to pass to Helm
- name: image
desription: The image to override the Helm chart with
clusters:
- name: targetCluster
buildSpec:
Expand All @@ -33,7 +36,9 @@ spec:
type: git
params:
- name: kubectlArgs
description: Extra arguments to pass to kubectl
- name: pathToFiles
description: Path to the manifests to apply
clusters:
- name: targetCluster
buildSpec:
Expand Down
3 changes: 3 additions & 0 deletions examples/test_tasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ spec:
type: git
params:
- name: makeTarget
description: The target to have make run
outputs:
results:
- name: testResults
Expand All @@ -34,7 +35,9 @@ spec:
type: git
params:
- name: testImage
description: The image to use while running the test
- name: testArgs
description: The arguments to pass to the image
outputs:
results:
- name: testResults
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/pipeline/v1alpha1/task_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ type Inputs struct {
// such as resources.
type TaskParam struct {
Name string `json:"name"`
Description string `json:"description"`
// +optional
Default string `json:"default,omitempty"`
}

// Param declares a value to use for the Param called Name.
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/pipeline/v1alpha1/task_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (ts *TaskSpec) Validate() *apis.FieldError {
if err := checkForDuplicates(ts.Inputs.Resources, "taskspec.Inputs.Resources.Name"); err != nil {
return err
}
for _, p := range ts.Inputs.Params {
if p.Description == "" {
return apis.ErrMissingField("taskspec.Inputs.Params.Description")
}
}
}
if ts.Outputs != nil {
if err := ts.Outputs.Validate("taskspec.Outputs"); err != nil {
Expand Down
20 changes: 20 additions & 0 deletions pkg/apis/pipeline/v1alpha1/task_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func TestTaskSpec_Validate(t *testing.T) {
fields: fields{
Inputs: &Inputs{
Resources: []TaskResource{validResource},
Params: []TaskParam{
{
Name: "task",
Description: "param",
},
},
},
BuildSpec: validBuild,
},
Expand Down Expand Up @@ -134,6 +140,20 @@ func TestTaskSpec_ValidateError(t *testing.T) {
BuildSpec: validBuild,
},
},
{
name: "one invalid input param",
fields: fields{
Inputs: &Inputs{
Resources: []TaskResource{validResource},
Params: []TaskParam{
{
Name: "task",
},
},
},
BuildSpec: validBuild,
},
},
{
name: "one invalid output",
fields: fields{
Expand Down
10 changes: 5 additions & 5 deletions pkg/apis/pipeline/v1alpha1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ type TaskRunStatus struct {
func (tr *TaskRunStatus) GetCondition(t duckv1alpha1.ConditionType) *duckv1alpha1.Condition {
return taskRunCondSet.Manage(tr).GetCondition(t)
}
func (ts *TaskRunStatus) InitializeConditions() {
taskRunCondSet.Manage(ts).InitializeConditions()
func (tr *TaskRunStatus) InitializeConditions() {
taskRunCondSet.Manage(tr).InitializeConditions()
}

// SetCondition sets the condition, unsetting previous conditions with the same
// type as necessary.
func (ts *TaskRunStatus) SetCondition(newCond *duckv1alpha1.Condition) {
func (tr *TaskRunStatus) SetCondition(newCond *duckv1alpha1.Condition) {
if newCond != nil {
taskRunCondSet.Manage(ts).SetCondition(*newCond)
taskRunCondSet.Manage(tr).SetCondition(*newCond)
}
}

Expand Down Expand Up @@ -141,7 +141,7 @@ type TaskRunList struct {
Items []TaskRun `json:"items"`
}

func (t *TaskRun) SetDefaults() {}
func (tr *TaskRun) SetDefaults() {}

// GetBuildRef for task
func (tr *TaskRun) GetBuildRef() corev1.ObjectReference {
Expand Down
6 changes: 3 additions & 3 deletions pkg/apis/pipeline/v1alpha1/taskrun_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"k8s.io/apimachinery/pkg/api/equality"
)

func (t *TaskRun) Validate() *apis.FieldError {
if err := validateObjectMetadata(t.GetObjectMeta()).ViaField("metadata"); err != nil {
func (tr *TaskRun) Validate() *apis.FieldError {
if err := validateObjectMetadata(tr.GetObjectMeta()).ViaField("metadata"); err != nil {
return err
}
return t.Spec.Validate()
return tr.Spec.Validate()
}

func (ts *TaskRunSpec) Validate() *apis.FieldError {
Expand Down
2 changes: 2 additions & 0 deletions pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ func TestReconcile(t *testing.T) {
Inputs: &v1alpha1.Inputs{
Params: []v1alpha1.TaskParam{{
Name: "foo",
Description: "foo",
}, {
Name: "bar",
Description: "bar",
}},
},
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/reconciler/v1alpha1/pipelinerun/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ func Test_InvalidPipelineTask(t *testing.T) {
Inputs: &v1alpha1.Inputs{
Params: []v1alpha1.TaskParam{{
Name: "foo",
Description: "foo",
}, {
Name: "bar",
Description: "bar",
}},
},
},
Expand Down
9 changes: 8 additions & 1 deletion pkg/reconciler/v1alpha1/taskrun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ import (
)

// ApplyParameters applies the params from a TaskRun.Input.Parameters to a BuildSpec.
func ApplyParameters(b *buildv1alpha1.Build, tr *v1alpha1.TaskRun) *buildv1alpha1.Build {
func ApplyParameters(b *buildv1alpha1.Build, tr *v1alpha1.TaskRun, defaults ...v1alpha1.TaskParam) *buildv1alpha1.Build {
// This assumes that the TaskRun inputs have been validated against what the Task requests.
replacements := map[string]string{}
// Set all the default replacements
for _, p := range defaults {
if p.Default != "" {
replacements[fmt.Sprintf("inputs.params.%s", p.Name)] = p.Default
}
}
// Set and overwrite params with the ones from the TaskRun
for _, p := range tr.Spec.Inputs.Params {
replacements[fmt.Sprintf("inputs.params.%s", p.Name)] = p.Value
}
Expand Down
20 changes: 19 additions & 1 deletion pkg/reconciler/v1alpha1/taskrun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestApplyParameters(t *testing.T) {
type args struct {
b *buildv1alpha1.Build
tr *v1alpha1.TaskRun
dp []v1alpha1.TaskParam
}
tests := []struct {
name string
Expand All @@ -111,10 +112,27 @@ func TestApplyParameters(t *testing.T) {
b.Spec.Steps[0].Image = "bar"
}),
},
{
name: "with default parameter",
args: args{
b: simpleBuild,
tr: &v1alpha1.TaskRun{},
dp: []v1alpha1.TaskParam{
{
Name: "myimage",
Default: "mydefault",
},
},

},
want: applyMutation(simpleBuild, func(b *buildv1alpha1.Build) {
b.Spec.Steps[0].Image = "mydefault"
}),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ApplyParameters(tt.args.b, tt.args.tr)
got := ApplyParameters(tt.args.b, tt.args.tr, tt.args.dp...)
if d := cmp.Diff(got, tt.want); d != "" {
t.Errorf("ApplyParameters() got diff %s", d)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/reconciler/v1alpha1/taskrun/resources/input_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package resources

import (
v1alpha1 "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1"
listers "github.com/knative/build-pipeline/pkg/client/listers/pipeline/v1alpha1"
buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"
"go.uber.org/zap"
Expand Down Expand Up @@ -60,6 +60,9 @@ func AddInputResource(
break
}
}
if gitResource == nil {
return build, nil
}

gitSourceSpec := &buildv1alpha1.GitSourceSpec{
Url: gitResource.URL,
Expand Down
6 changes: 5 additions & 1 deletion pkg/reconciler/v1alpha1/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,12 @@ func (c *Reconciler) createBuild(tr *v1alpha1.TaskRun, pvcName string) (*buildv1
return nil, err
}

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

// Apply resources from the taskrun.
build, err = resources.ApplyResources(build, tr, c.resourceLister.PipelineResources(t.Namespace))
Expand Down
Loading

0 comments on commit e268e1e

Please sign in to comment.