Skip to content

Commit

Permalink
Add Tekton Pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuGongpu committed Jul 14, 2020
1 parent f845528 commit bb0f6d1
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/cmdexport/orchestrators/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestPipeline(t *testing.T) {
cloudBuildTestSuite,
gitlabCITestSuite,
jenkinsTestSuite,
tektonPipelineTestSuite,
}

for _, testSuite := range testSuites {
Expand Down
90 changes: 90 additions & 0 deletions internal/cmdexport/orchestrators/tekton.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,101 @@
package orchestrators

import (
"bytes"
"path"

"sigs.k8s.io/kustomize/kyaml/yaml"

"github.com/GoogleContainerTools/kpt/internal/cmdexport/types"
)

// TektonPipeline wraps a Pipeline object in Tekton.
// @see https://github.com/tektoncd/pipeline/blob/master/docs/pipelines.md
type TektonPipeline struct {
APIVersion string `yaml:"apiVersion,omitempty"`
Kind string `yaml:",omitempty"`
Metadata *TektonMetadata `yaml:",omitempty"`
Spec *TektonPipelineSpec `yaml:",omitempty"`
task *TektonTask
}

func (p *TektonPipeline) Init(config *types.PipelineConfig) Pipeline {
taskName := "run-kpt-functions"

p.APIVersion = "tekton.dev/v1beta1"
p.Kind = "Pipeline"
p.Metadata = &TektonMetadata{Name: taskName}

workspaceName := "shared-workspace"
pipelineWorkspace := &TektonWorkspace{
Name: workspaceName,
}

taskConfig := &TektonTaskConfig{PipelineConfig: config, Name: taskName}
task := new(TektonTask).Init(taskConfig)

pipelineTaskWorkspace := &TektonWorkspace{
Name: "source",
Workspace: workspaceName,
}
pipelineTask := &TektonPipelineTask{
Name: "kpt",
TaskRef: &TektonPipelineTaskRef{Name: taskName},
Workspaces: []*TektonWorkspace{pipelineTaskWorkspace},
}

p.task = task
p.Spec = &TektonPipelineSpec{
Workspaces: []*TektonWorkspace{pipelineWorkspace},
Tasks: []*TektonPipelineTask{pipelineTask},
}

return p
}

// Generate outputs a multi-doc yaml that contains Tekton Pipeline Object and a Tekton Task object.
func (p *TektonPipeline) Generate() (out []byte, err error) {
task, err := p.task.Generate()

if err != nil {
return
}

pipeline, err := yaml.Marshal(p)

if err != nil {
return
}

// Concat the two yaml docs.
b := &bytes.Buffer{}

b.Write(task)
b.WriteString("---\n")
b.Write(pipeline)

return b.Bytes(), nil
}

// TektonPipelineSpec describes the spec of a Pipeline.
type TektonPipelineSpec struct {
Workspaces []*TektonWorkspace `yaml:",omitempty"`
Tasks []*TektonPipelineTask `yaml:",omitempty"`
}

// // TektonPipelineTaskRef represents a task in a Tekton Pipeline object.
type TektonPipelineTask struct {
Name string `yaml:",omitempty"`
TaskRef *TektonPipelineTaskRef `yaml:"taskRef,omitempty"`
RunAfter []string `yaml:"runAfter,omitempty"`
Workspaces []*TektonWorkspace `yaml:",omitempty"`
}

// TektonPipelineTaskRef represents a taskRef field in a task of a Tekton Pipeline object.
type TektonPipelineTaskRef struct {
Name string `yaml:",omitempty"`
}

// TektonMetadata contains metadata to describe a resource object.
type TektonMetadata struct {
Name string `yaml:",omitempty"`
Expand All @@ -31,6 +119,8 @@ type TektonMetadata struct {
type TektonWorkspace struct {
Name string `yaml:",omitempty"`
MountPath string `yaml:"mountPath,omitempty"`
// Workspace references another workspace declared elsewhere.
Workspace string `yaml:",omitempty"`
}

// TektonTaskConfig contains necessary configurations of the TektonTask class.
Expand Down
155 changes: 155 additions & 0 deletions internal/cmdexport/orchestrators/tekton_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,158 @@ func TestTektonTask(t *testing.T) {
})
}
}

// TODO: TektonPipeline
var tektonPipelineTestCases = []testCase{
{
description: "generate a tekton pipeline with a task",
config: &types.PipelineConfig{
Dir: "local-resources/",
},
expected: `
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: run-kpt-functions
spec:
workspaces:
- name: source
mountPath: /source
steps:
- name: run-kpt-functions
image: gongpu/kpt:latest
args:
- fn
- run
- $(workspaces.source.path)/local-resources
volumeMounts:
- name: docker-socket
mountPath: /var/run/docker.sock
volumes:
- name: docker-socket
hostPath:
path: /var/run/docker.sock
type: Socket
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: run-kpt-functions
spec:
workspaces:
- name: shared-workspace
tasks:
- name: kpt
taskRef:
name: run-kpt-functions
workspaces:
- name: source
workspace: shared-workspace
`,
},
{
description: "generate a tekton pipeline with --fn-path",
config: &types.PipelineConfig{
Dir: "local-resources",
FnPaths: []string{"config/"},
},
expected: `
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: run-kpt-functions
spec:
workspaces:
- name: source
mountPath: /source
steps:
- name: run-kpt-functions
image: gongpu/kpt:latest
args:
- fn
- run
- $(workspaces.source.path)/local-resources
- --fn-path
- $(workspaces.source.path)/config
volumeMounts:
- name: docker-socket
mountPath: /var/run/docker.sock
volumes:
- name: docker-socket
hostPath:
path: /var/run/docker.sock
type: Socket
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: run-kpt-functions
spec:
workspaces:
- name: shared-workspace
tasks:
- name: kpt
taskRef:
name: run-kpt-functions
workspaces:
- name: source
workspace: shared-workspace
`,
},
{
description: "generate a tekton task with multiple --fn-path",
config: &types.PipelineConfig{
Dir: "local-resources",
FnPaths: []string{"config/gate-keeper.yaml", "config/label-namespace.yaml"},
},
expected: `
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: run-kpt-functions
spec:
workspaces:
- name: source
mountPath: /source
steps:
- name: run-kpt-functions
image: gongpu/kpt:latest
args:
- fn
- run
- $(workspaces.source.path)/local-resources
- --fn-path
- $(workspaces.source.path)/config/gate-keeper.yaml
- --fn-path
- $(workspaces.source.path)/config/label-namespace.yaml
volumeMounts:
- name: docker-socket
mountPath: /var/run/docker.sock
volumes:
- name: docker-socket
hostPath:
path: /var/run/docker.sock
type: Socket
---
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: run-kpt-functions
spec:
workspaces:
- name: shared-workspace
tasks:
- name: kpt
taskRef:
name: run-kpt-functions
workspaces:
- name: source
workspace: shared-workspace
`,
},
}

var tektonPipelineTestSuite = testSuite{
pipeline: new(TektonPipeline),
testCases: tektonPipelineTestCases,
}

0 comments on commit bb0f6d1

Please sign in to comment.