Browse code

Add information for `Manager Addresses` in the output of `docker info`

As is specified in 28018, it would be useful to know the manager's addresses
even in a worker node. This is especially useful when there are many
worker nodes in a big cluster.

The information is available in `info.Swarm.RemoteManagers`.

This fix add the information of `Manager Addresses` to the output
of `docker info`, to explicitly show it.

A test has been added for this fix.

This fix fixes 28018.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>

Yong Tang authored on 2016/11/04 03:23:58
Showing 2 changed files
... ...
@@ -2,6 +2,7 @@ package system
2 2
 
3 3
 import (
4 4
 	"fmt"
5
+	"sort"
5 6
 	"strings"
6 7
 	"time"
7 8
 
... ...
@@ -131,6 +132,17 @@ func prettyPrintInfo(dockerCli *command.DockerCli, info types.Info) error {
131 131
 			}
132 132
 		}
133 133
 		fmt.Fprintf(dockerCli.Out(), " Node Address: %s\n", info.Swarm.NodeAddr)
134
+		managers := []string{}
135
+		for _, entry := range info.Swarm.RemoteManagers {
136
+			managers = append(managers, entry.Addr)
137
+		}
138
+		if len(managers) > 0 {
139
+			sort.Strings(managers)
140
+			fmt.Fprintf(dockerCli.Out(), " Manager Addresses:\n")
141
+			for _, entry := range managers {
142
+				fmt.Fprintf(dockerCli.Out(), "  %s\n", entry)
143
+			}
144
+		}
134 145
 	}
135 146
 
136 147
 	if len(info.Runtimes) > 0 {
... ...
@@ -1051,3 +1051,24 @@ func (s *DockerSwarmSuite) TestExtraHosts(c *check.C) {
1051 1051
 	c.Assert(err, checker.IsNil)
1052 1052
 	c.Assert(out, checker.Contains, expectedOutput, check.Commentf("Expected '%s', but got %q", expectedOutput, out))
1053 1053
 }
1054
+
1055
+func (s *DockerSwarmSuite) TestSwarmManagerAddress(c *check.C) {
1056
+	d1 := s.AddDaemon(c, true, true)
1057
+	d2 := s.AddDaemon(c, true, false)
1058
+	d3 := s.AddDaemon(c, true, false)
1059
+
1060
+	// Manager Addresses will always show Node 1's address
1061
+	expectedOutput := fmt.Sprintf("Manager Addresses:\n  127.0.0.1:%d\n", d1.port)
1062
+
1063
+	out, err := d1.Cmd("info")
1064
+	c.Assert(err, checker.IsNil)
1065
+	c.Assert(out, checker.Contains, expectedOutput)
1066
+
1067
+	out, err = d2.Cmd("info")
1068
+	c.Assert(err, checker.IsNil)
1069
+	c.Assert(out, checker.Contains, expectedOutput)
1070
+
1071
+	out, err = d3.Cmd("info")
1072
+	c.Assert(err, checker.IsNil)
1073
+	c.Assert(out, checker.Contains, expectedOutput)
1074
+}