-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Move logic to a new file in resources pkg. - Switch to using a Getter from a Lister - Add separate tests for params and resources
- Loading branch information
1 parent
170e628
commit 3b03577
Showing
5 changed files
with
308 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
201
pkg/reconciler/v1alpha1/taskrun/resources/apply_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
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 { | ||
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters