sailesh1993 / rpms / cloud-init

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