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