sailesh1993 / rpms / cloud-init

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