@@ -68,16 +68,22 @@ func (agent *Agent) collectMetadata() {
6868 // Always update primary IP
6969 ipAddrs , err := iface .Addrs ()
7070 if err != nil || len (ipAddrs ) < 1 {
71- agent .Log .Error ("Failed to collect machine primary IP" )
71+ agent .Log .Error ("Failed to collect machine primary IP" , zap . Error ( err ) )
7272 return
7373 }
7474
75- ip , _ , err := net .ParseCIDR (ipAddrs [0 ].String ())
76- if err != nil {
77- agent .Log .Error ("Failed to collect machine primary IP" )
78- return
75+ for _ , ipAddr := range ipAddrs {
76+ ip , _ , err := net .ParseCIDR (ipAddr .String ())
77+ if err != nil {
78+ agent .Log .Error ("Failed to collect machine primary IP" , zap .Error (err ))
79+ continue
80+ }
81+ if ! ip .IsLoopback () && ! ip .IsLinkLocalUnicast () && ! ip .IsGlobalUnicast () {
82+ continue
83+ }
84+ agent .Metadata .PrimaryIP = ip .String ()
7985 }
80- agent . Metadata . PrimaryIP = ip . String ()
86+
8187}
8288
8389// isMulticastCapable reports whether ifi is an IP multicast-capable
@@ -150,7 +156,7 @@ func routableIP(network string, ip net.IP) net.IP {
150156 return ip
151157 }
152158 case "ip6" :
153- if ip .IsLoopback () { // addressing scope of the loopback address depends on each implementation
159+ if ip .IsLoopback () || ip . IsLinkLocalUnicast () { // addressing scope of the loopback address depends on each implementation
154160 return nil
155161 }
156162 if ip := ip .To16 (); ip != nil && ip .To4 () == nil {
0 commit comments