sailesh1993 / rpms / cloud-init

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