-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Advertise driver-specific addresses #2709
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3fddb05
Implement DriverNetwork and Service.AddressMode
schmichael 8a7df57
Test driver network advertisement and checks
schmichael 4117c8b
Fix Service.AddressMode changes during task updates
schmichael 57b6f6e
Consolidate envvars in a partial template
schmichael ee5e02f
Document address_mode
schmichael cda2db2
Skip DRIVER env vars for labels without a port mapping
schmichael 8ed23d4
Fix lxc tests
schmichael a464ca3
Remove debug logging
schmichael e6552e1
Add script to demo weave in vagrant
schmichael 2023946
Remove readme
schmichael 81b942e
Bump error log level
schmichael deffe5b
Simplify Docker Networks processing
schmichael 3f37be3
Move caonicalization from nomad/structs/ to api/
schmichael b5fdf1d
Have Qemu return PortMap
schmichael 5d7fba4
Remove DRIVER env vars
schmichael fe3bb07
Fixup example
schmichael 9ef4a42
Remove DRIVER env vars from docs
schmichael a70ad9b
Style and comments
schmichael cc29d74
Remove defunct DRIVER references in docs
schmichael 8c32582
Fix some tests still expecting reverted behavior
schmichael 85860dc
Fix test error formats
schmichael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -471,7 +471,7 @@ func (d *DockerDriver) Prestart(ctx *ExecContext, task *structs.Task) (*Prestart | |
return nil, err | ||
} | ||
|
||
// Set state needed by Start() | ||
// Set state needed by Start | ||
d.driverConfig = driverConfig | ||
|
||
// Initialize docker API clients | ||
|
@@ -485,15 +485,21 @@ func (d *DockerDriver) Prestart(ctx *ExecContext, task *structs.Task) (*Prestart | |
if err != nil { | ||
return nil, err | ||
} | ||
d.imageID = id | ||
|
||
resp := NewPrestartResponse() | ||
resp.CreatedResources.Add(dockerImageResKey, id) | ||
resp.PortMap = d.driverConfig.PortMap | ||
d.imageID = id | ||
|
||
// Return the PortMap if it's set | ||
if len(driverConfig.PortMap) > 0 { | ||
resp.Network = &cstructs.DriverNetwork{ | ||
PortMap: driverConfig.PortMap, | ||
} | ||
} | ||
return resp, nil | ||
} | ||
|
||
func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle, error) { | ||
func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (*StartResponse, error) { | ||
|
||
pluginLogFile := filepath.Join(ctx.TaskDir.Dir, "executor.out") | ||
executorConfig := &dstructs.ExecutorConfig{ | ||
|
@@ -560,6 +566,15 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle | |
pluginClient.Kill() | ||
return nil, fmt.Errorf("Failed to start container %s: %s", container.ID, err) | ||
} | ||
// InspectContainer to get all of the container metadata as | ||
// much of the metadata (eg networking) isn't populated until | ||
// the container is started | ||
if container, err = client.InspectContainer(container.ID); err != nil { | ||
err = fmt.Errorf("failed to inspect started container %s: %s", container.ID, err) | ||
d.logger.Printf("[ERR] driver.docker: %v", err) | ||
pluginClient.Kill() | ||
return nil, structs.NewRecoverableError(err, true) | ||
} | ||
d.logger.Printf("[INFO] driver.docker: started container %s", container.ID) | ||
} else { | ||
d.logger.Printf("[DEBUG] driver.docker: re-attaching to container %s with status %q", | ||
|
@@ -585,7 +600,58 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle | |
} | ||
go h.collectStats() | ||
go h.run() | ||
return h, nil | ||
|
||
// Detect container address | ||
ip, autoUse := d.detectIP(container) | ||
|
||
// Create a response with the driver handle and container network metadata | ||
resp := &StartResponse{ | ||
Handle: h, | ||
Network: &cstructs.DriverNetwork{ | ||
PortMap: d.driverConfig.PortMap, | ||
IP: ip, | ||
AutoAdvertise: autoUse, | ||
}, | ||
} | ||
return resp, nil | ||
} | ||
|
||
// detectIP of Docker container. Returns the first IP found as well as true if | ||
// the IP should be advertised (bridge network IPs return false). Returns an | ||
// empty string and false if no IP could be found. | ||
func (d *DockerDriver) detectIP(c *docker.Container) (string, bool) { | ||
if c.NetworkSettings == nil { | ||
// This should only happen if there's been a coding error (such | ||
// as not calling InspetContainer after CreateContainer). Code | ||
// defensively in case the Docker API changes subtly. | ||
d.logger.Printf("[ERROR] driver.docker: no network settings for container %s", c.ID) | ||
return "", false | ||
} | ||
|
||
ip, ipName := "", "" | ||
auto := false | ||
for name, net := range c.NetworkSettings.Networks { | ||
if net.IPAddress == "" { | ||
// Ignore networks without an IP address | ||
continue | ||
} | ||
|
||
ip = net.IPAddress | ||
ipName = name | ||
|
||
// Don't auto-advertise bridge IPs | ||
if name != "bridge" { | ||
auto = true | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just break? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
break | ||
} | ||
|
||
if n := len(c.NetworkSettings.Networks); n > 1 { | ||
d.logger.Printf("[WARN] driver.docker: multiple (%d) Docker networks for container %q but Nomad only supports 1: choosing %q", n, c.ID, ipName) | ||
} | ||
|
||
return ip, auto | ||
} | ||
|
||
func (d *DockerDriver) Cleanup(_ *ExecContext, res *CreatedResources) error { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Canonicalize below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added