20599f
From 93b48730e201bf374f75a3f71d8d6b28211016ba Mon Sep 17 00:00:00 2001
20599f
From: Eduardo Otubo <otubo@redhat.com>
20599f
Date: Tue, 23 Mar 2021 16:14:16 +0100
20599f
Subject: [PATCH] Fix requiring device-number on EC2 derivatives (#836)
20599f
20599f
RH-Author: Eduardo Otubo <otubo@redhat.com>
20599f
RH-MergeRequest: 3: Fix requiring device-number on EC2 derivatives (#836)
20599f
RH-Commit: [1/1] f372b10d179a969fcf824db8a39bdea3befc4ef4 (eterell/cloud-init)
20599f
RH-Bugzilla: 1942699
20599f
RH-Acked-by: Acked-by: Mohammed Gamal <mgamal@redhat.com>
20599f
RH-Acked-by: Acked-by: Vitaly Kuznetsov vkuznets@redhat.com
20599f
RH-Acked-by: Acked-by: Cathy Avery cavery@redhat.com
20599f
20599f
commit 9bd19645a61586b82e86db6f518dd05c3363b17f
20599f
Author: James Falcon <TheRealFalcon@users.noreply.github.com>
20599f
Date:   Mon Mar 8 14:09:47 2021 -0600
20599f
20599f
    Fix requiring device-number on EC2 derivatives (#836)
20599f
20599f
    #342 (70dbccbb) introduced the ability to determine route-metrics based on
20599f
    the `device-number` provided by the EC2 IMDS. Not all datasources that
20599f
    subclass EC2 will have this attribute, so allow the old behavior if
20599f
    `device-number` is not present.
20599f
20599f
    LP: #1917875
20599f
20599f
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
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
20599f
index 1d09c12a..ce69d1b3 100644
20599f
--- a/cloudinit/sources/DataSourceEc2.py
20599f
+++ b/cloudinit/sources/DataSourceEc2.py
20599f
@@ -764,13 +764,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
20599f
index b626229e..a57f86a1 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