Skip to content
This repository has been archived by the owner on Sep 22, 2020. It is now read-only.

Commit

Permalink
Issue 40: Add functions to generate trigger templates and bindings (#15)
Browse files Browse the repository at this point in the history
* Refactor the bindings functions in triggers package

- Using v1alpha1 package instead of v1alpha2
- Renamed createTypeMeta() to createTriggerBindingMeta()
- Added test case for createBindingParam()
- Using Fatalf() instead of Errorf()

* template

* Barebones of test working.

* templates-test

* created go file to generate Templates

* Add generated trigger templates and bindings to outputs slice

Co-Authored-By: Dhanashri Palodkar <dpalodka@redhat.com>

* Merge templates and bindings functions into a single package

Co-Authored-By: Dhanashri Palodkar <dpalodka@redhat.com>

* Add namespaces to TriggerBindings TriggerTemplates.

Co-authored-by: Kevin McDermott <bigkevmcd@gmail.com>
Co-authored-by: Dhanashri Palodkar <dpalodkar14@gmail.com>
Co-authored-by: William Tam <wtam@redhat.com>
  • Loading branch information
4 people committed Feb 25, 2020
1 parent ae77faa commit eee2b6b
Show file tree
Hide file tree
Showing 7 changed files with 773 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/pipelines/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/openshift/odo/pkg/pipelines/meta"
"github.com/openshift/odo/pkg/pipelines/routes"
"github.com/openshift/odo/pkg/pipelines/tasks"
"github.com/openshift/odo/pkg/pipelines/triggers"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -85,6 +86,18 @@ func Bootstrap(o *BootstrapOptions) error {
outputs = append(outputs, task)
}

// Create trigger templates
templates := triggers.GenerateTemplates(namespaces["cicd"])
for _, template := range templates {
outputs = append(outputs, template)
}

// Create trigger bindings
bindings := triggers.GenerateBindings(namespaces["cicd"])
for _, binding := range bindings {
outputs = append(outputs, binding)
}

// Create Pipelines
outputs = append(outputs, createDevCIPipeline(meta.NamespacedName(namespaces["cicd"], "dev-ci-pipeline")))
outputs = append(outputs, createStageCIPipeline(meta.NamespacedName(namespaces["cicd"], "stage-ci-pipeline"), namespaces["stage"]))
Expand Down
95 changes: 95 additions & 0 deletions pkg/pipelines/triggers/bindings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package triggers

import (
"github.com/openshift/odo/pkg/pipelines/meta"
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var (
triggerBindingTypeMeta = v1.TypeMeta{
Kind: "TriggerBinding",
APIVersion: "tekton.dev/v1alpha1",
}
)

// GenerateBindings returns a slice of trigger bindings
func GenerateBindings(ns string) []triggersv1.TriggerBinding {
return []triggersv1.TriggerBinding{
createDevCDDeployBinding(ns),
createDevCIBuildBinding(ns),
createStageCDDeployBinding(ns),
createStageCIDryRunBinding(ns),
}
}

func createDevCDDeployBinding(ns string) triggersv1.TriggerBinding {
return triggersv1.TriggerBinding{
TypeMeta: triggerBindingTypeMeta,
ObjectMeta: meta.CreateObjectMeta(ns, "dev-cd-deploy-from-master-binding"),
Spec: triggersv1.TriggerBindingSpec{
Params: []pipelinev1.Param{
createBindingParam("gitref", "$(body.head_commit.id)"),
createBindingParam("gitrepositoryurl", "$(body.repository.clone_url)"),
},
},
}
}

func createDevCIBuildBinding(ns string) triggersv1.TriggerBinding {
return triggersv1.TriggerBinding{
TypeMeta: triggerBindingTypeMeta,
ObjectMeta: meta.CreateObjectMeta(ns, "dev-ci-build-from-pr-binding"),
Spec: triggersv1.TriggerBindingSpec{
Params: []pipelinev1.Param{
createBindingParam("gitref", "$(body.pull_request.head.ref)"),
createBindingParam("gitsha", "$(body.pull_request.head.sha)"),
createBindingParam("gitrepositoryurl", "$(body.repository.clone_url)"),
createBindingParam("fullname", "$(body.repository.full_name)"),
},
},
}
}

func createStageCDDeployBinding(ns string) triggersv1.TriggerBinding {
return triggersv1.TriggerBinding{
TypeMeta: triggerBindingTypeMeta,
ObjectMeta: meta.CreateObjectMeta(ns, "stage-cd-deploy-from-push-binding"),
Spec: triggersv1.TriggerBindingSpec{
Params: []pipelinev1.Param{
createBindingParam("gitref", "$(body.ref)"),
createBindingParam("gitsha", "$(body.commits.0.id)"),
createBindingParam("gitrepositoryurl", "$(body.repository.clone_url)"),
},
},
}
}

func createStageCIDryRunBinding(ns string) triggersv1.TriggerBinding {
return triggersv1.TriggerBinding{
TypeMeta: triggerBindingTypeMeta,
ObjectMeta: meta.CreateObjectMeta(ns, "stage-ci-dryrun-from-pr-binding"),
Spec: triggersv1.TriggerBindingSpec{
Params: []pipelinev1.Param{
createBindingParam("gitref", "$(body.pull_request.head.ref)"),
createBindingParam("gitrepositoryurl", "$(body.repository.clone_url)"),
},
},
}
}

func createObjectMeta(name string) v1.ObjectMeta {
return v1.ObjectMeta{
Name: name,
}
}
func createBindingParam(name string, value string) pipelinev1.Param {
return pipelinev1.Param{
Name: name,
Value: pipelinev1.ArrayOrString{
StringVal: value,
Type: pipelinev1.ParamTypeString,
},
}
}
185 changes: 185 additions & 0 deletions pkg/pipelines/triggers/bindings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package triggers

import (
"testing"

"github.com/google/go-cmp/cmp"
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
triggersv1 "github.com/tektoncd/triggers/pkg/apis/triggers/v1alpha1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestCreateDevCDDeployBinding(t *testing.T) {
validDevCDBinding := triggersv1.TriggerBinding{
TypeMeta: v1.TypeMeta{
Kind: "TriggerBinding",
APIVersion: "tekton.dev/v1alpha1",
},
ObjectMeta: v1.ObjectMeta{
Name: "dev-cd-deploy-from-master-binding",
Namespace: "testns",
},
Spec: triggersv1.TriggerBindingSpec{
Params: []pipelinev1.Param{
pipelinev1.Param{
Name: "gitref",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.head_commit.id)",
Type: pipelinev1.ParamTypeString,
},
},
pipelinev1.Param{
Name: "gitrepositoryurl",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.repository.clone_url)",
Type: pipelinev1.ParamTypeString,
},
},
},
},
}
binding := createDevCDDeployBinding("testns")
if diff := cmp.Diff(validDevCDBinding, binding); diff != "" {
t.Fatalf("createDevCDDeployBinding() failed:\n%s", diff)
}
}

