From 42930d8459b59b22cab3f76d85d170871174b479 Mon Sep 17 00:00:00 2001
From: Alberto Contreras <alberto.contreras@canonical.com>
Date: Wed, 29 May 2024 09:10:53 +0200
Subject: [PATCH] fix(azure): disable use-dns for secondary nics (#5314)

Reference:https://github.com/canonical/cloud-init/commit/42930d8459b59b22cab3f76d85d170871174b479
Conflict:(1)not change tests/integration_tests/datasources/test_azure.py
(2)change TestParseNetworkConfig not TestGenerateNetworkConfig, other differences are significant.

DNS resolution through secondary NICs is not supported on Azure. Disable
it.

Without this, we see seconds of delay resolving urls in cloud-init logs
from Jammy+, see SF ticket.

Per cjp256's comment, the first NIC under metadata.imds.network is ensured
to be the primary one. We use this to determine primary NICs instead of
relying on fragile driver and/or NIC names.

Fixes: SF: #00380708

Co-authored-by: Calvin Mwadime <calvin.mwadime@canonical.com>
---
 cloudinit/sources/DataSourceAzure.py          |  3 +++
 tests/unittests/test_datasource/test_azure.py | 19 ++++++++++++-------
 2 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
index f1e6642..7b070b8 100755
--- a/cloudinit/sources/DataSourceAzure.py
+++ b/cloudinit/sources/DataSourceAzure.py
@@ -2207,6 +2207,9 @@ def _generate_network_config_from_imds_metadata(imds_metadata) -> dict:
         # addresses.
         nicname = 'eth{idx}'.format(idx=idx)
         dhcp_override = {'route-metric': (idx + 1) * 100}
+        # DNS resolution through secondary NICs is not supported, disable it.
+        if idx > 0:
+            dhcp_override["use-dns"] = False
         dev_config = {'dhcp4': True, 'dhcp4-overrides': dhcp_override,
                       'dhcp6': False}
         for addr_type in ('ipv4', 'ipv6'):
diff --git a/tests/unittests/test_datasource/test_azure.py b/tests/unittests/test_datasource/test_azure.py
index 62e657b..82d9718 100644
--- a/tests/unittests/test_datasource/test_azure.py
+++ b/tests/unittests/test_datasource/test_azure.py
@@ -243,12 +243,17 @@ class TestParseNetworkConfig(CiTestCase):
                      'match': {'macaddress': '22:0d:3a:04:75:98'},
                      'dhcp6': False,
                      'dhcp4': True,
-                     'dhcp4-overrides': {'route-metric': 200}},
+                     'dhcp4-overrides': {
+                         'route-metric': 200,
+                         "use-dns": False,}},
             'eth2': {'set-name': 'eth2',
                      'match': {'macaddress': '33:0d:3a:04:75:98'},
                      'dhcp6': False,
                      'dhcp4': True,
-                     'dhcp4-overrides': {'route-metric': 300}}}, 'version': 2}
+                     'dhcp4-overrides': {
+                         'route-metric': 300,
+                         "use-dns": False,},},},
+                     'version': 2,}
         imds_data = copy.deepcopy(NETWORK_METADATA)
         imds_data['network']['interface'].append(SECONDARY_INTERFACE)
         third_intf = copy.deepcopy(SECONDARY_INTERFACE)
@@ -274,13 +279,13 @@ class TestParseNetworkConfig(CiTestCase):
                      'match': {'macaddress': '22:0d:3a:04:75:98'},
                      'dhcp4': True,
                      'dhcp6': False,
-                     'dhcp4-overrides': {'route-metric': 200}},
+                     'dhcp4-overrides': {'route-metric': 200, "use-dns": False}},
             'eth2': {'set-name': 'eth2',
                      'match': {'macaddress': '33:0d:3a:04:75:98'},
                      'dhcp4': True,
-                     'dhcp4-overrides': {'route-metric': 300},
+                     'dhcp4-overrides': {'route-metric': 300, "use-dns": False},
                      'dhcp6': True,
-                     'dhcp6-overrides': {'route-metric': 300}}}, 'version': 2}
+                     'dhcp6-overrides': {'route-metric': 300, "use-dns": False}}}, 'version': 2}
         imds_data = copy.deepcopy(NETWORK_METADATA)
         nic1 = imds_data['network']['interface'][0]
         nic1['ipv4']['ipAddress'].append({'privateIpAddress': '10.0.0.5'})
@@ -1184,12 +1189,12 @@ scbus-1 on xpt0 bus 0
                          'match': {'macaddress': '22:0d:3a:04:75:98'},
                          'dhcp6': False,
                          'dhcp4': True,
-                         'dhcp4-overrides': {'route-metric': 200}},
+                         'dhcp4-overrides': {'route-metric': 200, "use-dns": False}},
                 'eth2': {'set-name': 'eth2',
                          'match': {'macaddress': '33:0d:3a:04:75:98'},
                          'dhcp6': False,
                          'dhcp4': True,
-                         'dhcp4-overrides': {'route-metric': 300}}},
+                         'dhcp4-overrides': {'route-metric': 300, "use-dns": False}}},
             'version': 2}
         imds_data = copy.deepcopy(NETWORK_METADATA)
         imds_data['network']['interface'].append(SECONDARY_INTERFACE)
-- 
2.33.0


