Skip to content

Commit

Permalink
Merge pull request moby#2087 from fcrisciani/join-flag
Browse files Browse the repository at this point in the history
Add an explicit flag to join network in diagnostic
  • Loading branch information
Flavio Crisciani authored Feb 23, 2018
2 parents daa47e8 + 47c28fe commit 9cdb30e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmd/diagnostic/Dockerfile.dind
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM docker:17.12-dind
RUN apk add --no-cache curl
ENV DIND_CLIENT=true
COPY daemon.json /etc/docker/daemon.json
COPY diagnosticClient /usr/local/bin/diagnosticClient
14 changes: 12 additions & 2 deletions cmd/diagnostic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,18 @@ The following flags are supported:
| -ip <string> | The IP address to query. Defaults to 127.0.0.1. |
| -net <string> | The target network ID. |
| -port <int> | The target port. (default port is 2000) |
| -a | Join/leave network |
| -v | Enable verbose output. |

*NOTE*
By default the tool won't try to join the network. This is following the intent to not change
the state on which the node is when the diagnostic client is run. This means that it is safe
to run the diagnosticClient against a running daemon because it will just dump the current state.
When using instead the diagnosticClient in the containerized version the flag `-a` MUST be passed
to avoid retrieving empty results. On the other side using the `-a` flag against a loaded daemon
will have the undesirable side effect to leave the network and so cutting down the data path for
that daemon.
### Container version of the diagnostic tool
The CLI is provided as a container with a 17.12 engine that needs to run using privileged mode.
Expand Down Expand Up @@ -242,11 +252,11 @@ Remember to use the full network ID, you can easily find that with `docker netwo
**Service discovery and load balancer:**
```bash
$ diagnostiClient -c sd -v -net n8a8ie6tb3wr2e260vxj8ncy4
$ diagnostiClient -c sd -v -net n8a8ie6tb3wr2e260vxj8ncy4 -a
```
**Overlay network:**
```bash
$ diagnostiClient -port 2001 -c overlay -v -net n8a8ie6tb3wr2e260vxj8ncy4
$ diagnostiClient -port 2001 -c overlay -v -net n8a8ie6tb3wr2e260vxj8ncy4 -a
```
32 changes: 25 additions & 7 deletions cmd/diagnostic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func main() {
networkPtr := flag.String("net", "", "target network")
tablePtr := flag.String("t", "", "table to process <sd/overlay>")
remediatePtr := flag.Bool("r", false, "perform remediation deleting orphan entries")
joinPtr := flag.Bool("a", false, "join/leave network")
verbosePtr := flag.Bool("v", false, "verbose output")

flag.Parse()
Expand All @@ -53,6 +54,11 @@ func main() {
logrus.SetLevel(logrus.DebugLevel)
}

if _, ok := os.LookupEnv("DIND_CLIENT"); !ok && *joinPtr {
logrus.Fatal("you are not using the client in docker in docker mode, the use of the -a flag can be disruptive, " +
"please remove it (doc:https://github.com/docker/libnetwork/blob/master/cmd/diagnostic/README.md)")
}

logrus.Infof("Connecting to %s:%d checking ready", *ipPtr, *portPtr)
resp, err := http.Get(fmt.Sprintf(readyPath, *ipPtr, *portPtr))
if err != nil {
Expand All @@ -64,14 +70,20 @@ func main() {
var networkPeers map[string]string
var joinedNetwork bool
if *networkPtr != "" {
logrus.Infof("Joining the network:%s", *networkPtr)
resp, err = http.Get(fmt.Sprintf(joinNetwork, *ipPtr, *portPtr, *networkPtr))
if err != nil {
logrus.WithError(err).Fatalf("Failed joining the network")
if *joinPtr {
logrus.Infof("Joining the network:%q", *networkPtr)
resp, err = http.Get(fmt.Sprintf(joinNetwork, *ipPtr, *portPtr, *networkPtr))
if err != nil {
logrus.WithError(err).Fatalf("Failed joining the network")
}
httpIsOk(resp.Body)
joinedNetwork = true
}
httpIsOk(resp.Body)

networkPeers = fetchNodePeers(*ipPtr, *portPtr, *networkPtr)
joinedNetwork = true
if len(networkPeers) == 0 {
logrus.Warnf("There is no peer on network %q, check the network ID, and verify that is the non truncated version", *networkPtr)
}
}

switch *tablePtr {
Expand All @@ -82,6 +94,7 @@ func main() {
}

if joinedNetwork {
logrus.Infof("Leaving the network:%q", *networkPtr)
resp, err = http.Get(fmt.Sprintf(leaveNetwork, *ipPtr, *portPtr, *networkPtr))
if err != nil {
logrus.WithError(err).Fatalf("Failed leaving the network")
Expand All @@ -91,7 +104,12 @@ func main() {
}

func fetchNodePeers(ip string, port int, network string) map[string]string {
logrus.Infof("Fetch peers %s", network)
if network == "" {
logrus.Infof("Fetch cluster peers")
} else {
logrus.Infof("Fetch peers network:%q", network)
}

var path string
if network != "" {
path = fmt.Sprintf(networkPeers, ip, port, network)
Expand Down

0 comments on commit 9cdb30e

Please sign in to comment.