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