Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 454b441

Browse filesBrowse files
authored
feat: add configurable GCE Metadata Server retries (#1488)
This PR aims to replicate the functionality of the Nodejs library implemented here: https://togithub.com/googleapis/gcp-metadata/pull/275
1 parent 6cfdeeb commit 454b441
Copy full SHA for 454b441

3 files changed

+40-2Lines changed: 40 additions & 2 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎google/auth/compute_engine/_metadata.py‎

Copy file name to clipboardExpand all lines: google/auth/compute_engine/_metadata.py
+13-2Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,19 @@ def _get_metadata_ip_root(use_mtls: bool):
9797
# Timeout in seconds to wait for the GCE metadata server when detecting the
9898
# GCE environment.
9999
try:
100-
_METADATA_DEFAULT_TIMEOUT = int(os.getenv("GCE_METADATA_TIMEOUT", 3))
100+
_METADATA_DEFAULT_TIMEOUT = int(os.getenv(environment_vars.GCE_METADATA_TIMEOUT, 3))
101101
except ValueError: # pragma: NO COVER
102102
_METADATA_DEFAULT_TIMEOUT = 3
103103

104+
# The number of tries to perform when waiting for the GCE metadata server
105+
# when detecting the GCE environment.
106+
try:
107+
_METADATA_DETECT_RETRIES = int(
108+
os.getenv(environment_vars.GCE_METADATA_DETECT_RETRIES, 3)
109+
)
110+
except ValueError: # pragma: NO COVER
111+
_METADATA_DETECT_RETRIES = 3
112+
104113
# This is used to disable checking for the GCE metadata server and directly
105114
# assuming it's not available.
106115
_NO_GCE_CHECK = os.getenv(environment_vars.NO_GCE_CHECK) == "true"
@@ -177,7 +186,9 @@ def _prepare_request_for_mds(request, use_mtls=False) -> None:
177186
request.session.mount(f"https://{host}/", adapter)
178187

179188

180-
def ping(request, timeout=_METADATA_DEFAULT_TIMEOUT, retry_count=3):
189+
def ping(
190+
request, timeout=_METADATA_DEFAULT_TIMEOUT, retry_count=_METADATA_DETECT_RETRIES
191+
):
181192
"""Checks to see if the metadata server is available.
182193
183194
Args:
Collapse file

‎google/auth/environment_vars.py‎

Copy file name to clipboardExpand all lines: google/auth/environment_vars.py
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@
6060
"""Environment variable providing an alternate ip:port to be used for ip-only
6161
GCE metadata requests."""
6262

63+
GCE_METADATA_TIMEOUT = "GCE_METADATA_TIMEOUT"
64+
"""Environment variable defining the timeout in seconds to wait for the
65+
GCE metadata server when detecting the GCE environment.
66+
"""
67+
68+
GCE_METADATA_DETECT_RETRIES = "GCE_METADATA_DETECT_RETRIES"
69+
"""Environment variable representing the number of retries that should be
70+
attempted on metadata lookup.
71+
"""
72+
6373
NO_GCE_CHECK = "NO_GCE_CHECK"
6474
"""Environment variable controlling whether to check if running on GCE or not.
6575
Collapse file

‎tests/compute_engine/test__metadata.py‎

Copy file name to clipboardExpand all lines: tests/compute_engine/test__metadata.py
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,23 @@ def test_ping_success_custom_root(mock_metrics_header_value):
201201
)
202202

203203

204+
@mock.patch("google.auth.metrics.mds_ping", return_value=MDS_PING_METRICS_HEADER_VALUE)
205+
def test_ping_failure_custom_retry(mock_metrics_header_value):
206+
request = make_request("")
207+
request.side_effect = exceptions.TransportError()
208+
209+
os.environ[environment_vars.GCE_METADATA_DETECT_RETRIES] = "10"
210+
importlib.reload(_metadata)
211+
212+
try:
213+
_metadata.ping(request)
214+
finally:
215+
del os.environ[environment_vars.GCE_METADATA_DETECT_RETRIES]
216+
importlib.reload(_metadata)
217+
218+
assert request.call_count == 10
219+
220+
204221
def test_get_success_json():
205222
key, value = "foo", "bar"
206223

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.