Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(xo-server-netbox): handle nested prefixes #5908

Merged
merged 2 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
> Users must be able to say: “I had this issue, happy to know it's fixed”

- [SSH keys] Allow SSH key to be broken anywhere to avoid breaking page formatting (Thanks @tstivers1990!) [#5891](https://github.com/vatesfr/xen-orchestra/issues/5891) (PR [#5892](https://github.com/vatesfr/xen-orchestra/pull/5892))
- [Netbox] Handle nested prefixes by always assigning an IP to the smallest prefix it matches (PR [#5908](https://github.com/vatesfr/xen-orchestra/pull/5908))

### Packages to release

Expand All @@ -30,4 +31,5 @@
>
> In case of conflict, the highest (lowest in previous list) `$version` wins.

- xo-server-netbox patch
- xo-web patch
40 changes: 23 additions & 17 deletions packages/xo-server-netbox/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ class Netbox {
.forEach(newInterfaces => Object.assign(interfaces, newInterfaces))

// IPs
const [oldNetboxIps, prefixes] = await Promise.all([
const [oldNetboxIps, netboxPrefixes] = await Promise.all([
this.#makeRequest('/ipam/ip-addresses/', 'GET').then(addresses =>
groupBy(
// In Netbox, a device interface and a VM interface can have the same
Expand Down Expand Up @@ -517,27 +517,33 @@ class Netbox {
const parsedIp = ipaddr.parse(ip)
const ipKind = parsedIp.kind()
const ipCompactNotation = parsedIp.toString()
// FIXME: Should we compare the IPs with their range? ie: can 2 IPs
// look identical but belong to 2 different ranges?
const netboxIpIndex = interfaceOldIps.findIndex(
netboxIp => ipaddr.parse(netboxIp.address.split('/')[0]).toString() === ipCompactNotation
)

let smallestPrefix
let highestBits = 0
netboxPrefixes.forEach(({ prefix }) => {
const [range, bits] = prefix.split('/')
const parsedRange = ipaddr.parse(range)
if (parsedRange.kind() === ipKind && parsedIp.match(parsedRange, bits) && bits > highestBits) {
smallestPrefix = prefix
highestBits = bits
}
})
if (smallestPrefix === undefined) {
ignoredIps.push(ip)
continue
}

const netboxIpIndex = interfaceOldIps.findIndex(netboxIp => {
const [ip, bits] = netboxIp.address.split('/')
return ipaddr.parse(ip).toString() === ipCompactNotation && bits === highestBits
})

if (netboxIpIndex >= 0) {
netboxIpsByVif[vifId].push(interfaceOldIps[netboxIpIndex])
interfaceOldIps.splice(netboxIpIndex, 1)
} else {
const prefix = prefixes.find(({ prefix }) => {
const [range, bits] = prefix.split('/')
const parsedRange = ipaddr.parse(range)
return parsedRange.kind() === ipKind && parsedIp.match(parsedRange, bits)
})
if (prefix === undefined) {
ignoredIps.push(ip)
continue
}

ipsToCreate.push({
address: `${ip}/${prefix.prefix.split('/')[1]}`,
address: `${ip}/${smallestPrefix.split('/')[1]}`,
assigned_object_type: 'virtualization.vminterface',
assigned_object_id: interface_.id,
vifId, // needed to populate netboxIpsByVif with newly created IPs
Expand Down