Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated the structure of V1 pipeline. #273

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 68 additions & 91 deletions convert/drone/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
// conversion context
type context struct {
pipeline []*v1.Pipeline
stage *v1.Pipeline
}

// Converter converts a Drone pipeline to a Harness
Expand Down Expand Up @@ -141,17 +140,11 @@ func (d *Converter) convert(ctx *context) ([]byte, error) {
//

// create the pipeline spec
pipeline := &v2.Pipeline{
Options: &v2.Default{
Registry: convertRegistry(ctx.pipeline),
},
}
pipeline := &v2.PipelineV1{}

// create the harness pipeline resource
config := &v2.Config{
Version: 1,
Kind: "pipeline",
Spec: pipeline,
config := &v2.ConfigV1{
Pipeline: pipeline,
}

for _, from := range ctx.pipeline {
Expand All @@ -165,23 +158,12 @@ func (d *Converter) convert(ctx *context) ([]byte, error) {
case v1.KindPipeline:
// TODO pipeline.name removed from spec
// pipeline.Name = from.Name

pipeline.Stages = append(pipeline.Stages, &v2.Stage{
Name: from.Name,
Type: "ci",
When: convertCond(from.Trigger),
Delegate: convertNode(from.Node),
Spec: &v2.StageCI{
Clone: convertClone(from.Clone),
Envs: copyenv(from.Environment),
Platform: convertPlatform(from.Platform),
Runtime: convertRuntime(from),
Steps: convertSteps(from, d.orgSecrets),
Volumes: convertVolumes(from.Volumes),

// TODO support for delegate.selectors from from.Node
// TODO support for stage.variables
},
runtime := determineRuntime(from)
pipeline.Stages = append(pipeline.Stages, &v2.StageV1{
Name: from.Name,
Clone: convertCloneV1(&from.Clone),
Runtime: runtime,
Steps: convertSteps(from, d.orgSecrets),
})
}
}
Expand All @@ -203,6 +185,13 @@ func (d *Converter) convert(ctx *context) ([]byte, error) {
return out, nil
}

func determineRuntime(from *v1.Pipeline) string {
if from.Runtime != "" {
return from.Runtime
}
return "machine"
}

func convertRegistry(src []*v1.Pipeline) *v2.Registry {
// note that registry credentials in Drone are stored
// at the stage level, but in Harness, we are proposing
Expand Down Expand Up @@ -236,28 +225,50 @@ func convertRegistry(src []*v1.Pipeline) *v2.Registry {
return dst
}

func convertSteps(src *v1.Pipeline, orgSecrets []string) []*v2.Step {
var dst []*v2.Step
for _, v := range src.Services {
if v != nil {
dst = append(dst, convertBackground(v, orgSecrets))
}
}
func convertSteps(src *v1.Pipeline, orgSecrets []string) []*v2.StepV1 {
var dst []*v2.StepV1
for _, v := range src.Steps {
if v != nil {
switch {
case v.Detach:
dst = append(dst, convertBackground(v, orgSecrets))
continue
case isPlugin(v):
dst = append(dst, convertPlugin(v, orgSecrets))
stepV1 := &v2.StepV1{
Name: v.Name,
}
stepV1.RunSpec = v2.RunSpec{
Container: &v2.ContainerSpec{
Image: v.Image,
Connector: v.Connector,
},
With: convertSettings(v.Settings, orgSecrets),
Env: convertVariables(v.Environment, orgSecrets),
}
dst = append(dst, stepV1)
default:
dst = append(dst, convertRun(v, orgSecrets))
stepV1 := &v2.StepV1{
Name: v.Name,
Run: &v2.RunSpec{
Container: &v2.ContainerSpec{
Image: v.Image,
Connector: v.Connector,
},
Env: convertVariables(v.Environment, orgSecrets),
Script: joinCommands(v.Commands),
},
}
dst = append(dst, convertRun(stepV1, orgSecrets))
}
}
}

return dst
}

func joinCommands(commands []string) string {
return strings.Join(commands, "\n")
}

func convertPlugin(src *v1.Step, orgSecrets []string) *v2.Step {
return &v2.Step{
Name: src.Name,
Expand Down Expand Up @@ -299,25 +310,20 @@ func convertBackground(src *v1.Step, orgSecrets []string) *v2.Step {
}
}

func convertRun(src *v1.Step, orgSecrets []string) *v2.Step {
return &v2.Step{
Name: src.Name,
Type: "script",
When: convertCond(src.When),
Spec: &v2.StepExec{
Image: src.Image,
Mount: convertMounts(src.Volumes),
Privileged: src.Privileged,
Pull: convertPull(src.Pull),
Shell: convertShell(src.Shell),
User: src.User,
Entrypoint: convertEntrypoint(src.Entrypoint),
Args: convertArgs(src.Entrypoint, src.Command),
Run: convertScript(src.Commands),
Envs: convertVariables(src.Environment, orgSecrets),
Resources: convertResourceLimits(&src.Resource),
// Volumes // FIX
func convertRun(src *v2.StepV1, orgSecrets []string) *v2.StepV1 {
runSpec := &v2.RunSpec{
With: src.Run.With,
Container: &v2.ContainerSpec{
Image: src.Run.Container.Image,
Connector: src.Run.Container.Connector,
},
Env: src.Run.Env,
Script: src.Run.Script,
}

return &v2.StepV1{
Name: src.Name,
Run: runSpec,
}
}

Expand Down Expand Up @@ -626,45 +632,16 @@ func convertShell(src string) string {
}
}

func convertRuntime(src *v1.Pipeline) *v2.Runtime {
if src.Type == "kubernetes" {
return &v2.Runtime{
Type: "kubernetes",
Spec: &v2.RuntimeKube{
// TODO should harness support `dns_config`
// TODO should harness support `host_aliases`
// TODO support for `tolerations`
Annotations: src.Metadata.Annotations,
Labels: src.Metadata.Labels,
Namespace: src.Metadata.Namespace,
NodeSelector: src.NodeSelector,
Node: src.NodeName,
ServiceAccount: src.ServiceAccount,
Resources: convertResourceRequests(&src.Resource),
},
func convertCloneV1(from *v1.Clone) *v2.CloneStageV1 {
// If from is nil, set Disabled to true
if from == nil {
return &v2.CloneStageV1{
Disabled: true,
}
}
return &v2.Runtime{
Type: "machine",
Spec: v2.RuntimeMachine{},
}
}

func convertClone(src v1.Clone) *v2.CloneStage {
dst := new(v2.CloneStage)
if v := src.Depth; v != 0 {
dst.Depth = int64(v)
}
if v := src.Disable; v {
dst.Disabled = true
}
if v := src.SkipVerify; v {
dst.Insecure = true
return &v2.CloneStageV1{
Disabled: from.Disable || true,
}
if v := src.Trace; v {
dst.Trace = true
}
return dst
}

func convertNode(src map[string]string) []string {
Expand Down
2 changes: 1 addition & 1 deletion convert/drone/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
)

func TestConvert(t *testing.T) {
tests, err := filepath.Glob("testdata/examples/*.yaml")
tests, err := filepath.Glob("testdatav1/examples/*.yaml")
if err != nil {
t.Error(err)
return
Expand Down
23 changes: 23 additions & 0 deletions convert/drone/testdatav1/examples/drone.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
kind: pipeline
type: docker
name: default

steps:
- name: backend
image: golang
environment:
PLUGIN_LOG_LEVEL: info
PLUGIN_ARTIFACT_REGION: us-east-1
PLUGIN_ARTIFACT_BUCKET_NAME: unified-versioning-bucket
commands:
- go build
- go test

- name: frontend
image: node
commands:
- npm install
- npm run test

...
25 changes: 25 additions & 0 deletions convert/drone/testdatav1/examples/drone.yaml.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pipeline:
stages:
- clone:
disabled: true
name: default
runtime: machine
steps:
- name: backend
run:
container:
image: golang
env:
PLUGIN_ARTIFACT_BUCKET_NAME: unified-versioning-bucket
PLUGIN_ARTIFACT_REGION: us-east-1
PLUGIN_LOG_LEVEL: info
script: |-
go build
go test
- name: frontend
run:
container:
image: node
script: |-
npm install
npm run test
29 changes: 29 additions & 0 deletions convert/drone/testdatav1/examples/settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
kind: pipeline
type: docker
name: default

steps:
- name: backend
image: golang
environment:
PLUGIN_LOG_LEVEL: info
PLUGIN_ARTIFACT_REGION: us-east-1
PLUGIN_ARTIFACT_BUCKET_NAME: unified-versioning-bucket
commands:
- go build
- go test

- name: frontend
image: node
settings:
dockerfile: docker/docker/Dockerfile.windows.amd64.1809
repo: plugins/buildx
username:
from_secret: docker_username
password:
from_secret: docker_password
auto_tag: true
auto_tag_suffix: windows-1809-amd64

...
28 changes: 28 additions & 0 deletions convert/drone/testdatav1/examples/settings.yaml.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pipeline:
stages:
- clone:
disabled: true
name: default
runtime: machine
steps:
- name: backend
run:
container:
image: golang
env:
PLUGIN_ARTIFACT_BUCKET_NAME: unified-versioning-bucket
PLUGIN_ARTIFACT_REGION: us-east-1
PLUGIN_LOG_LEVEL: info
script: |-
go build
go test
- container:
image: node
name: frontend
with:
auto_tag: true
auto_tag_suffix: windows-1809-amd64
dockerfile: docker/docker/Dockerfile.windows.amd64.1809
password: <+secrets.getValue("docker_password")>
repo: plugins/buildx
username: <+secrets.getValue("docker_username")>
19 changes: 19 additions & 0 deletions convert/drone/testdatav1/examples/step.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
kind: pipeline
type: docker
name: default

steps:
- name: backend
image: golang
commands:
- go build
- go test

- name: frontend
image: node
commands:
- npm install
- npm run test

...
21 changes: 21 additions & 0 deletions convert/drone/testdatav1/examples/step.yaml.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pipeline:
stages:
- clone:
disabled: true
name: default
runtime: machine
steps:
- name: backend
run:
container:
image: golang
script: |-
go build
go test
- name: frontend
run:
container:
image: node
script: |-
npm install
npm run test
Loading