Skip to content

Commit

Permalink
initial git resource for pipeline #58
Browse files Browse the repository at this point in the history
 - add resource to Build as an input source
  • Loading branch information
nader-ziada committed Oct 3, 2018
1 parent c74ad1f commit 8b30910
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 0 deletions.
28 changes: 28 additions & 0 deletions pkg/apis/pipeline/v1alpha1/git_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ limitations under the License.

package v1alpha1

import (
"strings"
)

// GitResource is an endpoint from which to get data which is required
// by a Build/Task for context (e.g. a repo from which to build an image).
type GitResource struct {
Expand All @@ -30,6 +34,25 @@ type GitResource struct {
ServiceAccount string `json:"serviceAccount,omitempty"`
}

// NewGitResource create a new git resource to pass to Knativve Build
func NewGitResource(r *PipelineResource) *GitResource {
gitResource := GitResource{
Name: r.Name,
Type: r.Spec.Type,
}
for _, param := range r.Spec.Params {
switch {
case strings.EqualFold(param.Name, "URL"):
gitResource.URL = param.Value
case strings.EqualFold(param.Name, "serviceAccount"):
gitResource.ServiceAccount = param.Value
case strings.EqualFold(param.Name, "Revision"):
gitResource.Revision = param.Value
}
}
return &gitResource
}

// GetName returns the name of the resource
func (s GitResource) GetName() string {
return s.Name
Expand All @@ -52,5 +75,10 @@ func (s *GitResource) GetServiceAccountName() string {
return s.ServiceAccount
}

// GetURL returns the url to be used with this resource
func (s *GitResource) GetURL() string {
return s.URL
}

// GetParams returns the resoruce params
func (s GitResource) GetParams() []Param { return []Param{} }
164 changes: 164 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/resources/input_resource_test.go
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 pkg/reconciler/v1alpha1/taskrun/resources/input_resources.go
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
}

0 comments on commit 8b30910

Please sign in to comment.