-
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.
initial git resource for pipeline #58
- add resource to Build as an input source
- Loading branch information
1 parent
c74ad1f
commit 8b30910
Showing
3 changed files
with
257 additions
and
0 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
164 changes: 164 additions & 0 deletions
164
pkg/reconciler/v1alpha1/taskrun/resources/input_resource_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,164 @@ | ||
/* | ||
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" | ||
v1alpha1 "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1" | ||
fakeclientset "github.com/knative/build-pipeline/pkg/client/clientset/versioned/fake" | ||
informers "github.com/knative/build-pipeline/pkg/client/informers/externalversions" | ||
listers "github.com/knative/build-pipeline/pkg/client/listers/pipeline/v1alpha1" | ||
"github.com/knative/build-pipeline/pkg/logging" | ||
buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1" | ||
"go.uber.org/zap" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var ( | ||
pipelineResourceLister listers.PipelineResourceLister | ||
logger *zap.SugaredLogger | ||
) | ||
|
||
func setUp() { | ||
logger, _ = logging.NewLogger("", "") | ||
fakeClient := fakeclientset.NewSimpleClientset() | ||
sharedInfomer := informers.NewSharedInformerFactory(fakeClient, 0) | ||
pipelineResourceInformer := sharedInfomer.Pipeline().V1alpha1().PipelineResources() | ||
pipelineResourceLister = pipelineResourceInformer.Lister() | ||
|
||
res := &v1alpha1.PipelineResource{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "workspace", | ||
Namespace: "marshmallow", | ||
}, | ||
Spec: v1alpha1.PipelineResourceSpec{ | ||
Type: "git", | ||
Params: []v1alpha1.Param{ | ||
v1alpha1.Param{ | ||
Name: "Url", | ||
Value: "https://github.com/grafeas/kritis", | ||
}, | ||
v1alpha1.Param{ | ||
Name: "Revision", | ||
Value: "master", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
pipelineResourceInformer.Informer().GetIndexer().Add(res) | ||
} | ||
|
||
func TestAddResourceToBuild(t *testing.T) { | ||
boolTrue := true | ||
|
||
for _, c := range []struct { | ||
desc string | ||
task *v1alpha1.Task | ||
taskRun *v1alpha1.TaskRun | ||
build *buildv1alpha1.Build | ||
want *buildv1alpha1.Build | ||
wantErr error | ||
}{{ | ||
desc: "simple", | ||
task: &v1alpha1.Task{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "build-from-repo", | ||
Namespace: "marshmallow", | ||
}, | ||
Spec: v1alpha1.TaskSpec{ | ||
Inputs: &v1alpha1.Inputs{ | ||
Sources: []v1alpha1.Source{ | ||
v1alpha1.Source{ | ||
Name: "workspace", | ||
Type: "git", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
taskRun: &v1alpha1.TaskRun{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "build-from-repo-run", | ||
Namespace: "marshmallow", | ||
}, | ||
Spec: v1alpha1.TaskRunSpec{ | ||
TaskRef: v1alpha1.TaskRef{ | ||
Name: "simpleTask", | ||
}, | ||
}, | ||
}, | ||
build: &buildv1alpha1.Build{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Build", | ||
APIVersion: "build.knative.dev/v1alpha1"}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "build-from-repo", | ||
Namespace: "marshmallow", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "pipeline.knative.dev/v1alpha1", | ||
Kind: "TaskRun", | ||
Name: "build-from-repo-run", | ||
Controller: &boolTrue, | ||
BlockOwnerDeletion: &boolTrue, | ||
}, | ||
}, | ||
}, | ||
Spec: buildv1alpha1.BuildSpec{}, | ||
}, | ||
want: &buildv1alpha1.Build{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "Build", | ||
APIVersion: "build.knative.dev/v1alpha1"}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "build-from-repo", | ||
Namespace: "marshmallow", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "pipeline.knative.dev/v1alpha1", | ||
Kind: "TaskRun", | ||
Name: "build-from-repo-run", | ||
Controller: &boolTrue, | ||
BlockOwnerDeletion: &boolTrue, | ||
}, | ||
}, | ||
}, | ||
Spec: buildv1alpha1.BuildSpec{ | ||
Source: &buildv1alpha1.SourceSpec{ | ||
Git: &buildv1alpha1.GitSourceSpec{ | ||
Url: "https://github.com/grafeas/kritis", | ||
Revision: "master", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} { | ||
setUp() | ||
t.Run(c.desc, func(t *testing.T) { | ||
got, err := AddInputResource(c.build, c.task, c.taskRun, pipelineResourceLister, logger) | ||
if err != c.wantErr { | ||
t.Fatalf("AddInputResource: %v", err) | ||
} | ||
fmt.Printf("%+v", got) | ||
if d := cmp.Diff(got, c.want); d != "" { | ||
t.Errorf("Diff:\n%s", d) | ||
} | ||
}) | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
pkg/reconciler/v1alpha1/taskrun/resources/input_resources.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,65 @@ | ||
/* | ||
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 ( | ||
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" | ||
) | ||
|
||
// AddInputResource will update the input build with the input resource from the task | ||
func AddInputResource( | ||
build *buildv1alpha1.Build, | ||
task *v1alpha1.Task, | ||
taskRun *v1alpha1.TaskRun, | ||
pipelineResourceLister listers.PipelineResourceLister, | ||
logger *zap.SugaredLogger, | ||
) (*buildv1alpha1.Build, error) { | ||
|
||
var gitResource *v1alpha1.GitResource | ||
|
||
for _, input := range task.Spec.Inputs.Sources { | ||
resource, err := pipelineResourceLister.PipelineResources(task.Namespace).Get(input.Name) | ||
if err != nil { | ||
logger.Errorf("Failed to reconcile TaskRun: %q failed to Get Pipeline Resource: %q", task.Name, input.Name) | ||
return nil, err | ||
} | ||
if resource.Spec.Type == v1alpha1.PipelineResourceTypeGit { | ||
gitResource = v1alpha1.NewGitResource(resource) | ||
|
||
for _, trInput := range taskRun.Spec.Inputs.Resources { | ||
if trInput.ResourceRef.Name == input.Name { | ||
gitResource.Revision = trInput.Version | ||
break | ||
} | ||
} | ||
break | ||
} | ||
} | ||
|
||
gitSourceSpec := &buildv1alpha1.GitSourceSpec{ | ||
Url: gitResource.URL, | ||
Revision: gitResource.Revision, | ||
} | ||
|
||
build.Spec.Source = &buildv1alpha1.SourceSpec{Git: gitSourceSpec} | ||
build.Spec.ServiceAccountName = gitResource.ServiceAccount | ||
|
||
return build, nil | ||
} |