Skip to content

Commit

Permalink
TEP-0075: Implement object var replacement on task&taskrun level
Browse files Browse the repository at this point in the history
Implement variable replacement for object's individual attributes
on task&taskrun level.

[According to TEP-0075, when providing values for strings, Task and
Pipeline authors can access individual attributes of an object param;
they cannot access the object as whole.]
  • Loading branch information
chuangw6 committed Jul 4, 2022
1 parent 38b65fa commit fb6e796
Show file tree
Hide file tree
Showing 22 changed files with 601 additions and 68 deletions.
73 changes: 73 additions & 0 deletions pkg/apis/pipeline/v1alpha1/pipeline_resource_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
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 v1alpha1

import (
resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
)

// PipelineResourceType represents the type of endpoint the pipelineResource is, so that the
// controller will know this pipelineResource should be fetched and optionally what
// additional metatdata should be provided for it.
type PipelineResourceType = resource.PipelineResourceType

const (
// PipelineResourceTypeGit indicates that this source is a Git repo.
PipelineResourceTypeGit PipelineResourceType = resource.PipelineResourceTypeGit

// PipelineResourceTypeStorage indicates that this source is a storage blob resource.
PipelineResourceTypeStorage PipelineResourceType = resource.PipelineResourceTypeStorage

// PipelineResourceTypeImage indicates that this source is a docker Image.
PipelineResourceTypeImage PipelineResourceType = resource.PipelineResourceTypeImage

// PipelineResourceTypeCluster indicates that this source is a k8s cluster Image.
PipelineResourceTypeCluster PipelineResourceType = resource.PipelineResourceTypeCluster

// PipelineResourceTypePullRequest indicates that this source is a SCM Pull Request.
PipelineResourceTypePullRequest PipelineResourceType = resource.PipelineResourceTypePullRequest

// PipelineResourceTypeCloudEvent indicates that this source is a cloud event URI
PipelineResourceTypeCloudEvent PipelineResourceType = resource.PipelineResourceTypeCloudEvent
)

// AllResourceTypes can be used for validation to check if a provided Resource type is one of the known types.
var AllResourceTypes = resource.AllResourceTypes

// PipelineResource describes a resource that is an input to or output from a
// Task.
type PipelineResource = resource.PipelineResource

// PipelineResourceSpec defines an individual resources used in the pipeline.
type PipelineResourceSpec = resource.PipelineResourceSpec

// SecretParam indicates which secret can be used to populate a field of the resource
type SecretParam = resource.SecretParam

// ResourceParam declares a string value to use for the parameter called Name, and is used in
// the specific context of PipelineResources.
type ResourceParam = resource.ResourceParam

// ResourceDeclaration defines an input or output PipelineResource declared as a requirement
// by another type such as a Task or Condition. The Name field will be used to refer to these
// PipelineResources within the type's definition, and when provided as an Input, the Name will be the
// path to the volume mounted containing this PipelineResource as an input (e.g.
// an input Resource named `workspace` will be mounted at `/workspace`).
type ResourceDeclaration = resource.ResourceDeclaration

// PipelineResourceList contains a list of PipelineResources
type PipelineResourceList = resource.PipelineResourceList
174 changes: 174 additions & 0 deletions pkg/apis/pipeline/v1alpha1/taskrun_validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
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 v1alpha1

import (
"context"
"fmt"
"strings"

"github.com/tektoncd/pipeline/pkg/apis/validate"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/util/sets"
"knative.dev/pkg/apis"
)

var _ apis.Validatable = (*TaskRun)(nil)

// Validate taskrun
func (tr *TaskRun) Validate(ctx context.Context) *apis.FieldError {
if err := validate.ObjectMetadata(tr.GetObjectMeta()).ViaField("metadata"); err != nil {
return err
}
if apis.IsInDelete(ctx) {
return nil
}
return tr.Spec.Validate(ctx)
}

// Validate taskrun spec
func (ts *TaskRunSpec) Validate(ctx context.Context) *apis.FieldError {
if equality.Semantic.DeepEqual(ts, &TaskRunSpec{}) {
return apis.ErrMissingField("spec")
}

// can't have both taskRef and taskSpec at the same time
if (ts.TaskRef != nil && ts.TaskRef.Name != "") && ts.TaskSpec != nil {
return apis.ErrDisallowedFields("spec.taskref", "spec.taskspec")
}

// Check that one of TaskRef and TaskSpec is present
if (ts.TaskRef == nil || (ts.TaskRef != nil && ts.TaskRef.Name == "")) && ts.TaskSpec == nil {
return apis.ErrMissingField("spec.taskref.name", "spec.taskspec")
}

// Validate TaskSpec if it's present
if ts.TaskSpec != nil {
if err := ts.TaskSpec.Validate(ctx); err != nil {
return err
}
}

// Deprecated
// check for input resources
if ts.Inputs != nil {
if err := ts.Inputs.Validate(ctx, "spec.Inputs"); err != nil {
return err
}
}

// Deprecated
// check for output resources
if ts.Outputs != nil {
if err := ts.Outputs.Validate(ctx, "spec.Outputs"); err != nil {
return err
}
}

// Validate Resources
if err := ts.Resources.Validate(ctx); err != nil {
return err
}

if err := validateWorkspaceBindings(ctx, ts.Workspaces); err != nil {
return err
}
if err := validateParameters("spec.inputs.params", ts.Params); err != nil {
return err
}

if ts.Timeout != nil {
// timeout should be a valid duration of at least 0.
if ts.Timeout.Duration < 0 {
return apis.ErrInvalidValue(fmt.Sprintf("%s should be >= 0", ts.Timeout.Duration.String()), "spec.timeout")
}
}

return nil
}

// Validate implements apis.Validatable
func (i TaskRunInputs) Validate(ctx context.Context, path string) *apis.FieldError {
if err := validatePipelineResources(ctx, i.Resources, fmt.Sprintf("%s.Resources.Name", path)); err != nil {
return err
}
return validateParameters("spec.inputs.params", i.Params)
}

// Validate implements apis.Validatable
func (o TaskRunOutputs) Validate(ctx context.Context, path string) *apis.FieldError {
return validatePipelineResources(ctx, o.Resources, fmt.Sprintf("%s.Resources.Name", path))
}

// validateWorkspaceBindings makes sure the volumes provided for the Task's declared workspaces make sense.
func validateWorkspaceBindings(ctx context.Context, wb []WorkspaceBinding) *apis.FieldError {
seen := sets.NewString()
for _, w := range wb {
if seen.Has(w.Name) {
return apis.ErrMultipleOneOf("spec.workspaces.name")
}
seen.Insert(w.Name)

if err := w.Validate(ctx).ViaField("workspace"); err != nil {
return err
}
}

return nil
}

// validatePipelineResources validates that
// 1. resource is not declared more than once
// 2. if both resource reference and resource spec is defined at the same time
// 3. at least resource ref or resource spec is defined
func validatePipelineResources(ctx context.Context, resources []TaskResourceBinding, path string) *apis.FieldError {
encountered := sets.NewString()
for _, r := range resources {
// We should provide only one binding for each resource required by the Task.
name := strings.ToLower(r.Name)
if encountered.Has(strings.ToLower(name)) {
return apis.ErrMultipleOneOf(path)
}
encountered.Insert(name)
// Check that both resource ref and resource Spec are not present
if r.ResourceRef != nil && r.ResourceSpec != nil {
return apis.ErrDisallowedFields(fmt.Sprintf("%s.ResourceRef", path), fmt.Sprintf("%s.ResourceSpec", path))
}
// Check that one of resource ref and resource Spec is present
if (r.ResourceRef == nil || r.ResourceRef.Name == "") && r.ResourceSpec == nil {
return apis.ErrMissingField(fmt.Sprintf("%s.ResourceRef", path), fmt.Sprintf("%s.ResourceSpec", path))
}
if r.ResourceSpec != nil && r.ResourceSpec.Validate(ctx) != nil {
return r.ResourceSpec.Validate(ctx)
}
}

return nil
}

// TODO(jasonhall): Share this with v1beta1/taskrun_validation.go
func validateParameters(path string, params []Param) *apis.FieldError {
// Template must not duplicate parameter names.
seen := sets.NewString()
for _, p := range params {
if seen.Has(strings.ToLower(p.Name)) {
return apis.ErrMultipleOneOf(path)
}
seen.Insert(p.Name)
}
return nil
}
6 changes: 3 additions & 3 deletions pkg/apis/pipeline/v1beta1/resource_types_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func (tr *TaskRunResources) Validate(ctx context.Context) *apis.FieldError {
}

// validateTaskRunResources validates that
// 1. resource is not declared more than once
// 2. if both resource reference and resource spec is defined at the same time
// 3. at least resource ref or resource spec is defined
// 1. resource is not declared more than once
// 2. if both resource reference and resource spec is defined at the same time
// 3. at least resource ref or resource spec is defined
func validateTaskRunResources(ctx context.Context, resources []TaskResourceBinding, path string) *apis.FieldError {
encountered := sets.NewString()
for _, r := range resources {
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/taskrun_defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand Down
14 changes: 7 additions & 7 deletions pkg/client/clientset/versioned/fake/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions pkg/client/clientset/versioned/scheme/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions pkg/client/resource/clientset/versioned/fake/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions pkg/client/resource/clientset/versioned/scheme/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions pkg/controller/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (
// For example, a controller impl that wants to be notified of updates to Runs
// which reference a Task with apiVersion "example.dev/v0" and kind "Example":
//
// runinformer.Get(ctx).Informer().AddEventHandler(cache.FilteringResourceEventHandler{
// FilterFunc: FilterRunRef("example.dev/v0", "Example"),
// Handler: controller.HandleAll(impl.Enqueue),
// })
// runinformer.Get(ctx).Informer().AddEventHandler(cache.FilteringResourceEventHandler{
// FilterFunc: FilterRunRef("example.dev/v0", "Example"),
// Handler: controller.HandleAll(impl.Enqueue),
// })
func FilterRunRef(apiVersion, kind string) func(interface{}) bool {
return func(obj interface{}) bool {
r, ok := obj.(*v1alpha1.Run)
Expand Down Expand Up @@ -66,10 +66,10 @@ func FilterRunRef(apiVersion, kind string) func(interface{}) bool {
// For example, a controller impl that wants to be notified of updates to TaskRuns that are controlled by
// a Run which references a custom task with apiVersion "example.dev/v0" and kind "Example":
//
// taskruninformer.Get(ctx).Informer().AddEventHandler(cache.FilteringResourceEventHandler{
// FilterFunc: FilterOwnerRunRef("example.dev/v0", "Example"),
// Handler: controller.HandleAll(impl.Enqueue),
// })
// taskruninformer.Get(ctx).Informer().AddEventHandler(cache.FilteringResourceEventHandler{
// FilterFunc: FilterOwnerRunRef("example.dev/v0", "Example"),
// Handler: controller.HandleAll(impl.Enqueue),
// })
func FilterOwnerRunRef(runLister listersalpha.RunLister, apiVersion, kind string) func(interface{}) bool {
return func(obj interface{}) bool {
object, ok := obj.(metav1.Object)
Expand Down
2 changes: 1 addition & 1 deletion pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ 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
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,
Expand Down
Loading

0 comments on commit fb6e796

Please sign in to comment.