Merge pull request #44 from Roblox/cgroups

Apply memory cgroups to the container.
This commit is contained in:
Charles Z 2020-09-25 17:01:41 -07:00 committed by GitHub
commit 22f5ed4240
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 2 deletions

View File

@ -41,7 +41,7 @@ func (d *Driver) pullImage(imageName string) (containerd.Image, error) {
return d.client.Pull(d.ctxContainerd, imageName, containerd.WithPullUnpack) return d.client.Pull(d.ctxContainerd, imageName, containerd.WithPullUnpack)
} }
func (d *Driver) createContainer(image containerd.Image, containerName, containerSnapshotName, containerdRuntime, netnsPath, secretsDir, taskDir, allocDir string, env []string, config *TaskConfig) (containerd.Container, error) { func (d *Driver) createContainer(image containerd.Image, containerName, containerSnapshotName, containerdRuntime, netnsPath, secretsDir, taskDir, allocDir string, env []string, memoryLimit int64, config *TaskConfig) (containerd.Container, error) {
if config.Command == "" && len(config.Args) > 0 { if config.Command == "" && len(config.Args) > 0 {
return nil, fmt.Errorf("Command is empty. Cannot set --args without --command.") return nil, fmt.Errorf("Command is empty. Cannot set --args without --command.")
} }
@ -105,6 +105,9 @@ func (d *Driver) createContainer(image containerd.Image, containerName, containe
// Set environment variables. // Set environment variables.
opts = append(opts, oci.WithEnv(env)) opts = append(opts, oci.WithEnv(env))
// Set cgroups memory limit.
opts = append(opts, oci.WithMemoryLimit(uint64(memoryLimit)))
// Add linux devices into the container. // Add linux devices into the container.
for _, device := range config.Devices { for _, device := range config.Devices {
opts = append(opts, oci.WithLinuxDevice(device, "rwm")) opts = append(opts, oci.WithLinuxDevice(device, "rwm"))

View File

@ -380,7 +380,7 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
netnsPath = cfg.NetworkIsolation.Path netnsPath = cfg.NetworkIsolation.Path
} }
container, err := d.createContainer(image, containerName, containerSnapshotName, d.config.ContainerdRuntime, netnsPath, secretsDir, taskDir, allocDir, env, &driverConfig) container, err := d.createContainer(image, containerName, containerSnapshotName, d.config.ContainerdRuntime, netnsPath, secretsDir, taskDir, allocDir, env, cfg.Resources.LinuxResources.MemoryLimitBytes, &driverConfig)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("Error in creating container: %v", err) return nil, nil, fmt.Errorf("Error in creating container: %v", err)
} }