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

Commit

Permalink
Merge pull request #5 from chetan-rns/bootstrap
Browse files Browse the repository at this point in the history
Issue-38: Add functions to generate Tekton task objects
  • Loading branch information
wtam2018 authored Feb 19, 2020
2 parents 9351c47 + 1e40c29 commit ed0b881
Show file tree
Hide file tree
Showing 12 changed files with 558 additions and 12 deletions.
11 changes: 9 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import:
- package: k8s.io/apimachinery
version: origin-4.1-kubernetes-1.13.4
repo: https://github.com/openshift/kubernetes-apimachinery.git
subpackages:
- pkg/apis/meta/v1
- package: k8s.io/api
version: origin-4.1-kubernetes-1.13.4
repo: https://github.com/openshift/kubernetes-api.git
Expand Down
10 changes: 8 additions & 2 deletions pkg/pipelines/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/mitchellh/go-homedir"
"github.com/openshift/odo/pkg/pipelines/eventlisteners"
"github.com/openshift/odo/pkg/pipelines/routes"
"github.com/openshift/odo/pkg/pipelines/tasks"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -60,10 +61,15 @@ func Bootstrap(quayUsername, baseRepo, prefix string) error {
}
outputs = append(outputs, dockerSecret)

eventListener := eventlisteners.GenerateEventListener(baseRepo)
tasks := tasks.Generate(githubAuth.GetName())
for _, task := range tasks {
outputs = append(outputs, task)
}

eventListener := eventlisteners.Generate(baseRepo)
outputs = append(outputs, eventListener)

route := routes.GenerateRoute()
route := routes.Generate()
outputs = append(outputs, route)

