Skip to content

Commit

Permalink
Adjust for different plugin name
Browse files Browse the repository at this point in the history
  • Loading branch information
sorenisanerd committed Jun 23, 2023
1 parent 6e6aa21 commit 9685fc8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 39 deletions.
40 changes: 21 additions & 19 deletions docker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
ContainerNotRunningError = "Container not running"

// pluginName is the name of the plugin
pluginName = "docker"
pluginName = "docker-ext"

// fingerprintPeriod is the interval at which the driver will send fingerprint responses
fingerprintPeriod = 30 * time.Second
Expand All @@ -45,69 +45,71 @@ const (
dockerAuthHelperPrefix = "docker-credential-"
)

func prefixOption(opt string) string {
return fmt.Sprintf("%s.%s", pluginName, opt)
}

func PluginLoader(opts map[string]string) (map[string]interface{}, error) {
conf := map[string]interface{}{}
if v, ok := opts["docker.endpoint"]; ok {
if v, ok := opts[prefixOption("endpoint")]; ok {
conf["endpoint"] = v
}

// dockerd auth
authConf := map[string]interface{}{}
if v, ok := opts["docker.auth.config"]; ok {
if v, ok := opts[prefixOption("auth.config")]; ok {
authConf["config"] = v
}
if v, ok := opts["docker.auth.helper"]; ok {
if v, ok := opts[prefixOption("auth.helper")]; ok {
authConf["helper"] = v
}
conf["auth"] = authConf

// dockerd tls
if _, ok := opts["docker.tls.cert"]; ok {
if _, ok := opts[prefixOption("tls.cert")]; ok {
conf["tls"] = map[string]interface{}{
"cert": opts["docker.tls.cert"],
"key": opts["docker.tls.key"],
"ca": opts["docker.tls.ca"],
"cert": opts[prefixOption("tls.cert")],
"key": opts[prefixOption("tls.key")],
"ca": opts[prefixOption("tls.ca")],
}
}

// garbage collection
gcConf := map[string]interface{}{}
if v, err := strconv.ParseBool(opts["docker.cleanup.image"]); err == nil {
if v, err := strconv.ParseBool(opts[prefixOption("cleanup.image")]); err == nil {
gcConf["image"] = v
}
if v, ok := opts["docker.cleanup.image.delay"]; ok {
if v, ok := opts[prefixOption("cleanup.image.delay")]; ok {
gcConf["image_delay"] = v
}
if v, err := strconv.ParseBool(opts["docker.cleanup.container"]); err == nil {
if v, err := strconv.ParseBool(opts[prefixOption("cleanup.container")]); err == nil {
gcConf["container"] = v
}
conf["gc"] = gcConf

// volume options
volConf := map[string]interface{}{}
if v, err := strconv.ParseBool(opts["docker.volumes.enabled"]); err == nil {
if v, err := strconv.ParseBool(opts[prefixOption("volumes.enabled")]); err == nil {
volConf["enabled"] = v
}
if v, ok := opts["docker.volumes.selinuxlabel"]; ok {
if v, ok := opts[prefixOption("volumes.selinuxlabel")]; ok {
volConf["selinuxlabel"] = v
}
conf["volumes"] = volConf

// capabilities
// COMPAT(1.0) uses inclusive language. whitelist is used for backward compatibility.
if v, ok := opts["docker.caps.allowlist"]; ok {
conf["allow_caps"] = strings.Split(v, ",")
} else if v, ok := opts["docker.caps.whitelist"]; ok {
if v, ok := opts[prefixOption("caps.allowlist")]; ok {
conf["allow_caps"] = strings.Split(v, ",")
}

// privileged containers
if v, err := strconv.ParseBool(opts["docker.privileged.enabled"]); err == nil {
if v, err := strconv.ParseBool(opts[prefixOption("privileged.enabled")]); err == nil {
conf["allow_privileged"] = v
}

// nvidia_runtime
if v, ok := opts["docker.nvidia_runtime"]; ok {
if v, ok := opts[prefixOption("nvidia_runtime")]; ok {
conf["nvidia_runtime"] = v
}

Expand All @@ -124,7 +126,7 @@ var (
// PluginConfig is the docker config factory function registered in the plugin catalog.
PluginConfig = &loader.InternalPluginConfig{
Config: map[string]interface{}{},
Factory: func(ctx context.Context, l hclog.Logger) interface{} { return NewDockerDriver(ctx, l) },
Factory: func(ctx context.Context, l hclog.Logger) interface{} { return NewDockerDriver(l) },
}

// pluginInfo is the response returned for the PluginInfo RPC.
Expand Down
3 changes: 2 additions & 1 deletion docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ type Driver struct {
}

// NewDockerDriver returns a docker implementation of a driver plugin
func NewDockerDriver(ctx context.Context, logger hclog.Logger) drivers.DriverPlugin {
func NewDockerDriver(logger hclog.Logger) drivers.DriverPlugin {
ctx, _ := context.WithCancel(context.Background())
logger = logger.Named(pluginName)
driver := &Driver{
eventer: eventer.NewEventer(ctx, logger),
Expand Down
4 changes: 1 addition & 3 deletions docker/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ func cleanSlate(client *docker.Client, imageID string) {
// A driver plugin interface and cleanup function is returned
func dockerDriverHarness(t *testing.T, cfg map[string]interface{}) *dtestutil.DriverHarness {
logger := testlog.HCLogger(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() { cancel() })
harness := dtestutil.NewDriverHarness(t, NewDockerDriver(ctx, logger))
harness := dtestutil.NewDriverHarness(t, NewDockerDriver(logger))
if cfg == nil {
cfg = map[string]interface{}{
"gc": map[string]interface{}{
Expand Down
19 changes: 12 additions & 7 deletions docker/fingerprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package docker

import (
"context"
"fmt"
"runtime"
"sort"
"strings"
Expand Down Expand Up @@ -83,6 +84,10 @@ func (d *Driver) handleFingerprint(ctx context.Context, ch chan *drivers.Fingerp
}
}

func prefixAttribute(attr string) string {
return fmt.Sprintf("driver.%s.%s", pluginName, attr)
}

func (d *Driver) buildFingerprint() *drivers.Fingerprint {
fp := &drivers.Fingerprint{
Attributes: make(map[string]*pstructs.Attribute, 8),
Expand Down Expand Up @@ -130,17 +135,17 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {

d.setDetected(true)
fp.Attributes["driver.docker"] = pstructs.NewBoolAttribute(true)
fp.Attributes["driver.docker.version"] = pstructs.NewStringAttribute(env.Get("Version"))
fp.Attributes[prefixAttribute("version")] = pstructs.NewStringAttribute(env.Get("Version"))
if d.config.AllowPrivileged {
fp.Attributes["driver.docker.privileged.enabled"] = pstructs.NewBoolAttribute(true)
fp.Attributes[prefixAttribute("privileged.enabled")] = pstructs.NewBoolAttribute(true)
}

if d.config.PidsLimit > 0 {
fp.Attributes["driver.docker.pids.limit"] = pstructs.NewIntAttribute(d.config.PidsLimit, "")
fp.Attributes[prefixAttribute("pids.limit")] = pstructs.NewIntAttribute(d.config.PidsLimit, "")
}

if d.config.Volumes.Enabled {
fp.Attributes["driver.docker.volumes.enabled"] = pstructs.NewBoolAttribute(true)
fp.Attributes[prefixAttribute("volumes.enabled")] = pstructs.NewBoolAttribute(true)
}

if nets, err := client.ListNetworks(); err != nil {
Expand All @@ -157,7 +162,7 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
}

if n.IPAM.Config[0].Gateway != "" {
fp.Attributes["driver.docker.bridge_ip"] = pstructs.NewStringAttribute(n.IPAM.Config[0].Gateway)
fp.Attributes[prefixAttribute("bridge_ip")] = pstructs.NewStringAttribute(n.IPAM.Config[0].Gateway)
} else if d.fingerprintSuccess == nil {
// Docker 17.09.0-ce dropped the Gateway IP from the bridge network
// See https://github.com/moby/moby/issues/32648
Expand All @@ -181,9 +186,9 @@ func (d *Driver) buildFingerprint() *drivers.Fingerprint {
}
sort.Strings(runtimeNames)

fp.Attributes["driver.docker.runtimes"] = pstructs.NewStringAttribute(
fp.Attributes[prefixAttribute("runtimes")] = pstructs.NewStringAttribute(
strings.Join(runtimeNames, ","))
fp.Attributes["driver.docker.os_type"] = pstructs.NewStringAttribute(dockerInfo.OSType)
fp.Attributes[prefixAttribute("os_type")] = pstructs.NewStringAttribute(dockerInfo.OSType)

// If this situations arises, we are running in Windows 10 with Linux Containers enabled via VM
if runtime.GOOS == "windows" && dockerInfo.OSType == "linux" {
Expand Down
11 changes: 2 additions & 9 deletions docker/fingerprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package docker

import (
"context"
"testing"

"github.com/hashicorp/nomad/ci"
Expand All @@ -22,10 +21,7 @@ func TestDockerDriver_FingerprintHealth(t *testing.T) {
ci.Parallel(t)
testutil.DockerCompatible(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

d := NewDockerDriver(ctx, testlog.HCLogger(t)).(*Driver)
d := NewDockerDriver(testlog.HCLogger(t)).(*Driver)

fp := d.buildFingerprint()
must.Eq(t, drivers.HealthStateHealthy, fp.Health)
Expand All @@ -39,10 +35,7 @@ func TestDockerDriver_NonRoot_CGV2(t *testing.T) {
testutil.CgroupsCompatibleV2(t)
testutil.RequireNonRoot(t)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

d := NewDockerDriver(ctx, testlog.HCLogger(t)).(*Driver)
d := NewDockerDriver(testlog.HCLogger(t)).(*Driver)

fp := d.buildFingerprint()
must.Eq(t, drivers.HealthStateUndetected, fp.Health)
Expand Down

0 comments on commit 9685fc8

Please sign in to comment.