Hi Nux,

It is because NetworkInterface.getInterfaceAddresses() returns the ips in
reverse order.
For example, "ip addr show dev eth0"  shows the following ips
inet X.X.X.X
inet secondary Y.Y.Y.Y
inet6 Z:Z:Z:Z:Z:Z

Then NetworkInterface.getInterfaceAddresses returns
Z:Z:Z:Z:Z:Z
Y.Y.Y.Y
X.X.X.X

The following patch should work as you expect.

```
diff --git a/utils/src/main/java/com/cloud/utils/net/NetUtils.java
b/utils/src/main/java/com/cloud/utils/net/NetUtils.java
index afe73f1..afb0ce6 100644
--- a/utils/src/main/java/com/cloud/utils/net/NetUtils.java
+++ b/utils/src/main/java/com/cloud/utils/net/NetUtils.java
@@ -32,6 +32,7 @@ import java.net.SocketException;
 import java.net.URI;
 import java.net.UnknownHostException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Formatter;
 import java.util.List;
 import java.util.Random;
@@ -394,10 +395,11 @@ public class NetUtils {
     }

     public static String[] getNetworkParams(final NetworkInterface nic) {
-        final List<InterfaceAddress> addrs = nic.getInterfaceAddresses();
+        List<InterfaceAddress> addrs = nic.getInterfaceAddresses();
         if (addrs == null || addrs.size() == 0) {
             return null;
         }
+        Collections.reverse(addrs);
         InterfaceAddress addr = null;
         for (final InterfaceAddress iaddr : addrs) {
             final InetAddress inet = iaddr.getAddress();
```


-Wei



Nux! <n...@li.nux.ro> 于2019年5月1日周三 下午4:00写道:

> I ran into a similar problem when I tried to allocate an additional IP on
> the agent machine (for other purposes), the hypervisor would then show up
> in the UI with this new IP and in "Alert" state.
>
> I solved the problem by moving the IP out of br0 and on to the loopback
> interface.
>
> --
> Sent from the Delta quadrant using Borg technology!
>
> Nux!
> www.nux.ro
>
> ----- Original Message -----
> > From: "li jerry" <div...@hotmail.com>
> > To: "users" <us...@cloudstack.apache.org>, "dev" <
> dev@cloudstack.apache.org>
> > Sent: Monday, 22 April, 2019 14:36:51
> > Subject: Cloudstack-agent gets the local IP address exception
>
> > HI All
> >
> >
> > my kvm host [ip:10.226.16.11] virtualized a vip[10.226.16.10] via
> keepalived.
> >
> > After I started the cloudstack-agent, I got vip[10.226.16.10] and I
> couldn't get
> > the correct IP [10.226.16.11];
> >
> > Can I have any way to get the cloudstack agent to get the correct IP
> address?
> >
> >
> >
> > Cloudstack 4.11.2
> > CentOS 7.5
> >
> >
> >
> > br2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
> group
> > default qlen 1000
> >    link/ether ac:1f:6b:ba:96:ea brd ff:ff:ff:ff:ff:ff
> >    inet 10.226.16.11/24 brd 10.226.16.255 scope global noprefixroute br2
> >       valid_lft forever preferred_lft forever
> >    inet 10.226.16.10/24 scope global secondary br2
> >       valid_lft forever preferred_lft forever
> >    inet6 fe80::1456:f9ff:fe06:6228/64 scope link
> >        valid_lft forever preferred_lft forever
>

Reply via email to