diff --git a/command/compile.go b/command/compile.go index 69a8de3..2065d18 100644 --- a/command/compile.go +++ b/command/compile.go @@ -183,13 +183,10 @@ func registerCompile(app *kingpin.Application) { Int64Var(&c.Resources.CPUShares) cmd.Flag("memory", "container memory limit"). - Int64Var(&c.Resources.MemLimit) + Int64Var(&c.Resources.Memory) cmd.Flag("memory-swap", "container memory swap limit"). - Int64Var(&c.Resources.MemSwapLimit) - - cmd.Flag("shmsize", "container shm size"). - Int64Var(&c.Resources.ShmSize) + Int64Var(&c.Resources.MemorySwap) cmd.Flag("docker-config", "path to the docker config file"). StringVar(&c.Config) diff --git a/command/daemon/config.go b/command/daemon/config.go index acfdaa9..d535e0f 100644 --- a/command/daemon/config.go +++ b/command/daemon/config.go @@ -73,6 +73,15 @@ type Config struct { Trusted bool `envconfig:"DRONE_LIMIT_TRUSTED"` } + Resources struct { + Memory int64 `envconfig:"DRONE_MEMORY_LIMIT"` + MemorySwap int64 `envconfig:"DRONE_MEMORY_SWAP_LIMIT"` + CPUQuota int64 `envconfig:"DRONE_CPU_QUOTA"` + CPUPeriod int64 `envconfig:"DRONE_CPU_PERIOD"` + CPUShares int64 `envconfig:"DRONE_CPU_SHARES"` + CPUSet []string `envconfig:"DRONE_CPU_SET"` + } + Secret struct { Endpoint string `envconfig:"DRONE_SECRET_PLUGIN_ENDPOINT"` Token string `envconfig:"DRONE_SECRET_PLUGIN_TOKEN"` diff --git a/command/daemon/daemon.go b/command/daemon/daemon.go index 9ddca46..64cc8b5 100644 --- a/command/daemon/daemon.go +++ b/command/daemon/daemon.go @@ -123,7 +123,14 @@ func (c *daemonCommand) run(*kingpin.ParseContext) error { Privileged: append(config.Runner.Privileged, compiler.Privileged...), Networks: config.Runner.Networks, Volumes: config.Runner.Volumes, - // Resources: nil, + Resources: compiler.Resources{ + Memory: config.Resources.Memory, + MemorySwap: config.Resources.MemorySwap, + CPUQuota: config.Resources.CPUQuota, + CPUPeriod: config.Resources.CPUPeriod, + CPUShares: config.Resources.CPUShares, + CPUSet: config.Resources.CPUSet, + }, Registry: registry.Combine( registry.File( config.Docker.Config, diff --git a/command/exec.go b/command/exec.go index be7f33f..b9842e6 100644 --- a/command/exec.go +++ b/command/exec.go @@ -313,13 +313,10 @@ func registerExec(app *kingpin.Application) { Int64Var(&c.Resources.CPUShares) cmd.Flag("memory", "container memory limit"). - Int64Var(&c.Resources.MemLimit) + Int64Var(&c.Resources.Memory) cmd.Flag("memory-swap", "container memory swap limit"). - Int64Var(&c.Resources.MemSwapLimit) - - cmd.Flag("shmsize", "container shm size"). - Int64Var(&c.Resources.ShmSize) + Int64Var(&c.Resources.MemorySwap) cmd.Flag("public-key", "public key file path"). ExistingFileVar(&c.PublicKey) diff --git a/engine/compiler/compiler.go b/engine/compiler/compiler.go index 8d53f99..2829937 100644 --- a/engine/compiler/compiler.go +++ b/engine/compiler/compiler.go @@ -41,13 +41,12 @@ var Privileged = []string{ // 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 + Memory int64 + MemorySwap int64 + CPUQuota int64 + CPUPeriod int64 + CPUShares int64 + CPUSet []string } // Args provides compiler arguments. @@ -373,9 +372,8 @@ func (c *Compiler) Compile(ctx context.Context, args Args) *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.MemSwapLimit = c.Resources.MemorySwap + step.MemLimit = c.Resources.Memory step.CPUPeriod = c.Resources.CPUPeriod step.CPUQuota = c.Resources.CPUQuota step.CPUShares = c.Resources.CPUShares diff --git a/engine/compiler/step.go b/engine/compiler/step.go index 0e0fa9c..d0bd093 100644 --- a/engine/compiler/step.go +++ b/engine/compiler/step.go @@ -42,7 +42,6 @@ func createStep(spec *resource.Pipeline, src *resource.Step) *engine.Step { // Networks: nil, // set in compiler.go - Files: nil, // set below Volumes: nil, // set below Devices: nil, // see below // Resources: toResources(src), // TODO diff --git a/engine/convert.go b/engine/convert.go index 8fe992d..045a75e 100644 --- a/engine/convert.go +++ b/engine/convert.go @@ -73,19 +73,16 @@ func toHostConfig(spec *Spec, step *Step) *container.HostConfig { if len(step.ExtraHosts) > 0 { config.ExtraHosts = step.ExtraHosts } - // if step.Resources != nil { - // config.Resources = container.Resources{} - // if limits := step.Resources.Limits; limits != nil { - // config.Resources.Memory = limits.Memory - // // TODO(bradrydewski) set config.Resources.CPUPercent - - // // IMPORTANT docker and kubernetes use - // // different units of measure for cpu limits. - // // we need to figure out how to convert from - // // the kubernetes unit of measure to the docker - // // unit of measure. - // } - // } + if isUnlimited(step) == false { + config.Resources = container.Resources{ + CPUPeriod: step.CPUPeriod, + CPUQuota: step.CPUQuota, + CpusetCpus: strings.Join(step.CPUSet, ","), + CPUShares: step.CPUShares, + Memory: step.MemLimit, + MemorySwap: step.MemSwapLimit, + } + } if len(step.Volumes) != 0 { config.Devices = toDeviceSlice(spec, step) @@ -259,6 +256,16 @@ func toEnv(env map[string]string) []string { return envs } +// returns true if the container has no resource limits. +func isUnlimited(res *Step) bool { + return len(res.CPUSet) == 0 && + res.CPUPeriod == 0 && + res.CPUQuota == 0 && + res.CPUShares == 0 && + res.MemLimit == 0 && + res.MemSwapLimit == 0 +} + // returns true if the volume is a bind mount. func isBindMount(volume *Volume) bool { return volume.HostPath != nil diff --git a/engine/engine_impl.go b/engine/engine_impl.go index 8865779..bc17a2c 100644 --- a/engine/engine_impl.go +++ b/engine/engine_impl.go @@ -214,19 +214,6 @@ func (e *Docker) create(ctx context.Context, spec *Spec, step *Step, output io.W return err } - // // use the default user-defined network if network_mode - // // is not otherwise specified. - // if step.Network == "" { - // for _, net := range step.Networks { - // err = e.client.NetworkConnect(ctx, net, step.ID, &network.EndpointSettings{ - // Aliases: []string{net}, - // }) - // if err != nil { - // return nil - // } - // } - // } - return nil } diff --git a/engine/resource/pipeline.go b/engine/resource/pipeline.go index edc465c..c992fff 100644 --- a/engine/resource/pipeline.go +++ b/engine/resource/pipeline.go @@ -13,8 +13,6 @@ var ( _ manifest.PlatformResource = (*Pipeline)(nil) ) -// TODO(bradrydzewski) add resource limits - // Defines the Resource Kind and Type. const ( Kind = "pipeline" @@ -108,8 +106,6 @@ type ( Volumes []*VolumeMount `json:"volumes,omitempty"` When manifest.Conditions `json:"when,omitempty"` WorkingDir string `json:"working_dir,omitempty" yaml:"working_dir"` - - // Resources *Resources `json:"resources,omitempty"` } // Volume that can be mounted by containers. diff --git a/engine/spec.go b/engine/spec.go index 393ba8e..9b524dc 100644 --- a/engine/spec.go +++ b/engine/spec.go @@ -10,7 +10,6 @@ type ( // execution. Spec struct { Platform Platform `json:"platform,omitempty"` - Files []*File `json:"files,omitempty"` Steps []*Step `json:"steps,omitempty"` Volumes []*Volume `json:"volumes,omitempty"` Network Network `json:"network"` @@ -33,7 +32,6 @@ type ( Entrypoint []string `json:"entrypoint,omitempty"` Envs map[string]string `json:"environment,omitempty"` ExtraHosts []string `json:"extra_hosts,omitempty"` - Files []*File `json:"files,omitempty"` IgnoreErr bool `json:"ignore_err,omitempty"` IgnoreStdout bool `json:"ignore_stderr,omitempty"` IgnoreStderr bool `json:"ignore_stdout,omitempty"` @@ -54,16 +52,6 @@ type ( WorkingDir string `json:"working_dir,omitempty"` } - // File defines a file that should be uploaded or - // mounted somewhere in the step container or virtual - // machine prior to command execution. - File struct { - Path string `json:"path,omitempty"` - Mode uint32 `json:"mode,omitempty"` - Data []byte `json:"data,omitempty"` - IsDir bool `json:"is_dir,omitempty"` - } - // Platform defines the target platform. Platform struct { OS string `json:"os,omitempty"`