Skip to content

Commit

Permalink
[WIP] enable compatibility with Nomad in cgroups.v2 mode
Browse files Browse the repository at this point in the history
This PR updates the containerd driver to support changes in how Nomad
manages cgroups when running on a machine using cgroups.v2

- The namespace is now set to "nomad.slice", which containerd uses as
  the cgroup parent.

- The container name is re-oriented to the new naming convention,
  i.e. "<allocID>.<taskName>.scope". This is necessary for Nomad to
  be able to manage the cpuset resource.
  • Loading branch information
shoenig committed Mar 12, 2022
1 parent cbe53a9 commit 34a9526
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion containerd/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/hashicorp/nomad/plugins/drivers"
"github.com/hashicorp/nomad/plugins/shared/hclspec"
"github.com/hashicorp/nomad/plugins/shared/structs"
"github.com/opencontainers/runc/libcontainer/cgroups"
)

const (
Expand Down Expand Up @@ -259,7 +260,13 @@ func NewPlugin(logger log.Logger) drivers.DriverPlugin {
// Calls to containerd API are namespaced.
// "nomad" is the namespace that will be used for all nomad-driver-containerd
// related containerd API calls.
ctxContainerd := namespaces.WithNamespace(context.Background(), "nomad")
namespace := "nomad"
// Unless we are operating in cgroups.v2 mode, in which case we use the
// name "nomad.slice", which ends up being the cgroup parent.
if cgroups.IsCgroup2UnifiedMode() {
namespace = "nomad.slice"
}
ctxContainerd := namespaces.WithNamespace(context.Background(), namespace)

return &Driver{
eventer: eventer.NewEventer(ctx, logger),
Expand Down Expand Up @@ -428,8 +435,13 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive
handle := drivers.NewTaskHandle(taskHandleVersion)
handle.Config = cfg

// Use Nomad's docker naming convention for the container name
// https://www.nomadproject.io/docs/drivers/docker#container-name
containerName := cfg.Name + "-" + cfg.AllocID
if cgroups.IsCgroup2UnifiedMode() {
// In cgroup.v2 mode, the name is slightly different.
containerName = fmt.Sprintf("%s.%s.scope", cfg.AllocID, cfg.Name)
}
containerConfig.ContainerName = containerName

var err error
Expand Down

0 comments on commit 34a9526

Please sign in to comment.