-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Separate logic to follow references from TaskRun resource validation #261
Merged
knative-prow-robot
merged 2 commits into
tektoncd:master
from
bobcatfish:refactor_resource_validation
Nov 26, 2018
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
72 changes: 72 additions & 0 deletions
72
pkg/reconciler/v1alpha1/taskrun/resources/taskrunresolution.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,72 @@ | ||
/* | ||
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 extress 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" | ||
) | ||
|
||
// ResolvedTaskRun contains all the data that is needed to execute | ||
// the TaskRun: the TaskRun, it's Task and the PipelineResources it needs. | ||
type ResolvedTaskRun struct { | ||
Task *v1alpha1.Task | ||
// Inputs is a map from the name of the input required by the Task | ||
// to the actual Resource to use for it | ||
Inputs map[string]*v1alpha1.PipelineResource | ||
// Outputs is a map from the name of the output required by the Task | ||
// to the actual Resource to use for it | ||
Outputs map[string]*v1alpha1.PipelineResource | ||
} | ||
|
||
// GetResource is a function used to retrieve PipelineResources. | ||
type GetResource func(string) (*v1alpha1.PipelineResource, error) | ||
|
||
// GetTask is a function used to retrieve Tasks. | ||
type GetTask func(string) (*v1alpha1.Task, error) | ||
|
||
// ResolveTaskRun looks up CRDs referenced by the TaskRun and returns an instance | ||
// of TaskRunResource with all of the relevant data populated. If referenced CRDs can't | ||
// be found, an error is returned. | ||
func ResolveTaskRun(tr *v1alpha1.TaskRunSpec, getTask GetTask, gr GetResource) (*ResolvedTaskRun, error) { | ||
var err error | ||
rtr := ResolvedTaskRun{ | ||
Inputs: map[string]*v1alpha1.PipelineResource{}, | ||
Outputs: map[string]*v1alpha1.PipelineResource{}, | ||
} | ||
|
||
rtr.Task, err = getTask(tr.TaskRef.Name) | ||
if err != nil { | ||
return nil, fmt.Errorf("couldn't retrieve referenced Task %q: %s", tr.TaskRef.Name, err) | ||
} | ||
for _, r := range tr.Inputs.Resources { | ||
rr, err := gr(r.ResourceRef.Name) | ||
if err != nil { | ||
return nil, fmt.Errorf("couldn't retreive referenced input PipelineResource %q: %s", r.ResourceRef.Name, err) | ||
} | ||
rtr.Inputs[r.Name] = rr | ||
} | ||
for _, r := range tr.Outputs.Resources { | ||
rr, err := gr(r.ResourceRef.Name) | ||
if err != nil { | ||
return nil, fmt.Errorf("couldn't retreive referenced output PipelineResource %q: %s", r.ResourceRef.Name, err) | ||
} | ||
rtr.Outputs[r.Name] = rr | ||
} | ||
return &rtr, nil | ||
} |
234 changes: 234 additions & 0 deletions
234
pkg/reconciler/v1alpha1/taskrun/resources/taskrunresolution_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,234 @@ | ||
/* | ||
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 extress or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package resources | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestResolveTaskRun(t *testing.T) { | ||
tr := &v1alpha1.TaskRunSpec{ | ||
TaskRef: v1alpha1.TaskRef{ | ||
Name: "orchestrate", | ||
}, | ||
Inputs: v1alpha1.TaskRunInputs{ | ||
Resources: []v1alpha1.TaskRunResource{{ | ||
Name: "repoToBuildFrom", | ||
ResourceRef: v1alpha1.PipelineResourceRef{ | ||
Name: "git-repo", | ||
}, | ||
}, { | ||
Name: "clusterToUse", | ||
ResourceRef: v1alpha1.PipelineResourceRef{ | ||
Name: "k8s-cluster", | ||
}, | ||
}}, | ||
}, | ||
Outputs: v1alpha1.TaskRunOutputs{ | ||
Resources: []v1alpha1.TaskRunResource{{ | ||
Name: "imageToBuild", | ||
ResourceRef: v1alpha1.PipelineResourceRef{ | ||
Name: "image", | ||
}, | ||
}, { | ||
Name: "gitRepoToUpdate", | ||
ResourceRef: v1alpha1.PipelineResourceRef{ | ||
Name: "another-git-repo", | ||
}, | ||
}}, | ||
}, | ||
} | ||
|
||
task := &v1alpha1.Task{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "orchestrate", | ||
}, | ||
} | ||
gt := func(n string) (*v1alpha1.Task, error) { return task, nil } | ||
|
||
resources := []*v1alpha1.PipelineResource{{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "git-repo", | ||
}, | ||
}, { | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "k8s-cluster", | ||
}, | ||
}, { | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "image", | ||
}, | ||
}, { | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "another-git-repo", | ||
}, | ||
}} | ||
resourceIndex := 0 | ||
gr := func(n string) (*v1alpha1.PipelineResource, error) { | ||
r := resources[resourceIndex] | ||
resourceIndex++ | ||
return r, nil | ||
} | ||
|
||
rtr, err := ResolveTaskRun(tr, gt, gr) | ||
if err != nil { | ||
t.Fatalf("Did not expect error trying to resolve TaskRun: %s", err) | ||
} | ||
|
||
if rtr.Task == nil || rtr.Task.Name != "orchestrate" { | ||
t.Errorf("Task not resolved, expected `orchestrate` Task but got: %v", rtr.Task) | ||
} | ||
|
||
if len(rtr.Inputs) == 2 { | ||
r, ok := rtr.Inputs["repoToBuildFrom"] | ||
if !ok { | ||
t.Errorf("Expected value present in map for `repoToBuildFrom' but it was missing") | ||
} else { | ||
if r.Name != "git-repo" { | ||
t.Errorf("Expected to use resource `git-repo` for `repoToBuildFrom` but used %s", r.Name) | ||
} | ||
} | ||
r, ok = rtr.Inputs["clusterToUse"] | ||
if !ok { | ||
t.Errorf("Expected value present in map for `clusterToUse' but it was missing") | ||
} else { | ||
if r.Name != "k8s-cluster" { | ||
t.Errorf("Expected to use resource `k8s-cluster` for `clusterToUse` but used %s", r.Name) | ||
} | ||
} | ||
} else { | ||
t.Errorf("Expected 2 resolved inputs but instead had: %v", rtr.Inputs) | ||
} | ||
|
||
if len(rtr.Outputs) == 2 { | ||
r, ok := rtr.Outputs["imageToBuild"] | ||
if !ok { | ||
t.Errorf("Expected value present in map for `imageToBuild' but it was missing") | ||
} else { | ||
if r.Name != "image" { | ||
t.Errorf("Expected to use resource `image` for `imageToBuild` but used %s", r.Name) | ||
} | ||
} | ||
r, ok = rtr.Outputs["gitRepoToUpdate"] | ||
if !ok { | ||
t.Errorf("Expected value present in map for `gitRepoToUpdate' but it was missing") | ||
} else { | ||
if r.Name != "another-git-repo" { | ||
t.Errorf("Expected to use resource `another-git-repo` for `gitRepoToUpdate` but used %s", r.Name) | ||
} | ||
} | ||
} else { | ||
t.Errorf("Expected 2 resolved outputs but instead had: %v", rtr.Outputs) | ||
} | ||
} | ||
func TestResolveTaskRun_missingTask(t *testing.T) { | ||
tr := &v1alpha1.TaskRunSpec{ | ||
TaskRef: v1alpha1.TaskRef{ | ||
Name: "orchestrate", | ||
}, | ||
} | ||
|
||
gt := func(n string) (*v1alpha1.Task, error) { return nil, fmt.Errorf("nope") } | ||
gr := func(n string) (*v1alpha1.PipelineResource, error) { return &v1alpha1.PipelineResource{}, nil } | ||
|
||
_, err := ResolveTaskRun(tr, gt, gr) | ||
if err == nil { | ||
t.Fatalf("Expected to get error because task couldn't be resolved") | ||
} | ||
} | ||
func TestResolveTaskRun_missingOutput(t *testing.T) { | ||
tr := &v1alpha1.TaskRunSpec{ | ||
TaskRef: v1alpha1.TaskRef{ | ||
Name: "orchestrate", | ||
}, | ||
Outputs: v1alpha1.TaskRunOutputs{ | ||
Resources: []v1alpha1.TaskRunResource{{ | ||
Name: "repoToUpdate", | ||
ResourceRef: v1alpha1.PipelineResourceRef{ | ||
Name: "another-git-repo", | ||
}, | ||
}, | ||
}, | ||
}} | ||
|
||
gt := func(n string) (*v1alpha1.Task, error) { return &v1alpha1.Task{}, nil } | ||
gr := func(n string) (*v1alpha1.PipelineResource, error) { return nil, fmt.Errorf("nope") } | ||
|
||
_, err := ResolveTaskRun(tr, gt, gr) | ||
if err == nil { | ||
t.Fatalf("Expected to get error because output resource couldn't be resolved") | ||
} | ||
} | ||
|
||
func TestResolveTaskRun_missingInput(t *testing.T) { | ||
tr := &v1alpha1.TaskRunSpec{ | ||
TaskRef: v1alpha1.TaskRef{ | ||
Name: "orchestrate", | ||
}, | ||
Inputs: v1alpha1.TaskRunInputs{ | ||
Resources: []v1alpha1.TaskRunResource{{ | ||
Name: "repoToBuildFrom", | ||
ResourceRef: v1alpha1.PipelineResourceRef{ | ||
Name: "git-repo", | ||
}, | ||
}, | ||
}, | ||
}} | ||
|
||
gt := func(n string) (*v1alpha1.Task, error) { return &v1alpha1.Task{}, nil } | ||
gr := func(n string) (*v1alpha1.PipelineResource, error) { return nil, fmt.Errorf("nope") } | ||
|
||
_, err := ResolveTaskRun(tr, gt, gr) | ||
if err == nil { | ||
t.Fatalf("Expected to get error because input resource couldn't be resolved") | ||
} | ||
} | ||
|
||
func TestResolveTaskRun_noResources(t *testing.T) { | ||
tr := &v1alpha1.TaskRunSpec{ | ||
TaskRef: v1alpha1.TaskRef{ | ||
Name: "orchestrate", | ||
}, | ||
} | ||
task := &v1alpha1.Task{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "orchestrate", | ||
}, | ||
} | ||
gt := func(n string) (*v1alpha1.Task, error) { return task, nil } | ||
gr := func(n string) (*v1alpha1.PipelineResource, error) { return &v1alpha1.PipelineResource{}, nil } | ||
|
||
rtr, err := ResolveTaskRun(tr, gt, gr) | ||
if err != nil { | ||
t.Fatalf("Did not expect error trying to resolve TaskRun: %s", err) | ||
} | ||
|
||
if rtr.Task == nil || rtr.Task.Name != "orchestrate" { | ||
t.Errorf("Task not resolved, expected `orchestrate` Task but got: %v", rtr.Task) | ||
} | ||
|
||
if len(rtr.Inputs) != 0 { | ||
t.Errorf("Did not expect any outputs to be resolved when none specified but had %v", rtr.Inputs) | ||
} | ||
if len(rtr.Outputs) != 0 { | ||
t.Errorf("Did not expect any outputs to be resolved when none specified but had %v", rtr.Outputs) | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🕺