Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement template variable expansion for Resources. #153

Merged
merged 2 commits into from
Oct 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/pipeline-resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,21 @@ spec:
- name: builtImage
type: image
```

#### Templating

Git Resources (like all Resources) support template expansion into BuildSpecs.
Git Resources support the following keys for replacement:

* name
* url
* type
* revision

These can be referenced in a TaskRun spec like:

```shell
${inputs.resources.NAME.KEY}
```

where NAME is the Resource Name and KEY is the key from the above list.
dlorenc marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 10 additions & 0 deletions pkg/apis/pipeline/v1alpha1/git_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,13 @@ func (s *GitResource) GetURL() string {

// GetParams returns the resoruce params
func (s GitResource) GetParams() []Param { return []Param{} }

// Replacements is used for template replacement on a GitResource inside of a Taskrun.
func (s *GitResource) Replacements() map[string]string {
return map[string]string{
"name": s.Name,
"type": string(s.Type),
"url": s.URL,
"revision": s.Revision,
}
}
37 changes: 37 additions & 0 deletions pkg/apis/pipeline/v1alpha1/image_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,33 @@ limitations under the License.

package v1alpha1

import (
"fmt"
"strings"
)

// NewImageResource creates a new ImageResource from a PipelineResource.
func NewImageResource(r *PipelineResource) (*ImageResource, error) {
if r.Spec.Type != PipelineResourceTypeImage {
return nil, fmt.Errorf("ImageResource: Cannot create an Image resource from a %s Pipeline Resource", r.Spec.Type)
}
ir := &ImageResource{
Name: r.Name,
Type: PipelineResourceTypeImage,
}

for _, param := range r.Spec.Params {
switch {
case strings.EqualFold(param.Name, "URL"):
ir.URL = param.Value
case strings.EqualFold(param.Name, "Digest"):
ir.Digest = param.Value
}
}

return ir, nil
}

// ImageResource defines an endpoint where artifacts can be stored, such as images.
type ImageResource struct {
Name string `json:"name"`
Expand All @@ -41,3 +68,13 @@ func (s ImageResource) GetVersion() string {

// GetParams returns the resoruce params
func (s ImageResource) GetParams() []Param { return []Param{} }

// Replacements is used for template replacement on an ImageResource inside of a Taskrun.
func (s *ImageResource) Replacements() map[string]string {
return map[string]string{
"name": s.Name,
"type": string(s.Type),
"url": s.URL,
"digest": s.Digest,
}
}
14 changes: 14 additions & 0 deletions pkg/apis/pipeline/v1alpha1/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1alpha1

import (
"fmt"

"github.com/knative/pkg/apis"
"github.com/knative/pkg/webhook"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -46,6 +48,7 @@ type PipelineResourceInterface interface {
GetType() PipelineResourceType
GetParams() []Param
GetVersion() string
Replacements() map[string]string
}

// PipelineResourceSpec defines an individual resources used in the pipeline.
Expand Down Expand Up @@ -112,3 +115,14 @@ type PipelineResourceList struct {
metav1.ListMeta `json:"metadata,omitempty"`
Items []PipelineResource `json:"items"`
}

// ResourceFromType returns a PipelineResourceInterface from a PipelineResource's type.
func ResourceFromType(r *PipelineResource) (PipelineResourceInterface, error) {
switch r.Spec.Type {
case PipelineResourceTypeGit:
return NewGitResource(r)
case PipelineResourceTypeImage:
return NewImageResource(r)
}
return nil, fmt.Errorf("%s is an invalid or unimplemented PipelineResource", r.Spec.Type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol okay

}
61 changes: 61 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/resources/apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2018 The Knative 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 (
"fmt"

"github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1"
buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"
"github.com/knative/build/pkg/builder"
)

// ApplyParameters applies the params from a TaskRun.Input.Parameters to a BuildSpec.
func ApplyParameters(b *buildv1alpha1.Build, tr *v1alpha1.TaskRun) *buildv1alpha1.Build {
// This assumes that the TaskRun inputs have been validated against what the Task requests.
replacements := map[string]string{}
for _, p := range tr.Spec.Inputs.Params {
replacements[fmt.Sprintf("inputs.params.%s", p.Name)] = p.Value
}

return builder.ApplyReplacements(b, replacements)
}

type ResourceGetter interface {
Get(string) (*v1alpha1.PipelineResource, error)
}

// ApplyResources applies the params from a TaskRun.Input.Resources to a BuildSpec.
func ApplyResources(b *buildv1alpha1.Build, tr *v1alpha1.TaskRun, getter ResourceGetter) (*buildv1alpha1.Build, error) {
replacements := map[string]string{}

for _, ir := range tr.Spec.Inputs.Resources {
pr, err := getter.Get(ir.ResourceRef.Name)
if err != nil {
return nil, err
}

resource, err := v1alpha1.ResourceFromType(pr)
if err != nil {
return nil, err
}
for k, v := range resource.Replacements() {
replacements[fmt.Sprintf("inputs.resources.%s.%s", ir.ResourceRef.Name, k)] = v
}
}
return builder.ApplyReplacements(b, replacements), nil
}
201 changes: 201 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/resources/apply_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
Copyright 2018 The Knative 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 (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1"
buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var simpleBuild = &buildv1alpha1.Build{
Spec: buildv1alpha1.BuildSpec{
Steps: []corev1.Container{
{
Name: "foo",
Image: "${inputs.params.myimage}",
},
{
Name: "baz",
Image: "bat",
Args: []string{"${inputs.resources.git-resource.url}"},
},
},
},
}

var paramTaskRun = &v1alpha1.TaskRun{
Spec: v1alpha1.TaskRunSpec{
Inputs: v1alpha1.TaskRunInputs{
Params: []v1alpha1.Param{
{
Name: "myimage",
Value: "bar",
},
},
},
},
}

var resourceTaskRun = &v1alpha1.TaskRun{
Spec: v1alpha1.TaskRunSpec{
Inputs: v1alpha1.TaskRunInputs{
Resources: []v1alpha1.PipelineResourceVersion{
{
ResourceRef: v1alpha1.PipelineResourceRef{
Name: "git-resource",
},
},
},
},
},
}

var gitResource = &v1alpha1.PipelineResource{
ObjectMeta: metav1.ObjectMeta{
Name: "git-resource",
},
Spec: v1alpha1.PipelineResourceSpec{
Type: v1alpha1.PipelineResourceTypeGit,
Params: []v1alpha1.Param{
{
Name: "URL",
Value: "https://git-repo",
},
},
},
}

func applyMutation(b *buildv1alpha1.Build, f func(b *buildv1alpha1.Build)) *buildv1alpha1.Build {
b = b.DeepCopy()
f(b)
return b
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoa 😎


func TestApplyParameters(t *testing.T) {
type args struct {
b *buildv1alpha1.Build
tr *v1alpha1.TaskRun
}
tests := []struct {
name string
args args
want *buildv1alpha1.Build
}{
{
name: "single parameter",
args: args{
b: simpleBuild,
tr: paramTaskRun,
},
want: applyMutation(simpleBuild, func(b *buildv1alpha1.Build) {
b.Spec.Steps[0].Image = "bar"
}),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ApplyParameters(tt.args.b, tt.args.tr)
if d := cmp.Diff(got, tt.want); d != "" {
t.Errorf("ApplyParameters() got diff %s", d)
}
})
}
}

type rg struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the g in rg?

resources map[string]*v1alpha1.PipelineResource
}

func (rg *rg) Get(name string) (*v1alpha1.PipelineResource, error) {
if pr, ok := rg.resources[name]; ok {
return pr, nil
}
return nil, fmt.Errorf("Resource %s does not exist.", name)
}

func (rg *rg) With(name string, pr *v1alpha1.PipelineResource) *rg {
rg.resources[name] = pr
return rg
}

func mockGetter() *rg {
return &rg{
resources: map[string]*v1alpha1.PipelineResource{},
}
}

func TestApplyResources(t *testing.T) {
type args struct {
b *buildv1alpha1.Build
tr *v1alpha1.TaskRun
getter ResourceGetter
}
tests := []struct {
name string
args args
want *buildv1alpha1.Build
wantErr bool
}{
{
name: "no replacements specified",
args: args{
b: simpleBuild,
tr: paramTaskRun,
getter: mockGetter(),
},
want: simpleBuild,
},
{
name: "resource specified",
args: args{
b: simpleBuild,
tr: resourceTaskRun,
getter: mockGetter().With("git-resource", gitResource),
},
want: applyMutation(simpleBuild, func(b *buildv1alpha1.Build) {
b.Spec.Steps[1].Args = []string{"https://git-repo"}
}),
},
{
name: "resource does not exist",
args: args{
b: simpleBuild,
tr: resourceTaskRun,
getter: mockGetter(),
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ApplyResources(tt.args.b, tt.args.tr, tt.args.getter)
if (err != nil) != tt.wantErr {
t.Errorf("ApplyResources() error = %v, wantErr %v", err, tt.wantErr)
return
}
if d := cmp.Diff(got, tt.want); d != "" {
t.Errorf("ApplyResources() diff %s", d)
}
})
}
}
Loading