Skip to content

Commit

Permalink
Merge pull request juju#8375 from wupeka/2.3-controller-only-local
Browse files Browse the repository at this point in the history
  • Loading branch information
jujubot authored Feb 16, 2018
2 parents 40b0bbb + 94f181e commit e31ffef
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
28 changes: 19 additions & 9 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,19 +785,15 @@ func (c *configInternal) APIInfo() (*api.Info, bool) {
}
servingInfo, isController := c.StateServingInfo()
addrs := c.apiDetails.addresses
// For controller we return only localhost - we should not connect
// to other controllers if we can talk locally.
if isController {
port := servingInfo.APIPort
// TODO(macgreagoir) IPv6. Ubuntu still always provides IPv4
// loopback, and when/if this changes localhost should resolve
// to IPv6 loopback in any case (lp:1644009). Review.
localAPIAddr := net.JoinHostPort("localhost", strconv.Itoa(port))
newAddrs := []string{localAPIAddr}
for _, addr := range addrs {
if addr != localAPIAddr {
newAddrs = append(newAddrs, addr)
}
}
addrs = newAddrs
addrs = []string{localAPIAddr}
}
return &api.Info{
Addrs: addrs,
Expand All @@ -815,13 +811,27 @@ func (c *configInternal) MongoInfo() (info *mongo.MongoInfo, ok bool) {
if !ok {
return nil, false
}
// We return localhost first and then all addresses of known API
// endpoints - this lets us connect to other Mongo instances and start
// state even if our own Mongo has not started yet (see lp:1749383 #1).
// TODO(macgreagoir) IPv6. Ubuntu still always provides IPv4 loopback,
// and when/if this changes localhost should resolve to IPv6 loopback
// in any case (lp:1644009). Review.
addr := net.JoinHostPort("localhost", strconv.Itoa(ssi.StatePort))
local := net.JoinHostPort("localhost", strconv.Itoa(ssi.StatePort))
addrs := []string{local}

for _, addr := range c.apiDetails.addresses {
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, false
}
if host := net.JoinHostPort(host, strconv.Itoa(ssi.StatePort)); host != local {
addrs = append(addrs, host)
}
}
return &mongo.MongoInfo{
Info: mongo.Info{
Addrs: []string{addr},
Addrs: addrs,
CACert: c.caCert,
},
Password: c.statePassword,
Expand Down
24 changes: 7 additions & 17 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,42 +418,32 @@ func (*suite) TestAPIInfoMissingAddress(c *gc.C) {
c.Assert(ok, jc.IsFalse)
}

func (*suite) TestAPIInfoPutsLocalhostFirstWhenServingInfoPresent(c *gc.C) {
func (*suite) TestAPIInfoServesLocalhostOnlyWhenServingInfoPresent(c *gc.C) {
attrParams := attributeParams
attrParams.APIAddresses = []string{"localhost:1235", "localhost:1236"}
servingInfo := stateServingInfo()
conf, err := agent.NewStateMachineConfig(attrParams, servingInfo)
c.Assert(err, jc.ErrorIsNil)
apiinfo, ok := conf.APIInfo()
c.Assert(ok, jc.IsTrue)
c.Check(apiinfo.Addrs, gc.HasLen, len(attrParams.APIAddresses)+1)
c.Check(apiinfo.Addrs[0], gc.Equals, "localhost:47")
}

func (*suite) TestAPIInfoMovesLocalhostFirstWhenServingInfoPresent(c *gc.C) {
attrParams := attributeParams
attrParams.APIAddresses = []string{"localhost:1235", "localhost:47"}
servingInfo := stateServingInfo()
conf, err := agent.NewStateMachineConfig(attrParams, servingInfo)
c.Assert(err, jc.ErrorIsNil)
apiinfo, ok := conf.APIInfo()
c.Assert(ok, jc.IsTrue)
c.Check(apiinfo.Addrs, gc.HasLen, len(attrParams.APIAddresses))
c.Check(apiinfo.Addrs[0], gc.Equals, "localhost:47")
c.Check(apiinfo.Addrs, gc.DeepEquals, []string{"localhost:47"})
}

func (*suite) TestMongoInfo(c *gc.C) {
attrParams := attributeParams
attrParams.APIAddresses = []string{"foo.example:1235", "bar.example:1236", "localhost:88"}
servingInfo := stateServingInfo()
conf, err := agent.NewStateMachineConfig(attrParams, servingInfo)
c.Assert(err, jc.ErrorIsNil)
mongoInfo, ok := conf.MongoInfo()
c.Assert(ok, jc.IsTrue)
c.Check(mongoInfo.Info.Addrs, jc.DeepEquals, []string{"localhost:69"})
c.Check(mongoInfo.Info.Addrs, jc.DeepEquals, []string{"localhost:69", "foo.example:69", "bar.example:69"})
c.Check(mongoInfo.Info.DisableTLS, jc.IsFalse)
}

func (*suite) TestPromotedMongoInfo(c *gc.C) {
attrParams := attributeParams
attrParams.APIAddresses = []string{"foo.example:1235", "bar.example:1236", "localhost:88"}
conf, err := agent.NewAgentConfig(attrParams)
c.Assert(err, jc.ErrorIsNil)

Expand All @@ -468,7 +458,7 @@ func (*suite) TestPromotedMongoInfo(c *gc.C) {

mongoInfo, ok = conf.MongoInfo()
c.Assert(ok, jc.IsTrue)
c.Check(mongoInfo.Info.Addrs, jc.DeepEquals, []string{"localhost:69"})
c.Check(mongoInfo.Info.Addrs, jc.DeepEquals, []string{"localhost:69", "foo.example:69", "bar.example:69"})
c.Check(mongoInfo.Info.DisableTLS, jc.IsFalse)
}

Expand Down

0 comments on commit e31ffef

Please sign in to comment.