Skip to content

Commit

Permalink
feat(netbox): primary IP: fallback to next IPs in the list of addresses
Browse files Browse the repository at this point in the history
Fixes #6978
  • Loading branch information
pdonias committed Aug 28, 2023
1 parent 76b1747 commit f987234
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [REST API] Add support for `filter` and `limit` parameters to `backups/logs` and `restore/logs` collections [Forum#64789](https://xcp-ng.org/forum/post/64789)
- [Pool/Advanced] Ability to set a crash dump SR [#5060](https://github.com/vatesfr/xen-orchestra/issues/5060) (PR [#6973](https://github.com/vatesfr/xen-orchestra/pull/6973))
- [Netbox] Better handle cases where the IP addresses reported by XAPI are malformed (PR [#6989](https://github.com/vatesfr/xen-orchestra/pull/6989))
- [Netbox] Fallback to other VIF's IPs when first VIF doesn't have an IP [#6978](https://github.com/vatesfr/xen-orchestra/issues/6978) (PR [#6989](https://github.com/vatesfr/xen-orchestra/pull/6989))

### Bug fixes

Expand Down
35 changes: 27 additions & 8 deletions packages/xo-server-netbox/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,25 @@ class Netbox {
// Use the first IPs found in vm.addresses as the VMs' primary IPs in
// Netbox, both for IPv4 and IPv6

const getPrimaryIps = addresses => {
const primaryIps = {}
const keys = Object.keys(addresses).sort()
let i = 0
while ((primaryIps.ipv4 === undefined || primaryIps.ipv6 === undefined) && i < keys.length) {
const key = keys[i++]
const ip = addresses[key]

if (key.includes('ipv4') && primaryIps.ipv4 === undefined) {
primaryIps.ipv4 = ip
}
if (key.includes('ipv6') && primaryIps.ipv6 === undefined) {
primaryIps.ipv6 = ip
}
}

return primaryIps
}

log.info("Setting VMs' primary IPs")

const vmsToUpdate2 = []
Expand All @@ -711,7 +730,8 @@ class Netbox {

const nbVmIps = filter(nbIps, nbIp => nbIp.assigned_object?.virtual_machine?.id === nbVm.id)

const ipv4 = xoVm.addresses['0/ipv4/0']
const { ipv4, ipv6 } = getPrimaryIps(xoVm.addresses)

if (ipv4 === undefined && nbVm.primary_ip4 !== null) {
patch.primary_ip4 = null
} else if (ipv4 !== undefined) {
Expand All @@ -728,18 +748,17 @@ class Netbox {
}
}

const _ipv6 = xoVm.addresses['0/ipv6/0']
let ipv6
let compactIpv6
try {
// For IPv6, compare with the compact notation
ipv6 = _ipv6 && ipaddr.parse(_ipv6).toString()
compactIpv6 = ipv6 && ipaddr.parse(ipv6).toString()
} catch (error) {
log.error('Cannot parse IP address', { error, ip: _ipv6 })
log.error('Cannot parse IP address', { error, ip: ipv6 })
}
if (ipv6 === undefined && nbVm.primary_ip6 !== null) {
if (compactIpv6 === undefined && nbVm.primary_ip6 !== null) {
patch.primary_ip6 = null
} else if (ipv6 !== undefined) {
const nbIp = nbVmIps.find(nbIp => nbIp.address.split('/')[0] === ipv6)
} else if (compactIpv6 !== undefined) {
const nbIp = nbVmIps.find(nbIp => nbIp.address.split('/')[0] === compactIpv6)
if (nbIp === undefined && nbVm.primary_ip6 !== null) {
patch.primary_ip6 = null
} else if (nbIp !== undefined && nbIp.id !== nbVm.primary_ip6?.id) {
Expand Down

0 comments on commit f987234

Please sign in to comment.