18322d
From b2500e258b930479cef36f514fcf9581ba68c976 Mon Sep 17 00:00:00 2001
18322d
From: Eduardo Otubo <otubo@redhat.com>
18322d
Date: Wed, 29 May 2019 13:41:48 +0200
18322d
Subject: [PATCH 4/5] DataSourceAzure: Adjust timeout for polling IMDS
18322d
18322d
RH-Author: Eduardo Otubo <otubo@redhat.com>
18322d
Message-id: <20190529134149.842-5-otubo@redhat.com>
18322d
Patchwork-id: 88267
18322d
O-Subject: [RHEL-8.0.1/RHEL-8.1.0 cloud-init PATCHv2 4/5] DataSourceAzure: Adjust timeout for polling IMDS
18322d
Bugzilla: 1691986
18322d
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
18322d
RH-Acked-by: Cathy Avery <cavery@redhat.com>
18322d
18322d
From: Anh Vo <anhvo@microsoft.com>
18322d
commit ab6621d849b24bb652243e88c79f6f3b446048d7
18322d
Author: Anh Vo <anhvo@microsoft.com>
18322d
Date:   Wed May 8 14:54:03 2019 +0000
18322d
18322d
    DataSourceAzure: Adjust timeout for polling IMDS
18322d
18322d
    If the IMDS primary server is not available, falling back to the
18322d
    secondary server takes about 1s. The net result is that the
18322d
    expected E2E time is slightly more than 1s. This change increases
18322d
    the timeout to 2s to prevent the infinite loop of timeouts.
18322d
18322d
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
18322d
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
18322d
---
18322d
 cloudinit/sources/DataSourceAzure.py          | 15 ++++++++++-----
18322d
 tests/unittests/test_datasource/test_azure.py | 10 +++++++---
18322d
 2 files changed, 17 insertions(+), 8 deletions(-)
18322d
18322d
diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py
18322d
index c827816..5baf8da 100755
18322d
--- a/cloudinit/sources/DataSourceAzure.py
18322d
+++ b/cloudinit/sources/DataSourceAzure.py
18322d
@@ -57,7 +57,12 @@ AZURE_CHASSIS_ASSET_TAG = '7783-7084-3265-9085-8269-3286-77'
18322d
 REPROVISION_MARKER_FILE = "/var/lib/cloud/data/poll_imds"
18322d
 REPORTED_READY_MARKER_FILE = "/var/lib/cloud/data/reported_ready"
18322d
 AGENT_SEED_DIR = '/var/lib/waagent'
18322d
+
18322d
+# In the event where the IMDS primary server is not
18322d
+# available, it takes 1s to fallback to the secondary one
18322d
+IMDS_TIMEOUT_IN_SECONDS = 2
18322d
 IMDS_URL = "http://169.254.169.254/metadata/"
18322d
+
18322d
 PLATFORM_ENTROPY_SOURCE = "/sys/firmware/acpi/tables/OEM0"
18322d
 
18322d
 # List of static scripts and network config artifacts created by
18322d
@@ -582,9 +587,9 @@ class DataSourceAzure(sources.DataSource):
18322d
                         return
18322d
                     self._ephemeral_dhcp_ctx.clean_network()
18322d
                 else:
18322d
-                    return readurl(url, timeout=1, headers=headers,
18322d
-                                   exception_cb=exc_cb, infinite=True,
18322d
-                                   log_req_resp=False).contents
18322d
+                    return readurl(url, timeout=IMDS_TIMEOUT_IN_SECONDS,
18322d
+                                   headers=headers, exception_cb=exc_cb,
18322d
+                                   infinite=True, log_req_resp=False).contents
18322d
             except UrlError:
18322d
                 # Teardown our EphemeralDHCPv4 context on failure as we retry
18322d
                 self._ephemeral_dhcp_ctx.clean_network()
18322d
@@ -1291,8 +1296,8 @@ def _get_metadata_from_imds(retries):
18322d
     headers = {"Metadata": "true"}
18322d
     try:
18322d
         response = readurl(
18322d
-            url, timeout=1, headers=headers, retries=retries,
18322d
-            exception_cb=retry_on_url_exc)
18322d
+            url, timeout=IMDS_TIMEOUT_IN_SECONDS, headers=headers,
18322d
+            retries=retries, exception_cb=retry_on_url_exc)
18322d
     except Exception as e:
18322d
         LOG.debug('Ignoring IMDS instance metadata: %s', e)
18322d
         return {}
18322d
diff --git a/tests/unittests/test_datasource/test_azure.py b/tests/unittests/test_datasource/test_azure.py
18322d
index eacf225..bc8b42c 100644
18322d
--- a/tests/unittests/test_datasource/test_azure.py
18322d
+++ b/tests/unittests/test_datasource/test_azure.py
18322d
@@ -163,7 +163,8 @@ class TestGetMetadataFromIMDS(HttprettyTestCase):
18322d
 
18322d
         m_readurl.assert_called_with(
18322d
             self.network_md_url, exception_cb=mock.ANY,
18322d
-            headers={'Metadata': 'true'}, retries=2, timeout=1)
18322d
+            headers={'Metadata': 'true'}, retries=2,
18322d
+            timeout=dsaz.IMDS_TIMEOUT_IN_SECONDS)
18322d
 
18322d
     @mock.patch('cloudinit.url_helper.time.sleep')
18322d
     @mock.patch(MOCKPATH + 'net.is_up')
18322d
@@ -1789,7 +1790,8 @@ class TestAzureDataSourcePreprovisioning(CiTestCase):
18322d
                                     headers={'Metadata': 'true',
18322d
                                              'User-Agent':
18322d
                                              'Cloud-Init/%s' % vs()
18322d
-                                             }, method='GET', timeout=1,
18322d
+                                             }, method='GET',
18322d
+                                    timeout=dsaz.IMDS_TIMEOUT_IN_SECONDS,
18322d
                                     url=full_url)])
18322d
         self.assertEqual(m_dhcp.call_count, 2)
18322d
         m_net.assert_any_call(
18322d
@@ -1826,7 +1828,9 @@ class TestAzureDataSourcePreprovisioning(CiTestCase):
18322d
                                     headers={'Metadata': 'true',
18322d
                                              'User-Agent':
18322d
                                              'Cloud-Init/%s' % vs()},
18322d
-                                    method='GET', timeout=1, url=full_url)])
18322d
+                                    method='GET',
18322d
+                                    timeout=dsaz.IMDS_TIMEOUT_IN_SECONDS,
18322d
+                                    url=full_url)])
18322d
         self.assertEqual(m_dhcp.call_count, 2)
18322d
         m_net.assert_any_call(
18322d
             broadcast='192.168.2.255', interface='eth9', ip='192.168.2.9',
18322d
-- 
18322d
1.8.3.1
18322d