Merge lp:~axwalk/juju-core/machiner-host-addresses into lp:~go-bot/juju-core/trunk

Proposed by Andrew Wilkins
Status: Merged
Approved by: Andrew Wilkins
Approved revision: no longer in the source branch.
Merged at revision: 2225
Proposed branch: lp:~axwalk/juju-core/machiner-host-addresses
Merge into: lp:~go-bot/juju-core/trunk
Diff against target: 124 lines (+70/-0)
3 files modified
worker/machiner/export_test.go (+6/-0)
worker/machiner/machiner.go (+39/-0)
worker/machiner/machiner_test.go (+25/-0)
To merge this branch: bzr merge lp:~axwalk/juju-core/machiner-host-addresses
Reviewer Review Type Date Requested Status
Juju Engineering Pending
Review via email: mp+202383@code.launchpad.net

Commit message

worker/machiner: set machine addresses in SetUp

The machiner worker is updated to, at setup time,
set the machine's addresses to all of the non-loopback
interface addresses as detected from within the
machine. This enables us to record addresses for
containers created by the local provider.

https://codereview.appspot.com/54710043/

Description of the change

worker/machiner: set machine addresses in SetUp

The machiner worker is updated to, at setup time,
set the machine's addresses to all of the non-loopback
interface addresses as detected from within the
machine. This enables us to record addresses for
containers created by the local provider.

https://codereview.appspot.com/54710043/

To post a comment you must log in.
Revision history for this message
Andrew Wilkins (axwalk) wrote :
Download full text (4.8 KiB)

Reviewers: mp+202383_code.launchpad.net,

Message:
Please take a look.

Description:
worker/machiner: set machine addresses in SetUp

The machiner worker is updated to, at setup time,
set the machine's addresses to all of the non-loopback
interface addresses as detected from within the
machine. This enables us to record addresses for
containers created by the local provider.

https://code.launchpad.net/~axwalk/juju-core/machiner-host-addresses/+merge/202383

(do not edit description out of merge proposal)

Please review this at https://codereview.appspot.com/54710043/

Affected files (+74, -0 lines):
   A [revision details]
   A worker/machiner/export_test.go
   M worker/machiner/machiner.go
   M worker/machiner/machiner_test.go

Index: [revision details]
=== added file '[revision details]'
--- [revision details] 2012-01-01 00:00:00 +0000
+++ [revision details] 2012-01-01 00:00:00 +0000
@@ -0,0 +1,2 @@
+Old revision: tarmac-20140120214742-p8sw7oef0btqzfb0
+New revision: <email address hidden>

Index: worker/machiner/export_test.go
=== added file 'worker/machiner/export_test.go'
--- worker/machiner/export_test.go 1970-01-01 00:00:00 +0000
+++ worker/machiner/export_test.go 2014-01-20 22:01:41 +0000
@@ -0,0 +1,6 @@
+// Copyright 2014 Canonical Ltd.
+// Licensed under the AGPLv3, see LICENCE file for details.
+
+package machiner
+
+var InterfaceAddrs = &interfaceAddrs

Index: worker/machiner/machiner.go
=== modified file 'worker/machiner/machiner.go'
--- worker/machiner/machiner.go 2014-01-03 15:34:52 +0000
+++ worker/machiner/machiner.go 2014-01-20 22:04:23 +0000
@@ -4,10 +4,12 @@

  import (
   "fmt"
+ "net"

   "launchpad.net/loggo"

   "launchpad.net/juju-core/agent"
+ "launchpad.net/juju-core/instance"
   "launchpad.net/juju-core/state/api/machiner"
   "launchpad.net/juju-core/state/api/params"
   "launchpad.net/juju-core/state/api/watcher"
@@ -41,6 +43,11 @@
   }
   mr.machine = m

+ // Set the addresses in state to the host's addresses.
+ if err := setMachineAddresses(m); err != nil {
+ return nil, err
+ }
+
   // Mark the machine as started and log it.
   if err := m.SetStatus(params.StatusStarted, "", nil); err != nil {
    return nil, fmt.Errorf("%s failed to set status started: %v", mr.tag,
err)
@@ -50,6 +57,40 @@
   return m.Watch()
  }

