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"
|
|
|
|
|
2022-08-17 16:26:34 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
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) {
|
client: enable support for cgroups v2
This PR introduces support for using Nomad on systems with cgroups v2 [1]
enabled as the cgroups controller mounted on /sys/fs/cgroups. Newer Linux
distros like Ubuntu 21.10 are shipping with cgroups v2 only, causing problems
for Nomad users.
Nomad mostly "just works" with cgroups v2 due to the indirection via libcontainer,
but not so for managing cpuset cgroups. Before, Nomad has been making use of
a feature in v1 where a PID could be a member of more than one cgroup. In v2
this is no longer possible, and so the logic around computing cpuset values
must be modified. When Nomad detects v2, it manages cpuset values in-process,
rather than making use of cgroup heirarchy inheritence via shared/reserved
parents.
Nomad will only activate the v2 logic when it detects cgroups2 is mounted at
/sys/fs/cgroups. This means on systems running in hybrid mode with cgroups2
mounted at /sys/fs/cgroups/unified (as is typical) Nomad will continue to
use the v1 logic, and should operate as before. Systems that do not support
cgroups v2 are also not affected.
When v2 is activated, Nomad will create a parent called nomad.slice (unless
otherwise configured in Client conifg), and create cgroups for tasks using
naming convention <allocID>-<task>.scope. These follow the naming convention
set by systemd and also used by Docker when cgroups v2 is detected.
Client nodes now export a new fingerprint attribute, unique.cgroups.version
which will be set to 'v1' or 'v2' to indicate the cgroups regime in use by
Nomad.
The new cpuset management strategy fixes #11705, where docker tasks that
spawned processes on startup would "leak". In cgroups v2, the PIDs are
started in the cgroup they will always live in, and thus the cause of
the leak is eliminated.
[1] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
Closes #11289
Fixes #11705 #11773 #11933
2022-02-28 22:24:01 +00:00
|
|
|
// Start docker reconcilers when we start fingerprinting, a workaround for
|
|
|
|
// task drivers not having a kind of post-setup hook.
|
|
|
|
d.danglingReconciler.Start()
|
|
|
|
d.cpusetFixer.Start()
|
2019-10-18 18:35:15 +00:00
|
|
|
|
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()
|
2022-08-17 16:26:34 +00:00
|
|
|
d.fingerprintSuccess = pointer.Of(true)
|
2019-01-14 21:33:42 +00:00
|
|
|
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()
|
2022-08-17 16:26:34 +00:00
|
|
|
d.fingerprintSuccess = pointer.Of(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)
|
2020-03-26 15:00:53 +00:00
|
|
|
|
2020-03-26 15:02:20 +00:00
|
|
|
ticker := time.NewTimer(0)
|
2020-03-30 20:11:42 +00:00
|
|
|
defer ticker.Stop()
|
2020-03-26 15:00:53 +00:00
|
|
|
|
2018-11-16 18:52:54 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-21 18:31:34 +00:00
|
|
|
if d.config.PidsLimit > 0 {
|
|
|
|
fp.Attributes["driver.docker.pids.limit"] = pstructs.NewIntAttribute(d.config.PidsLimit, "")
|
|
|
|
}
|
|
|
|
|
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)
|
2020-12-08 20:47:04 +00:00
|
|
|
} else if d.fingerprintSuccess == nil {
|
2018-11-16 18:52:54 +00:00
|
|
|
// Docker 17.09.0-ce dropped the Gateway IP from the bridge network
|
|
|
|
// See https://github.com/moby/moby/issues/32648
|
2020-12-08 20:47:04 +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)
|
|
|
|
|
2020-05-04 17:08:47 +00:00
|
|
|
// If this situations arises, we are running in Windows 10 with Linux Containers enabled via VM
|
2019-02-27 13:10:23 +00:00
|
|
|
if runtime.GOOS == "windows" && dockerInfo.OSType == "linux" {
|
|
|
|
if d.fingerprintSuccessful() {
|
2020-05-04 17:08:47 +00:00
|
|
|
d.logger.Warn("Docker is configured with Linux containers; switch to Windows Containers")
|
2019-02-27 13:10:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
d.setFingerprintFailure()
|
|
|
|
return &drivers.Fingerprint{
|
|
|
|
Health: drivers.HealthStateUnhealthy,
|
2020-05-04 17:08:47 +00:00
|
|
|
HealthDescription: "Docker is configured with Linux containers; switch to Windows Containers",
|
2019-02-27 13:10:23 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|