Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make containerd socket path configurable #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,14 @@ To interact with `images` and `containers` directly, you can use [`nerdctl`](htt

**Driver Config**

| Option | Type | Required | Default | Description |
| :---: | :---: | :---: | :---: | :--- |
| **enabled** | bool | no | true | Enable/Disable task driver. |
| **containerd_runtime** | string | yes | N/A | Runtime for containerd e.g. `io.containerd.runc.v1` or `io.containerd.runc.v2`. |
| **stats_interval** | string | no | 1s | Interval for collecting `TaskStats`. |
| **allow_privileged** | bool | no | true | If set to `false`, driver will deny running privileged jobs. |
| **auth** | block | no | N/A | Provide authentication for a private registry. See [Authentication](#authentication-private-registry) for more details. |
| Option | Type | Required | Default | Description |
|:----------------------:|:------:|:--------:|:---------------------------------:|:------------------------------------------------------------------------------------------------------------------------|
| **enabled** | bool | no | true | Enable/Disable task driver. |
| **containerd_address** | string | no | `/run/containerd/containerd.sock` | Path to containerd socket. |
| **containerd_runtime** | string | yes | N/A | Runtime for containerd e.g. `io.containerd.runc.v1` or `io.containerd.runc.v2`. |
| **stats_interval** | string | no | 1s | Interval for collecting `TaskStats`. |
| **allow_privileged** | bool | no | true | If set to `false`, driver will deny running privileged jobs. |
| **auth** | block | no | N/A | Provide authentication for a private registry. See [Authentication](#authentication-private-registry) for more details. |

**Task Config**

Expand Down
26 changes: 16 additions & 10 deletions containerd/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/containerd/containerd"
"github.com/containerd/containerd/defaults"
"github.com/containerd/containerd/namespaces"
"github.com/hashicorp/consul-template/signals"
"github.com/hashicorp/go-hclog"
Expand Down Expand Up @@ -79,6 +80,10 @@ var (
hclspec.NewAttr("enabled", "bool", false),
hclspec.NewLiteral("true"),
),
"containerd_address": hclspec.NewDefault(
hclspec.NewAttr("containerd_address", "string", false),
hclspec.NewLiteral(defaults.DefaultAddress),
),
"containerd_runtime": hclspec.NewAttr("containerd_runtime", "string", true),
"stats_interval": hclspec.NewAttr("stats_interval", "string", false),
"allow_privileged": hclspec.NewDefault(
Expand Down Expand Up @@ -152,6 +157,7 @@ var (
// Config contains configuration information for the plugin
type Config struct {
Enabled bool `codec:"enabled"`
ContainerdAddress string `codec:"containerd_address"`
ContainerdRuntime string `codec:"containerd_runtime"`
StatsInterval string `codec:"stats_interval"`
AllowPrivileged bool `codec:"allow_privileged"`
Expand Down Expand Up @@ -249,14 +255,6 @@ func NewPlugin(logger log.Logger) drivers.DriverPlugin {
ctx, cancel := context.WithCancel(context.Background())
logger = logger.Named(PluginName)

// This will create a new containerd client which will talk to
// default containerd socket path.
client, err := containerd.New("/run/containerd/containerd.sock")
if err != nil {
logger.Error("Error in creating containerd client", "err", err)
return nil
}

// Calls to containerd API are namespaced.
// "nomad" is the namespace that will be used for all nomad-driver-containerd
// related containerd API calls.
Expand All @@ -274,7 +272,6 @@ func NewPlugin(logger log.Logger) drivers.DriverPlugin {
tasks: newTaskStore(),
ctx: ctx,
ctxContainerd: ctxContainerd,
client: client,
signalShutdown: cancel,
logger: logger,
}
Expand Down Expand Up @@ -324,12 +321,21 @@ func (d *Driver) ConfigSchema() (*hclspec.Spec, error) {
// SetConfig is called by the client to pass the configuration for the plugin.
func (d *Driver) SetConfig(cfg *base.Config) error {
var config Config
var err error
if len(cfg.PluginConfig) != 0 {
if err := base.MsgPackDecode(cfg.PluginConfig, &config); err != nil {
if err = base.MsgPackDecode(cfg.PluginConfig, &config); err != nil {
return err
}
}

// This will create a new containerd client which will talk to
// default containerd socket path.
d.client, err = containerd.New(config.ContainerdAddress)
if err != nil {
d.logger.Error("Error in creating containerd client", "err", err)
return err
}

// Save the configuration to the plugin
d.config = &config

Expand Down