2018-11-16 18:52:54 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-27 13:10:23 +00:00
|
|
|
"runtime"
|
2018-12-18 01:03:43 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2018-11-16 18:52:54 +00:00
|
|
|
"time"
|
|
|
|
|
2019-01-14 21:33:42 +00:00
|
|
|
"github.com/hashicorp/nomad/helper"
|
2018-11-16 18:52:54 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
2018-11-26 20:44:39 +00:00
|
|
|
pstructs "github.com/hashicorp/nomad/plugins/shared/structs"
|
2018-11-16 18:52:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (d *Driver) Fingerprint(ctx context.Context) (<-chan *drivers.Fingerprint, error) {
|
2019-10-18 18:35:15 +00:00
|
|
|
// start reconciler when we start fingerprinting
|
|
|
|
// this is the only method called when driver is launched properly
|
|
|
|
d.reconciler.Start()
|
|
|
|
|
2018-11-16 18:52:54 +00:00
|
|
|
ch := make(chan *drivers.Fingerprint)
|
|
|
|
go d.handleFingerprint(ctx, ch)
|
|
|
|
return ch, nil
|
|
|
|
}
|
|
|
|
|
2019-02-25 10:02:42 +00:00
|
|
|
func (d *Driver) previouslyDetected() bool {
|
|
|
|
d.detectedLock.RLock()
|
|
|
|
defer d.detectedLock.RUnlock()
|
|
|
|
|
|
|
|
return d.detected
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) setDetected(detected bool) {
|
|
|
|
d.detectedLock.Lock()
|
|
|
|
defer d.detectedLock.Unlock()
|
|
|
|
|
|
|
|
d.detected = detected
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:33:42 +00:00
|
|
|
// setFingerprintSuccess marks the driver as having fingerprinted successfully
|
|
|
|
func (d *Driver) setFingerprintSuccess() {
|
2019-01-11 17:50:46 +00:00
|
|
|
d.fingerprintLock.Lock()
|
2019-01-14 21:33:42 +00:00
|
|
|
d.fingerprintSuccess = helper.BoolToPtr(true)
|
|
|
|
d.fingerprintLock.Unlock()
|
2019-01-11 17:50:46 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 21:33:42 +00:00
|
|
|
// setFingerprintFailure marks the driver as having failed fingerprinting
|
|
|
|
func (d *Driver) setFingerprintFailure() {
|
2019-01-11 17:50:46 +00:00
|
|
|
d.fingerprintLock.Lock()
|
2019-01-14 21:33:42 +00:00
|
|
|
d.fingerprintSuccess = helper.BoolToPtr(false)
|
2019-01-11 17:50:46 +00:00
|
|
|
d.fingerprintLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-01-16 17:04:11 +00:00
|
|
|
// fingerprintSuccessful returns true if the driver has
|
|
|
|
// never fingerprinted or has successfully fingerprinted
|
|
|
|
func (d *Driver) fingerprintSuccessful() bool {
|
|
|
|
d.fingerprintLock.Lock()
|
|
|
|
defer d.fingerprintLock.Unlock()
|
|
|
|
return d.fingerprintSuccess == nil || *d.fingerprintSuccess
|
|
|
|
}
|
|
|
|
|
2018-11-16 18:52:54 +00:00
|
|
|
func (d *Driver) handleFingerprint(ctx context.Context, ch chan *drivers.Fingerprint) {
|
|
|
|
defer close(ch)
|
|
|
|
ticker := time.NewTimer(0)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-d.ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
ticker.Reset(fingerprintPeriod)
|
|
|
|
ch <- d.buildFingerprint()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Driver) buildFingerprint() *drivers.Fingerprint {
|
|
|
|
fp := &drivers.Fingerprint{
|
2018-11-26 20:44:39 +00:00
|
|
|
Attributes: map[string]*pstructs.Attribute{},
|
2018-11-16 18:52:54 +00:00
|
|
|
Health: drivers.HealthStateHealthy,
|
2019-01-07 04:04:15 +00:00
|
|
|
HealthDescription: drivers.DriverHealthy,
|
2018-11-16 18:52:54 +00:00
|
|
|
}
|
|
|
|
client, _, err := d.dockerClients()
|
|
|
|
if err != nil {
|
2019-01-16 17:04:11 +00:00
|
|
|
if d.fingerprintSuccessful() {
|
2019-01-11 17:50:46 +00:00
|
|
|
d.logger.Info("failed to initialize client", "error", err)
|
|
|
|
}
|
2019-01-14 21:33:42 +00:00
|
|
|
d.setFingerprintFailure()
|
2018-11-16 18:52:54 +00:00
|
|
|
return &drivers.Fingerprint{
|
|
|
|
Health: drivers.HealthStateUndetected,
|
2019-01-07 04:04:15 +00:00
|
|
|
HealthDescription: "Failed to initialize docker client",
|
2018-11-16 18:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
env, err := client.Version()
|
|
|
|
if err != nil {
|
2019-01-16 17:04:11 +00:00
|
|
|
if d.fingerprintSuccessful() {
|
2019-01-11 17:50:46 +00:00
|
|
|
d.logger.Debug("could not connect to docker daemon", "endpoint", client.Endpoint(), "error", err)
|
|
|
|
}
|
2019-01-14 21:33:42 +00:00
|
|
|
d.setFingerprintFailure()
|
2019-02-25 10:02:42 +00:00
|
|
|
|
|
|
|
result := drivers.HealthStateUndetected
|
|
|
|
if d.previouslyDetected() {
|
|
|
|
result = drivers.HealthStateUnhealthy
|
|
|
|
}
|
|
|
|
|
2018-11-16 18:52:54 +00:00
|
|
|
return &drivers.Fingerprint{
|
2019-02-25 10:02:42 +00:00
|
|
|
Health: result,
|
2019-01-07 04:04:15 +00:00
|
|
|
HealthDescription: "Failed to connect to docker daemon",
|
2018-11-16 18:52:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 10:02:42 +00:00
|
|
|
d.setDetected(true)
|
2018-11-26 20:44:39 +00:00
|
|
|
fp.Attributes["driver.docker"] = pstructs.NewBoolAttribute(true)
|
|
|
|
fp.Attributes["driver.docker.version"] = pstructs.NewStringAttribute(env.Get("Version"))
|
2018-11-16 18:52:54 +00:00
|
|
|
if d.config.AllowPrivileged {
|
2018-11-26 20:44:39 +00:00
|
|
|
fp.Attributes["driver.docker.privileged.enabled"] = pstructs.NewBoolAttribute(true)
|
2018-11-16 18:52:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 02:41:25 +00:00
|
|
|
if d.config.Volumes.Enabled {
|
2018-11-26 20:44:39 +00:00
|
|
|
fp.Attributes["driver.docker.volumes.enabled"] = pstructs.NewBoolAttribute(true)
|
2018-11-16 18:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if nets, err := client.ListNetworks(); err != nil {
|
|
|
|
d.logger.Warn("error discovering bridge IP", "error", err)
|
|
|
|
} else {
|
|
|
|
for _, n := range nets {
|
|
|
|
if n.Name != "bridge" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(n.IPAM.Config) == 0 {
|
|
|
|
d.logger.Warn("no IPAM config for bridge network")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if n.IPAM.Config[0].Gateway != "" {
|
2018-11-26 20:44:39 +00:00
|
|
|
fp.Attributes["driver.docker.bridge_ip"] = pstructs.NewStringAttribute(n.IPAM.Config[0].Gateway)
|
2018-11-16 18:52:54 +00:00
|
|
|
} else {
|
|
|
|
// Docker 17.09.0-ce dropped the Gateway IP from the bridge network
|
|
|
|
// See https://github.com/moby/moby/issues/32648
|
2019-01-25 03:34:57 +00:00
|
|
|
if d.fingerprintSuccess == nil {
|
2019-01-11 17:50:46 +00:00
|
|
|
d.logger.Debug("bridge_ip could not be discovered")
|
|
|
|
}
|
2018-11-16 18:52:54 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-18 01:03:43 +00:00
|
|
|
if dockerInfo, err := client.Info(); err != nil {
|
|
|
|
d.logger.Warn("failed to get Docker system info", "error", err)
|
|
|
|
} else {
|
|
|
|
runtimeNames := make([]string, 0, len(dockerInfo.Runtimes))
|
|
|
|
for name := range dockerInfo.Runtimes {
|
|
|
|
if d.config.GPURuntimeName == name {
|
|
|
|
// Nvidia runtime is detected by Docker.
|
|
|
|
// It makes possible to run GPU workloads using Docker driver on this host.
|
|
|
|
d.gpuRuntime = true
|
|
|
|
}
|
|
|
|
runtimeNames = append(runtimeNames, name)
|
|
|
|
}
|
|
|
|
sort.Strings(runtimeNames)
|
|
|
|
|
2019-01-23 18:52:23 +00:00
|
|
|
fp.Attributes["driver.docker.runtimes"] = pstructs.NewStringAttribute(
|
2018-12-18 01:03:43 +00:00
|
|
|
strings.Join(runtimeNames, ","))
|
2019-02-27 13:10:23 +00:00
|
|
|
fp.Attributes["driver.docker.os_type"] = pstructs.NewStringAttribute(dockerInfo.OSType)
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" && dockerInfo.OSType == "linux" {
|
|
|
|
if d.fingerprintSuccessful() {
|
|
|
|
d.logger.Warn("detected Linux docker containers on Windows; only Windows containers are supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
d.setFingerprintFailure()
|
|
|
|
return &drivers.Fingerprint{
|
|
|
|
Health: drivers.HealthStateUnhealthy,
|
|
|
|
HealthDescription: "Docker is configured with Linux containers; only Windows containers are supported",
|
|
|
|
}
|
|
|
|
}
|
2018-12-18 01:03:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 21:33:42 +00:00
|
|
|
d.setFingerprintSuccess()
|
|
|
|
|
2018-11-16 18:52:54 +00:00
|
|
|
return fp
|
|
|
|
}
|