+var interfaceAddrs = net.InterfaceAddrs
+
+// setMachineAddresses sets the addresses for this machine to all of the
+// host's non-loopback interface IP addresses. We only need to do this for
+// containers; instances' addresses are updated via the addressupdater
worker
+// by querying the provider.
+func setMachineAddresses(m *machiner.Machine) error {
+ addrs, err := interfaceAddrs()
+ if err != nil {
+ return err
+ }
+ var hostAddresses []instance.Address
+ for _, addr := range addrs {
+ var ip net.IP
+ switch addr := addr.(type) {
+ case *net.IPAddr:
+ ip = addr.IP
+ case *net.IPNet:
+ ip = addr.IP
+ default:
+ continue
+ }
+ if ip.IsLoopback() {
+ continue
+ }
+ hostAddresses = append(hostAddresses, instance.NewAddress(ip.String()))
+ }
+ if len(hostAddresses) == 0 {
+ return nil
+ }
+ logger.Infof("setting addresses for %v to %q"...

Read more...

Revision history for this message
Ian Booth (wallyworld) wrote :

LGTM, a couple of comments

https://codereview.appspot.com/54710043/diff/1/worker/machiner/machiner.go
File worker/machiner/machiner.go (right):

https://codereview.appspot.com/54710043/diff/1/worker/machiner/machiner.go#newcode64
worker/machiner/machiner.go:64: // containers; instances' addresses are
updated via the addressupdater worker
comment needs to be tweaked

https://codereview.appspot.com/54710043/diff/1/worker/machiner/machiner.go#newcode77
worker/machiner/machiner.go:77: case *net.IPNet:
can these 2 case statements be combined?

https://codereview.appspot.com/54710043/

Revision history for this message
Andrew Wilkins (axwalk) wrote :

Please take a look.

https://codereview.appspot.com/54710043/diff/1/worker/machiner/machiner.go
File worker/machiner/machiner.go (right):

https://codereview.appspot.com/54710043/diff/1/worker/machiner/machiner.go#newcode64
worker/machiner/machiner.go:64: // containers; instances' addresses are
updated via the addressupdater worker
On 2014/01/20 22:24:11, wallyworld wrote:
> comment needs to be tweaked

Done.

https://codereview.appspot.com/54710043/diff/1/worker/machiner/machiner.go#newcode77
worker/machiner/machiner.go:77: case *net.IPNet:
On 2014/01/20 22:24:11, wallyworld wrote:
> can these 2 case statements be combined?

Not for a type switch.

https://codereview.appspot.com/54710043/

Revision history for this message
John A Meinel (jameinel) wrote :

https://codereview.appspot.com/54710043/diff/1/worker/machiner/machiner.go
File worker/machiner/machiner.go (right):

https://codereview.appspot.com/54710043/diff/1/worker/machiner/machiner.go#newcode77
worker/machiner/machiner.go:77: case *net.IPNet:
On 2014/01/20 22:59:02, axw wrote:
> On 2014/01/20 22:24:11, wallyworld wrote:
> > can these 2 case statements be combined?

> Not for a type switch.

Couldn't you do:

case *net.IPAddr:
   fallthrough
case *net.IPNet:
   ip = addr.IP

I'd also say, reusing the variable in the switch seems *really*
confusing to me.

switch addr := addr.(type)

I'd rather see something like 'realAddr' or something that makes it
clear what variable you're talking about instead of overloading it.
Maybe that's just me.

https://codereview.appspot.com/54710043/

Revision history for this message
Andrew Wilkins (axwalk) wrote :

On 2014/01/21 07:09:37, jameinel wrote:

https://codereview.appspot.com/54710043/diff/1/worker/machiner/machiner.go
> File worker/machiner/machiner.go (right):

https://codereview.appspot.com/54710043/diff/1/worker/machiner/machiner.go#newcode77
> worker/machiner/machiner.go:77: case *net.IPNet:
> On 2014/01/20 22:59:02, axw wrote:
> > On 2014/01/20 22:24:11, wallyworld wrote:
> > > can these 2 case statements be combined?
> >
> > Not for a type switch.

> Couldn't you do:

> case *net.IPAddr:
> fallthrough
> case *net.IPNet:
> ip = addr.IP

Nope. In either case the block is operating on the newly declared "addr"
which has a type specific to that case. While the blocks *look* the
same, they're really:
     ip = addr.IP, where addr is of type *net.IPAddr
   and
     ip = addr.IP, where addr is of type *net.IPNet

> I'd also say, reusing the variable in the switch seems *really*
confusing to me.

> switch addr := addr.(type)

> I'd rather see something like 'realAddr' or something that makes it
clear what
> variable you're talking about instead of overloading it. Maybe that's
just me.

My opinion is that there's rarely a useful alternative name to give
something that's being type-asserted. To me, "realAddr" doesn't seem
right; how is it any more real than the net.Addr interface value? If
there's no additional information, I don't see the value of introducing
a new name. In this case I could have called it ipAddr, I think, on the
grounds that I've asserted that it's an Addr that has an IP (but not
necessarily just a *net.IPAddr).

It's not just you, people on golang-nuts have debated this before. I'll
try to be more mindful of this in the future. I pretty much always just
do "x := x.(type)" without thinking about it.

https://codereview.appspot.com/54710043/

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'worker/machiner/export_test.go'
2--- worker/machiner/export_test.go 1970-01-01 00:00:00 +0000
3+++ worker/machiner/export_test.go 2014-01-20 22:57:49 +0000
4@@ -0,0 +1,6 @@
5+// Copyright 2014 Canonical Ltd.
6+// Licensed under the AGPLv3, see LICENCE file for details.
7+
8+package machiner
9+
10+var InterfaceAddrs = &interfaceAddrs
11
12=== modified file 'worker/machiner/machiner.go'
13--- worker/machiner/machiner.go 2014-01-03 15:34:52 +0000
14+++ worker/machiner/machiner.go 2014-01-20 22:57:49 +0000
15@@ -4,10 +4,12 @@
16
17 import (
18 "fmt"
19+ "net"
20
21 "launchpad.net/loggo"
22
23 "launchpad.net/juju-core/agent"
24+ "launchpad.net/juju-core/instance"
25 "launchpad.net/juju-core/state/api/machiner"
26 "launchpad.net/juju-core/state/api/params"
27 "launchpad.net/juju-core/state/api/watcher"
28@@ -41,6 +43,11 @@
29 }
30 mr.machine = m
31
32+ // Set the addresses in state to the host's addresses.
33+ if err := setMachineAddresses(m); err != nil {
34+ return nil, err
35+ }
36+
37 // Mark the machine as started and log it.
38 if err := m.SetStatus(params.StatusStarted, "", nil); err != nil {
39 return nil, fmt.Errorf("%s failed to set status started: %v", mr.tag, err)
40@@ -50,6 +57,38 @@
41 return m.Watch()
42 }
43
44+var interfaceAddrs = net.InterfaceAddrs
45+
46+// setMachineAddresses sets the addresses for this machine to all of the
47+// host's non-loopback interface IP addresses.
48+func setMachineAddresses(m *machiner.Machine) error {
49+ addrs, err := interfaceAddrs()
50+ if err != nil {
51+ return err
52+ }
53+ var hostAddresses []instance.Address
54+ for _, addr := range addrs {
55+ var ip net.IP
56+ switch addr := addr.(type) {
57+ case *net.IPAddr:
58+ ip = addr.IP
59+ case *net.IPNet:
60+ ip = addr.IP
61+ default:
62+ continue
63+ }
64+ if ip.IsLoopback() {
65+ continue
66+ }
67+ hostAddresses = append(hostAddresses, instance.NewAddress(ip.String()))
68+ }
69+ if len(hostAddresses) == 0 {
70+ return nil
71+ }
72+ logger.Infof("setting addresses for %v to %q", m.Tag(), hostAddresses)
73+ return m.SetMachineAddresses(hostAddresses)
74+}
75+
76 func (mr *Machiner) Handle() error {
77 if err := mr.machine.Refresh(); params.IsCodeNotFoundOrCodeUnauthorized(err) {
78 return worker.ErrTerminateAgent
79
80=== modified file 'worker/machiner/machiner_test.go'
81--- worker/machiner/machiner_test.go 2013-09-27 10:59:13 +0000
82+++ worker/machiner/machiner_test.go 2014-01-20 22:57:49 +0000
83@@ -4,12 +4,14 @@
84 package machiner_test
85
86 import (
87+ "net"
88 stdtesting "testing"
89 "time"
90
91 gc "launchpad.net/gocheck"
92
93 "launchpad.net/juju-core/agent"
94+ "launchpad.net/juju-core/instance"
95 "launchpad.net/juju-core/juju/testing"
96 "launchpad.net/juju-core/state"
97 "launchpad.net/juju-core/state/api"
98@@ -133,3 +135,26 @@
99 c.Assert(s.machine.Refresh(), gc.IsNil)
100 c.Assert(s.machine.Life(), gc.Equals, state.Dead)
101 }
102+
103+func (s *MachinerSuite) TestMachineAddresses(c *gc.C) {
104+ s.PatchValue(machiner.InterfaceAddrs, func() ([]net.Addr, error) {
105+ addrs := []net.Addr{
106+ &net.IPAddr{IP: net.IPv4(10, 0, 0, 1)},
107+ &net.IPAddr{IP: net.IPv4(127, 0, 0, 1)}, // loopback, ignored
108+ &net.IPAddr{IP: net.IPv6loopback}, // loopback, ignored
109+ &net.UnixAddr{}, // not IP, ignored
110+ &net.IPNet{IP: net.ParseIP("2001:db8::1")},
111+ }
112+ return addrs, nil
113+ })
114+ mr := s.makeMachiner()
115+ defer worker.Stop(mr)
116+ c.Assert(s.machine.Destroy(), gc.IsNil)
117+ s.State.StartSync()
118+ c.Assert(mr.Wait(), gc.Equals, worker.ErrTerminateAgent)
119+ c.Assert(s.machine.Refresh(), gc.IsNil)
120+ c.Assert(s.machine.MachineAddresses(), gc.DeepEquals, []instance.Address{
121+ instance.NewAddress("10.0.0.1"),
122+ instance.NewAddress("2001:db8::1"),
123+ })
124+}

Subscribers

People subscribed via source and target branches

to status/vote changes: