Skip to content

Commit f6c1990

Browse files
committed
prefer global ip addresses
Change-Id: I88853631876489187feb88e1ae69b37263a7d5af
1 parent 45692cb commit f6c1990

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

pkg/inventory/db.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,25 @@ func (db *DB) netdevToDRAdev(ifName string) (*resourceapi.Device, error) {
226226
}
227227

228228
if ips, err := netlink.AddrList(link, netlink.FAMILY_ALL); err == nil && len(ips) > 0 {
229-
// TODO assume only one addres by now
230-
ip := ips[0].IP.String()
231-
device.Basic.Attributes["ip"] = resourceapi.DeviceAttribute{StringValue: &ip}
229+
// TODO assume only one address by now but prefer the global ones
230+
var v6found, v4found bool
231+
for _, address := range ips {
232+
if v4found && v6found {
233+
break
234+
}
235+
if !address.IP.IsGlobalUnicast() {
236+
continue
237+
}
238+
239+
if address.IP.To4() == nil && !v6found {
240+
device.Basic.Attributes["ipv6"] = resourceapi.DeviceAttribute{StringValue: ptr.To(address.IP.String())}
241+
} else if !v4found {
242+
device.Basic.Attributes["ip"] = resourceapi.DeviceAttribute{StringValue: ptr.To(address.IP.String())}
243+
}
244+
}
245+
if !v4found {
246+
device.Basic.Attributes["ip"] = resourceapi.DeviceAttribute{StringValue: ptr.To(ips[0].String())}
247+
}
232248
mac := link.Attrs().HardwareAddr.String()
233249
device.Basic.Attributes["mac"] = resourceapi.DeviceAttribute{StringValue: &mac}
234250
mtu := int64(link.Attrs().MTU)

0 commit comments

Comments
 (0)