-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enumerate docker network namespaces a different way (#11)
Should solve many of the issues where free-ips were not located correctly in Docker. Also cleans up some of the nl/desc logic repetition.
- Loading branch information
Showing
4 changed files
with
85 additions
and
75 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package nl | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/docker/docker/api/types" | ||
"github.com/docker/docker/client" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
// With a heavy heart and deep internal sadness, we support Docker | ||
func runningDockerContainers() (containerIDs []string, err error) { | ||
cli, err := client.NewEnvClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer cli.Close() | ||
|
||
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, container := range containers { | ||
containerIDs = append(containerIDs, container.ID) | ||
} | ||
return containerIDs, nil | ||
} | ||
|
||
func dockerNetworkNamespace(containerID string) (string, error) { | ||
cli, err := client.NewEnvClient() | ||
if err != nil { | ||
return "", err | ||
} | ||
defer cli.Close() | ||
r, err := cli.ContainerInspect(context.Background(), containerID) | ||
if err != nil { | ||
return "", nil | ||
} | ||
|
||
if r.State.Pid == 0 { | ||
// Container has exited | ||
return "", fmt.Errorf("Container has exited %v", containerID) | ||
} | ||
return fmt.Sprintf("/proc/%v/ns/net", r.State.Pid), nil | ||
} | ||
|
||
// Retrieve all namespaces from all running docker containers | ||
func dockerNetworkNamespaces(containerIDs []string) (namespaces []string) { | ||
for _, containerID := range containerIDs { | ||
cid, err := dockerNetworkNamespace(containerID) | ||
if err == nil && len(cid) > 0 { | ||
namespaces = append(namespaces, cid) | ||
} | ||
} | ||
return | ||
} |