bb836b
From bec5fb60ffae3d1137c7261e5571c2751c5dda25 Mon Sep 17 00:00:00 2001
bb836b
From: James Falcon <TheRealFalcon@users.noreply.github.com>
bb836b
Date: Mon, 8 Mar 2021 14:09:47 -0600
bb836b
Subject: Fix requiring device-number on EC2 derivatives (#836)
8855a0
bb836b
#342 (70dbccbb) introduced the ability to determine route-metrics based on
bb836b
the `device-number` provided by the EC2 IMDS. Not all datasources that
bb836b
subclass EC2 will have this attribute, so allow the old behavior if
bb836b
`device-number` is not present.
8855a0
bb836b
LP: #1917875
8855a0
---
8855a0
 cloudinit/sources/DataSourceEc2.py            |  3 +-
8855a0
 .../unittests/test_datasource/test_aliyun.py  | 30 +++++++++++++++++++
8855a0
 2 files changed, 32 insertions(+), 1 deletion(-)
8855a0
8855a0
diff --git a/cloudinit/sources/DataSourceEc2.py b/cloudinit/sources/DataSourceEc2.py
bb836b
index 1930a509..a2105dc7 100644
8855a0
--- a/cloudinit/sources/DataSourceEc2.py
8855a0
+++ b/cloudinit/sources/DataSourceEc2.py
bb836b
@@ -765,13 +765,14 @@ def convert_ec2_metadata_network_config(
8855a0
         netcfg['ethernets'][nic_name] = dev_config
8855a0
         return netcfg
8855a0
     # Apply network config for all nics and any secondary IPv4/v6 addresses
8855a0
+    nic_idx = 0
8855a0
     for mac, nic_name in sorted(macs_to_nics.items()):
8855a0
         nic_metadata = macs_metadata.get(mac)
8855a0
         if not nic_metadata:
8855a0
             continue  # Not a physical nic represented in metadata
8855a0
         # device-number is zero-indexed, we want it 1-indexed for the
8855a0
         # multiplication on the following line
8855a0
-        nic_idx = int(nic_metadata['device-number']) + 1
8855a0
+        nic_idx = int(nic_metadata.get('device-number', nic_idx)) + 1
8855a0
         dhcp_override = {'route-metric': nic_idx * 100}
8855a0
         dev_config = {'dhcp4': True, 'dhcp4-overrides': dhcp_override,
8855a0
                       'dhcp6': False,
8855a0
diff --git a/tests/unittests/test_datasource/test_aliyun.py b/tests/unittests/test_datasource/test_aliyun.py
bb836b
index eb2828d5..cab1ac2b 100644
8855a0
--- a/tests/unittests/test_datasource/test_aliyun.py
8855a0
+++ b/tests/unittests/test_datasource/test_aliyun.py
8855a0
@@ -7,6 +7,7 @@ from unittest import mock
8855a0
 
8855a0
 from cloudinit import helpers
8855a0
 from cloudinit.sources import DataSourceAliYun as ay
8855a0
+from cloudinit.sources.DataSourceEc2 import convert_ec2_metadata_network_config
8855a0
 from cloudinit.tests import helpers as test_helpers
8855a0
 
8855a0
 DEFAULT_METADATA = {
8855a0
@@ -183,6 +184,35 @@ class TestAliYunDatasource(test_helpers.HttprettyTestCase):
8855a0
         self.assertEqual(ay.parse_public_keys(public_keys),
8855a0
                          public_keys['key-pair-0']['openssh-key'])
8855a0
 
8855a0
+    def test_route_metric_calculated_without_device_number(self):
8855a0
+        """Test that route-metric code works without `device-number`
8855a0
+
8855a0
+        `device-number` is part of EC2 metadata, but not supported on aliyun.
8855a0
+        Attempting to access it will raise a KeyError.
8855a0
+
8855a0
+        LP: #1917875
8855a0
+        """
8855a0
+        netcfg = convert_ec2_metadata_network_config(
8855a0
+            {"interfaces": {"macs": {
8855a0
+                "06:17:04:d7:26:09": {
8855a0
+                    "interface-id": "eni-e44ef49e",
8855a0
+                },
8855a0
+                "06:17:04:d7:26:08": {
8855a0
+                    "interface-id": "eni-e44ef49f",
8855a0
+                }
8855a0
+            }}},
8855a0
+            macs_to_nics={
8855a0
+                '06:17:04:d7:26:09': 'eth0',
8855a0
+                '06:17:04:d7:26:08': 'eth1',
8855a0
+            }
8855a0
+        )
8855a0
+
8855a0
+        met0 = netcfg['ethernets']['eth0']['dhcp4-overrides']['route-metric']
8855a0
+        met1 = netcfg['ethernets']['eth1']['dhcp4-overrides']['route-metric']
8855a0
+
8855a0
+        # route-metric numbers should be 100 apart
8855a0
+        assert 100 == abs(met0 - met1)
8855a0
+
8855a0
 
8855a0
 class TestIsAliYun(test_helpers.CiTestCase):
8855a0
     ALIYUN_PRODUCT = 'Alibaba Cloud ECS'
8855a0
-- 
8855a0
2.27.0
8855a0