func TestCreateDevCIBuildBinding(t *testing.T) {
validDevCIBinding := triggersv1.TriggerBinding{
TypeMeta: v1.TypeMeta{
Kind: "TriggerBinding",
APIVersion: "tekton.dev/v1alpha1",
},
ObjectMeta: v1.ObjectMeta{
Name: "dev-ci-build-from-pr-binding",
Namespace: "testns",
},
Spec: triggersv1.TriggerBindingSpec{
Params: []pipelinev1.Param{
pipelinev1.Param{
Name: "gitref",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.pull_request.head.ref)",
Type: pipelinev1.ParamTypeString,
},
},
pipelinev1.Param{
Name: "gitsha",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.pull_request.head.sha)",
Type: pipelinev1.ParamTypeString,
},
},
pipelinev1.Param{
Name: "gitrepositoryurl",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.repository.clone_url)",
Type: pipelinev1.ParamTypeString,
},
},
pipelinev1.Param{
Name: "fullname",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.repository.full_name)",
Type: pipelinev1.ParamTypeString,
},
},
},
},
}
binding := createDevCIBuildBinding("testns")
if diff := cmp.Diff(validDevCIBinding, binding); diff != "" {
t.Fatalf("createDevCIBuildBinding() failed:\n%s", diff)
}
}

func TestCreateStageCDDeployBinding(t *testing.T) {
validStageCDDeployBinding := triggersv1.TriggerBinding{
TypeMeta: v1.TypeMeta{
Kind: "TriggerBinding",
APIVersion: "tekton.dev/v1alpha1",
},
ObjectMeta: v1.ObjectMeta{
Name: "stage-cd-deploy-from-push-binding",
Namespace: "testns",
},
Spec: triggersv1.TriggerBindingSpec{
Params: []pipelinev1.Param{
pipelinev1.Param{
Name: "gitref",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.ref)",
Type: pipelinev1.ParamTypeString,
},
},
pipelinev1.Param{
Name: "gitsha",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.commits.0.id)",
Type: pipelinev1.ParamTypeString,
},
},
pipelinev1.Param{
Name: "gitrepositoryurl",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.repository.clone_url)",
Type: pipelinev1.ParamTypeString,
},
},
},
},
}
binding := createStageCDDeployBinding("testns")
if diff := cmp.Diff(validStageCDDeployBinding, binding); diff != "" {
t.Fatalf("createDevCIBuildBinding() failed:\n%s", diff)
}
}

func TestCreateStageCIDryRunBinding(t *testing.T) {
validStageCIDryRunBinding := triggersv1.TriggerBinding{
TypeMeta: v1.TypeMeta{
Kind: "TriggerBinding",
APIVersion: "tekton.dev/v1alpha1",
},
ObjectMeta: v1.ObjectMeta{
Name: "stage-ci-dryrun-from-pr-binding",
Namespace: "testns",
},
Spec: triggersv1.TriggerBindingSpec{
Params: []pipelinev1.Param{
pipelinev1.Param{
Name: "gitref",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.pull_request.head.ref)",
Type: pipelinev1.ParamTypeString,
},
},
pipelinev1.Param{
Name: "gitrepositoryurl",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.repository.clone_url)",
Type: pipelinev1.ParamTypeString,
},
},
},
},
}
binding := createStageCIDryRunBinding("testns")
if diff := cmp.Diff(validStageCIDryRunBinding, binding); diff != "" {
t.Errorf("createStageCIDryRunBinding() failed:\n%s", diff)
}
}

func TestCreateBindingParam(t *testing.T) {
validParam := pipelinev1.Param{
Name: "gitref",
Value: pipelinev1.ArrayOrString{
StringVal: "$(body.ref)",
Type: pipelinev1.ParamTypeString,
},
}
bindingParam := createBindingParam("gitref", "$(body.ref)")
if diff := cmp.Diff(validParam, bindingParam); diff != "" {
t.Fatalf("createBindingParam() failed\n%s", diff)
}
}
Loading

0 comments on commit eee2b6b

Please sign in to comment.