for _, r := range outputs {
Expand Down
4 changes: 2 additions & 2 deletions pkg/pipelines/eventlisteners/eventlisteners.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const (
stageCDDeployFilters = "(header.match('X-GitHub-Event', 'push') && body.repository.full_name == '%s') && body.ref.startsWith('refs/heads/master')"
)

// GenerateEventListener will create the required eventlisteners
func GenerateEventListener(githubRepo string) triggersv1.EventListener {
// Generate will create the required eventlisteners
func Generate(githubRepo string) triggersv1.EventListener {
githubStageRepo := githubRepo + "-stage-config"
return triggersv1.EventListener{
TypeMeta: createListenerTypeMeta(),
Expand Down
4 changes: 2 additions & 2 deletions pkg/pipelines/eventlisteners/eventlisteners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func TestGenerateEventListener(t *testing.T) {
},
}

eventListener := GenerateEventListener("sample")
eventListener := Generate("sample")
if diff := cmp.Diff(validEventListener, eventListener); diff != "" {
t.Fatalf("GenerateEventListener() failed:\n%s", diff)
t.Fatalf("Generate() failed:\n%s", diff)
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/pipelines/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"k8s.io/apimachinery/pkg/util/intstr"
)

// GenerateRoute creates a route for event listener
func GenerateRoute() routev1.Route {
// Generate creates a route for event listener
func Generate() routev1.Route {
return routev1.Route{
TypeMeta: createRouteTypeMeta(),
ObjectMeta: createRouteObjectMeta("github-webhook-event-listener"),
Expand Down
4 changes: 2 additions & 2 deletions pkg/pipelines/routes/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func TestGenerateRoute(t *testing.T) {
WildcardPolicy: routev1.WildcardPolicyNone,
},
}
route := GenerateRoute()
route := Generate()
if diff := cmp.Diff(validRoute, route); diff != "" {
t.Fatalf("GenerateRoute() failed:\n%s", diff)
t.Fatalf("Generate() failed:\n%s", diff)
}
}

Expand Down
71 changes: 71 additions & 0 deletions pkg/pipelines/tasks/deploy_from_source_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package tasks

import (
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2"
)

func generateDeployFromSourceTask() pipelinev1.Task {
task := pipelinev1.Task{
TypeMeta: createTaskTypeMeta(),
ObjectMeta: createTaskObjectMeta("deploy-from-source-task"),
Spec: pipelinev1.TaskSpec{
Inputs: createInputsForDeployFromSourceTask(),
TaskSpec: v1alpha2.TaskSpec{
Steps: createStepsForDeployFromSourceTask(),
},
},
}
return task
}

func createStepsForDeployFromSourceTask() []pipelinev1.Step {
return []pipelinev1.Step{
pipelinev1.Step{
Container: createContainer(
"run-kubectl",
"quay.io/kmcdermo/k8s-kubectl:latest",
"/workspace/source",
[]string{"kubectl"},
argsForRunKubectlStep,
),
},
}
}

var argsForRunKubectlStep = []string{
"apply",
"--dry-run=$(inputs.params.DRYRUN)",
"-n",
"$(inputs.params.NAMESPACE)",
"-k",
"$(inputs.params.PATHTODEPLOYMENT)",
}

func createInputsForDeployFromSourceTask() *pipelinev1.Inputs {
return &pipelinev1.Inputs{
Resources: []pipelinev1.TaskResource{
createTaskResource("source", "git"),
},
Params: []pipelinev1.ParamSpec{
createTaskParamWithDefault(
"PATHTODEPLOYMENT",
"Path to the manifest to apply",
pipelinev1.ParamTypeString,
"deploy",
),
createTaskParam(
"NAMESPACE",
"Namespace to deploy into",
pipelinev1.ParamTypeString,
),
createTaskParamWithDefault(
"DRYRUN",
"If true run a server-side dryrun.",
pipelinev1.ParamTypeString,
"false",
),
},
}

}
91 changes: 91 additions & 0 deletions pkg/pipelines/tasks/deploy_using_kubectl_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package tasks

import (
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2"
)

func generateDeployUsingKubectlTask() pipelinev1.Task {
return pipelinev1.Task{
TypeMeta: createTaskTypeMeta(),
ObjectMeta: createTaskObjectMeta("deploy-using-kubectl-task"),
Spec: pipelinev1.TaskSpec{
Inputs: createInputsForDeployKubectlTask(),
TaskSpec: v1alpha2.TaskSpec{
Steps: createStepsForDeployKubectlTask(),
},
},
}
}

var argsForReplaceImageStep = []string{
"w",
"-i",
"$(inputs.params.PATHTODEPLOYMENT)/deployment.yaml",
"$(inputs.params.YAMLPATHTOIMAGE)",
"$(inputs.resources.image.url)",
}

var argsForKubectlStep = []string{
"apply",
"-n",
"$(inputs.params.NAMESPACE)",
"-k",
"$(inputs.params.PATHTODEPLOYMENT)",
}

func createStepsForDeployKubectlTask() []pipelinev1.Step {
return []pipelinev1.Step{
pipelinev1.Step{
Container: createContainer(
"replace-image",
"mikefarah/yq",
"/workspace/source",
[]string{"yq"},
argsForReplaceImageStep,
),
},
pipelinev1.Step{
Container: createContainer(
"run-kubectl",
"quay.io/kmcdermo/k8s-kubectl:latest",
"/workspace/source",
[]string{"kubectl"},
argsForKubectlStep,
),
},
}
}

func createInputsForDeployKubectlTask() *pipelinev1.Inputs {
return &pipelinev1.Inputs{
Resources: []pipelinev1.TaskResource{
createTaskResource("source", "git"),
createTaskResource("image", "image"),
},
Params: []pipelinev1.ParamSpec{
createTaskParamWithDefault(
"PATHTODEPLOYMENT",
"Path to the manifest to apply",
"string",
"deploy",
),
createTaskParam(
"NAMESPACE",
"Namespace to deploy into",
"string",
),
createTaskParamWithDefault(
"DRYRUN",
"If true run a server-side dryrun.",
"string",
"false",
),
createTaskParam(
"YAMLPATHTOIMAGE",
"The path to the image to replace in the yaml manifest (arg to yq)",
"string",
),
},
}
}
92 changes: 92 additions & 0 deletions pkg/pipelines/tasks/github_status_task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package tasks

import (
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2"
corev1 "k8s.io/api/core/v1"
)

func generateGithubStatusTask(secretName string) pipelinev1.Task {
task := pipelinev1.Task{
TypeMeta: createTaskTypeMeta(),
ObjectMeta: createTaskObjectMeta("create-github-status-task"),
Spec: pipelinev1.TaskSpec{
Inputs: createInputsForGithubStatusTask(),
TaskSpec: v1alpha2.TaskSpec{
Steps: createStepsForGithubStatusTask(secretName),
},
},
}
return task
}

var argsForStartStatusStep = []string{
"create-status",
"--repo",
"$(inputs.params.REPO)",
"--sha",
"$(inputs.params.COMMIT_SHA)",
"--state",
"$(inputs.params.STATE)",
"--target-url",
"$(inputs.params.TARGET_URL)",
"--description",
"$(inputs.params.DESCRIPTION)",
"--context",
"$(inputs.params.CONTEXT)",
}

func createInputsForGithubStatusTask() *pipelinev1.Inputs {
return &pipelinev1.Inputs{
Params: []pipelinev1.ParamSpec{
createTaskParam(
"REPO",
"The repo to publish the status update for e.g. tektoncd/triggers",
pipelinev1.ParamTypeString,
),
createTaskParam(
"COMMIT_SHA",
"The specific commit to report a status for.",
pipelinev1.ParamTypeString,
),
createTaskParam(
"STATE",
"The state to report error, failure, pending, or success.",
pipelinev1.ParamTypeString,
),
createTaskParamWithDefault(
"TARGET_URL",
"The target URL to associate with this status.",
pipelinev1.ParamTypeString,
"",
),
createTaskParam(
"DESCRIPTION",
"A short description of the status.",
pipelinev1.ParamTypeString,
),
createTaskParam(
"CONTEXT",
"A string label to differentiate this status from the status of other systems.",
pipelinev1.ParamTypeString,
),
},
}
}

func createStepsForGithubStatusTask(secretName string) []pipelinev1.Step {
return []pipelinev1.Step{
pipelinev1.Step{
Container: corev1.Container{
Name: "start-status",
Image: "quay.io/kmcdermo/github-tool:latest",
WorkingDir: "/workspace/source",
Env: []corev1.EnvVar{
createEnvFromSecret("GITHUB_TOKEN", secretName, "token"),
},
Command: []string{"github-tools"},
Args: argsForStartStatusStep,
},
},
}
}
Loading

0 comments on commit ed0b881

Please sign in to comment.