Преглед на файлове

docker: support memory limit

Miao Wang преди 4 години
родител
ревизия
f0ccdc47dc
променени са 3 файла, в които са добавени 8 реда и са изтрити 0 реда
  1. 2 0
      worker/docker.go
  2. 2 0
      worker/docker_test.go
  3. 4 0
      worker/runner.go

+ 2 - 0
worker/docker.go

@@ -13,6 +13,7 @@ type dockerHook struct {
 	image   string
 	volumes []string
 	options []string
+	memoryLimit MemBytes
 }
 
 func newDockerHook(p mirrorProvider, gCfg dockerConfig, mCfg mirrorConfig) *dockerHook {
@@ -35,6 +36,7 @@ func newDockerHook(p mirrorProvider, gCfg dockerConfig, mCfg mirrorConfig) *dock
 		image:   mCfg.DockerImage,
 		volumes: volumes,
 		options: options,
+		memoryLimit: mCfg.MemoryLimit,
 	}
 }
 

+ 2 - 0
worker/docker_test.go

@@ -8,6 +8,7 @@ import (
 	"path/filepath"
 	"testing"
 	"time"
+	units "github.com/docker/go-units"
 
 	"github.com/codeskyblue/go-sh"
 	. "github.com/smartystreets/goconvey/convey"
@@ -77,6 +78,7 @@ sleep 20
 			volumes: []string{
 				fmt.Sprintf("%s:%s", cmdScript, "/bin/cmd.sh"),
 			},
+			memoryLimit: 512 * units.MiB,
 		}
 		provider.AddHook(d)
 		So(provider.Docker(), ShouldNotBeNil)

+ 4 - 0
worker/runner.go

@@ -56,6 +56,10 @@ func newCmdJob(provider mirrorProvider, cmdAndArgs []string, workingDir string,
 			kv := fmt.Sprintf("%s=%s", k, v)
 			args = append(args, "-e", kv)
 		}
+		// set memlimit
+		if d.memoryLimit != 0 {
+		  args = append(args, "-m", fmt.Sprint(d.memoryLimit.Value()))
+		}
 		// apply options
 		args = append(args, d.options...)
 		// apply image and command