Skip to content

Commit

Permalink
Add CPU and RAM limits for Docker containers
Browse files Browse the repository at this point in the history
  • Loading branch information
robertjndw committed Jan 27, 2024
1 parent 40054fd commit 156a6d0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions HadesScheduler/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type DockerConfig struct {
DockerHost string `env:"DOCKER_HOST" envDefault:"unix:///var/run/docker.sock"`
ContainerAutoremove bool `env:"CONTAINER_AUTOREMOVE" envDefault:"true"`
DockerScriptExecutor string `env:"DOCKER_SCRIPT_EXECUTOR" envDefault:"/bin/bash -c"`
CPU_limit uint `env:"DOCKER_CPU_LIMIT"`
RAM_limit string `env:"DOCKER_RAM_LIMIT"`
}

func init() {
Expand Down Expand Up @@ -104,6 +106,19 @@ func executeStep(ctx context.Context, client *client.Client, step payload.Step,
AutoRemove: container_autoremove, // Remove the container after it is done only if the config is set to true
}

// Limit the resource usage of the containers
if DockerCfg.CPU_limit != 0 {
host_config.Resources.NanoCPUs = int64(DockerCfg.CPU_limit * 1e9)
}
if DockerCfg.RAM_limit != "" {
bytes, err := utils.ParseMemoryLimit(DockerCfg.RAM_limit)
if err != nil {
log.WithError(err).Errorf("Failed to parse RAM limit %s", DockerCfg.RAM_limit)
} else {
host_config.Resources.Memory = bytes
}
}

// Create the bash script if there is one
if step.Script != "" {
// Overwrite the default entrypoint
Expand Down
22 changes: 22 additions & 0 deletions shared/utils/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package utils

import (
"fmt"
"strconv"
"strings"

"github.com/caarlos0/env/v9"
"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -32,3 +36,21 @@ func LoadConfig(cfg interface{}) {

log.Debug("Config loaded: ", cfg)
}

func ParseMemoryLimit(limit string) (int64, error) {
unit := limit[len(limit)-2:]
number := limit[:len(limit)-2]
value, err := strconv.ParseInt(number, 10, 64)
if err != nil {
return 0, err
}

switch strings.ToUpper(unit) {
case "GB", "G":
return value * 1024 * 1024 * 1024, nil
case "MB", "M":
return value * 1024 * 1024, nil
default:
return 0, fmt.Errorf("unknown unit: %s", unit)
}
}

0 comments on commit 156a6d0

Please sign in to comment.