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