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

Add driver.docker.bridge_ip node attribute #2797

Merged
merged 4 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ IMPROVEMENTS:
* driver/docker: Allow setting seccomp profiles [GH-2658]
* driver/docker: Support Docker credential helpers [GH-2651]
* driver/docker: Auth failures can optionally be ignored [GH-2786]
* driver/docker: Add `driver.docker.bridge_ip` node attribute [GH-2797]
* driver/docker: Allow setting container IP with user defined networks
[GH-2535]
* driver/rkt: Support `no_overlay` [GH-2702]
Expand Down
21 changes: 21 additions & 0 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ func (d *DockerDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool
d.fingerprintSuccess = helper.BoolToPtr(false)
return false, nil
}
for i, v := range *env {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From debugging?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my good catch. I really need to scan for that stuff better

d.logger.Printf("[DEBUG] driver.docker: %d %s", i, v)
}

node.Attributes[dockerDriverAttr] = "1"
node.Attributes["driver.docker.version"] = env.Get("Version")
Expand All @@ -333,6 +336,24 @@ func (d *DockerDriver) Fingerprint(cfg *config.Config, node *structs.Node) (bool
node.Attributes["driver."+dockerVolumesConfigOption] = "1"
}

// Detect bridge IP address - #2785
if nets, err := client.ListNetworks(); err != nil {
d.logger.Printf("[WARN] driver.docker: error discovering bridge IP: %v", err)
} else {
for _, n := range nets {
if n.Name != "bridge" {
continue
}

if len(n.IPAM.Config) == 0 {
d.logger.Printf("[WARN] driver.docker: no IPAM config for bridge network")
break
}

node.Attributes["driver.docker.bridge_ip"] = n.IPAM.Config[0].Gateway
}
}

d.fingerprintSuccess = helper.BoolToPtr(true)
return true, nil
}
Expand Down
35 changes: 35 additions & 0 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

docker "github.com/fsouza/go-dockerclient"
sockaddr "github.com/hashicorp/go-sockaddr"
"github.com/hashicorp/nomad/client/allocdir"
"github.com/hashicorp/nomad/client/config"
"github.com/hashicorp/nomad/client/driver/env"
Expand Down Expand Up @@ -177,6 +178,40 @@ func TestDockerDriver_Fingerprint(t *testing.T) {
t.Logf("Found docker version %s", node.Attributes["driver.docker.version"])
}

// TestDockerDriver_Fingerprint_Bridge asserts that if Docker is running we set
// the bridge network's IP as a node attribute. See #2785
func TestDockerDriver_Fingerprint_Bridge(t *testing.T) {
if !testutil.DockerIsConnected(t) {
t.Skip("requires Docker")
}

// This seems fragile, so we might need to reconsider this test if it
// proves flaky
expectedAddr, err := sockaddr.GetInterfaceIP("docker0")
if err != nil {
t.Fatalf("unable to get ip for docker0: %v", err)
}
if expectedAddr == "" {
t.Fatalf("unable to get ip for docker bridge")
}

conf := testConfig()
conf.Node = mock.Node()
dd := NewDockerDriver(NewDriverContext("", "", conf, conf.Node, testLogger(), nil))
ok, err := dd.Fingerprint(conf, conf.Node)
if err != nil {
t.Fatalf("error fingerprinting docker: %v", err)
}
if !ok {
t.Fatalf("expected Docker to be enabled but false was returned")
}

if found := conf.Node.Attributes["driver.docker.bridge_ip"]; found != expectedAddr {
t.Fatalf("expected bridge ip %q but found: %q", expectedAddr, found)
}
t.Logf("docker bridge ip: %q", conf.Node.Attributes["driver.docker.bridge_ip"])
}

func TestDockerDriver_StartOpen_Wait(t *testing.T) {
if !testutil.DockerIsConnected(t) {
t.SkipNow()
Expand Down
2 changes: 2 additions & 0 deletions website/source/docs/drivers/docker.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,8 @@ The `docker` driver will set the following client attributes:

* `driver.docker` - This will be set to "1", indicating the driver is
available.
* `driver.docker.bridge_ip` - The IP of the Docker bridge network if one
exists.
* `driver.docker.version` - This will be set to version of the docker server.

Here is an example of using these properties in a job file:
Expand Down