feat(opennebula): support ETHx_METRIC for default route priority#6811
feat(opennebula): support ETHx_METRIC for default route priority#6811mcanevet wants to merge 1 commit intocanonical:mainfrom
Conversation
93e1974 to
f834651
Compare
|
Hello! Thank you for this proposed change to cloud-init. This pull request is now marked as stale as it has not seen any activity in 14 days. If no activity occurs within the next 7 days, this pull request will automatically close. If you are waiting for code review and you are seeing this message, apologies! Please reply, tagging blackboxsw, and he will ensure that someone takes a look soon. (If the pull request is closed and you would like to continue working on it, please do tag blackboxsw to reopen it.) |
f834651 to
6de0417
Compare
|
@blackboxsw rebased |
Add `get_metric()` to `OpenNebulaNetwork` and use it in `gen_conf()`. When `ETHx_METRIC` is set, the default gateway is emitted as an explicit Netplan v2 `routes:` entry with the metric attached, rather than the simpler `gateway4:` key. When the variable is absent the existing `gateway4:` behaviour is preserved. This allows correct route priority on multi-homed VMs: setting `ETH0_METRIC=0` and `ETH1_METRIC=1` ensures interface 0 is the preferred default route.
6de0417 to
995ab92
Compare
There was a problem hiding this comment.
Pull request overview
This PR enhances the OpenNebula datasource’s Netplan v2 generation to support per-interface default-route priority via ETH<x>_METRIC, aligning cloud-init behavior with OpenNebula’s context-linux contextualization variables for multi-homed VMs.
Changes:
- Add
OpenNebulaNetwork.get_metric()and use it ingen_conf()to emit the IPv4 default gateway as an explicitroutes:entry withmetricwhenETH<x>_METRICis set (otherwise preservegateway4:behavior). - Add unit tests covering
get_metric()and thegen_conf()metric-based route emission behavior. - Document
ETH<x>_METRICin the OpenNebula datasource reference.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
cloudinit/sources/DataSourceOpenNebula.py |
Adds metric lookup and conditionally emits default route with a metric using Netplan routes:. |
tests/unittests/sources/test_opennebula.py |
Adds unit tests for ETH<x>_METRIC parsing and Netplan config output. |
doc/rtd/reference/datasources/opennebula.rst |
Documents the new ETH<x>_METRIC contextualization variable and its effect on default routing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if metric is not None: | ||
| devconf.setdefault("routes", []).append( | ||
| { | ||
| "to": "default", | ||
| "via": gateway, | ||
| "metric": int(metric), | ||
| } | ||
| ) |
| devconf.setdefault("routes", []).append( | ||
| { | ||
| "to": "default", | ||
| "via": gateway, | ||
| "metric": int(metric), | ||
| } | ||
| ) |
| @mock.patch(DS_PATH + ".get_physical_nics_by_mac") | ||
| def test_gen_conf_metric_replaces_gateway4(self, m_get_phys_by_mac): | ||
| """When metric is set, gateway is emitted as an explicit route.""" | ||
| context = { | ||
| "ETH0_MAC": MACADDR, | ||
| "ETH0_IP": PUBLIC_IP, | ||
| "ETH0_GATEWAY": "10.0.0.1", | ||
| "ETH0_METRIC": "100", | ||
| } | ||
| m_get_phys_by_mac.return_value = {MACADDR: "eth0"} | ||
| net = ds.OpenNebulaNetwork(context, mock.Mock()) | ||
| conf = net.gen_conf() | ||
| eth0 = conf["ethernets"]["eth0"] | ||
| assert "gateway4" not in eth0 | ||
| expected_route = {"to": "default", "via": "10.0.0.1", "metric": 100} | ||
| assert expected_route in eth0["routes"] | ||
|
|
Proposed Commit Message
Additional Context
The OpenNebula
context-linuxpackage (one-apps) supportsETHx_METRICto set the route metric for the default gateway on each interface.
Without this, multi-homed VMs (common in OpenNebula deployments using
tools like netbox-network-config) have no way to express route priority
and the kernel picks an arbitrary default route.
The fix is minimal: when
ETHx_METRICis present thegateway4:key isreplaced by an explicit
routes:entry carrying the metric. When thevariable is absent behaviour is identical to before.
Test Steps
On a multi-homed OpenNebula VM, add to the VM template context:
After boot, verify route priorities:
Also verify that a VM with no
ETHx_METRICset continues to producegateway4:in its Netplan config without any change in behaviour.Merge type