source labels from runner-go

This commit is contained in:
Brad Rydzewski
2019-10-17 14:33:16 -07:00
parent 1abfbf69ea
commit b198003a0c
7 changed files with 102 additions and 57 deletions

View File

@@ -16,6 +16,7 @@ import (
"github.com/drone/drone-go/drone"
"github.com/drone/runner-go/clone"
"github.com/drone/runner-go/environ"
"github.com/drone/runner-go/labels"
"github.com/drone/runner-go/manifest"
"github.com/drone/runner-go/secret"
@@ -36,6 +37,18 @@ var Privileged = []string{
"plugins/heroku",
}
// Resources defines container resource constraints. These
// constraints are per-container, not per-pipeline.
type Resources struct {
MemLimit int64
MemSwapLimit int64
ShmSize int64
CPUQuota int64
CPUPeriod int64
CPUShares int64
CPUSet []string
}
// Compiler compiles the Yaml configuration file to an
// intermediate representation optimized for simple execution.
type Compiler struct {
@@ -88,6 +101,10 @@ type Compiler struct {
// mounted to each pipeline container.
Volumes map[string]string
// Resources provides global resource constraints
// applies to pipeline containers.
Resources Resources
// Netrc provides netrc parameters that can be used by the
// default clone step to authenticate to the remote
// repository.
@@ -111,23 +128,27 @@ func (c *Compiler) Compile(ctx context.Context) *engine.Spec {
Path: base,
}
// create system labels
labels := labels.Combine(
c.Labels,
labels.FromRepo(c.Repo),
labels.FromBuild(c.Build),
labels.FromStage(c.Stage),
labels.FromSystem(c.System),
labels.WithTimeout(c.Repo),
)
// create the workspace volume
volume := &engine.VolumeEmptyDir{
ID: random(),
Name: mount.Name,
Labels: environ.Combine(
c.Labels,
createLabels(c.Repo, c.Build, c.Stage),
),
ID: random(),
Name: mount.Name,
Labels: labels,
}
spec := &engine.Spec{
Network: engine.Network{
ID: random(),
Labels: environ.Combine(
c.Labels,
createLabels(c.Repo, c.Build, c.Stage),
),
ID: random(),
Labels: labels,
},
Platform: engine.Platform{
OS: c.Pipeline.Platform.OS,
@@ -200,10 +221,7 @@ func (c *Compiler) Compile(ctx context.Context) *engine.Spec {
step.ID = random()
step.Envs = environ.Combine(envs, step.Envs)
step.WorkingDir = full
step.Labels = environ.Combine(
c.Labels,
createLabels(c.Repo, c.Build, c.Stage),
)
step.Labels = labels
step.Volumes = append(step.Volumes, mount)
spec.Steps = append(spec.Steps, step)
}
@@ -214,10 +232,7 @@ func (c *Compiler) Compile(ctx context.Context) *engine.Spec {
dst.Detach = true
dst.Envs = environ.Combine(envs, dst.Envs)
dst.Volumes = append(dst.Volumes, mount)
dst.Labels = environ.Combine(
c.Labels,
createLabels(c.Repo, c.Build, c.Stage),
)
dst.Labels = labels
setupScript(src, dst, os)
setupWorkdir(src, dst, full)
spec.Steps = append(spec.Steps, dst)
@@ -234,10 +249,7 @@ func (c *Compiler) Compile(ctx context.Context) *engine.Spec {
dst := createStep(c.Pipeline, src)
dst.Envs = environ.Combine(envs, dst.Envs)
dst.Volumes = append(dst.Volumes, mount)
dst.Labels = environ.Combine(
c.Labels,
createLabels(c.Repo, c.Build, c.Stage),
)
dst.Labels = labels
setupScript(src, dst, full)
setupWorkdir(src, dst, full)
spec.Steps = append(spec.Steps, dst)
@@ -294,6 +306,17 @@ func (c *Compiler) Compile(ctx context.Context) *engine.Spec {
}
}
// append global resource limits to steps
for _, step := range spec.Steps {
step.MemSwapLimit = c.Resources.MemSwapLimit
step.MemLimit = c.Resources.MemLimit
step.ShmSize = c.Resources.ShmSize
step.CPUPeriod = c.Resources.CPUPeriod
step.CPUQuota = c.Resources.CPUQuota
step.CPUShares = c.Resources.CPUShares
step.CPUSet = c.Resources.CPUSet
}
// append global networks to the steps.
for _, step := range spec.Steps {
step.Networks = append(step.Networks, c.Networks...)

View File

@@ -1,33 +0,0 @@
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Polyform License
// that can be found in the LICENSE file.
package compiler
import (
"fmt"
"time"
"github.com/drone/drone-go/drone"
)
func createLabels(
repo *drone.Repo,
build *drone.Build,
stage *drone.Stage,
) map[string]string {
return map[string]string{
"io.drone": "true",
"io.drone.build.number": fmt.Sprint(build.Number),
"io.drone.repo.namespace": repo.Namespace,
"io.drone.repo.name": repo.Name,
"io.drone.stage.name": stage.Name,
"io.drone.stage.number": fmt.Sprint(stage.Number),
"io.drone.ttl": fmt.Sprint(time.Duration(repo.Timeout) * time.Minute),
"io.drone.expires": fmt.Sprint(time.Now().Add(time.Duration(repo.Timeout)*time.Minute + time.Hour).Unix()),
"io.drone.created": fmt.Sprint(time.Now().Unix()),
"io.drone.protected": "false",
}
// labels["io.drone.step.name"] = step.Name
// labels["io.drone.step.number"] = fmt.Sprint(step.Number)
}

View File

@@ -21,6 +21,10 @@ type (
ID string `json:"id,omitempty"`
Auth *Auth `json:"auth,omitempty"`
Command []string `json:"args,omitempty"`
CPUPeriod int64 `json:"cpu_period,omitempty"`
CPUQuota int64 `json:"cpu_quota,omitempty"`
CPUShares int64 `json:"cpu_shares,omitempty"`
CPUSet []string `json:"cpu_set,omitempty"`
Detach bool `json:"detach,omitempty"`
DependsOn []string `json:"depends_on,omitempty"`
DNS []string `json:"dns,omitempty"`
@@ -34,6 +38,8 @@ type (
IgnoreStderr bool `json:"ignore_stdout,omitempty"`
Image string `json:"image,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
MemSwapLimit int64 `json:"memswap_limit,omitempty"`
MemLimit int64 `json:"mem_limit,omitempty"`
Name string `json:"name,omitempty"`
Network string `json:"network,omitempty"`
Networks []string `json:"networks,omitempty"`
@@ -41,6 +47,7 @@ type (
Pull PullPolicy `json:"pull,omitempty"`
RunPolicy RunPolicy `json:"run_policy,omitempty"`
Secrets []*Secret `json:"secrets,omitempty"`
ShmSize int64 `json:"shm_size,omitempty"`
User string `json:"user,omitempty"`
Volumes []*VolumeMount `json:"volumes,omitempty"`
WorkingDir string `json:"working_dir,omitempty"`