summaryrefslogtreecommitdiff
path: root/cloudinit/sources
diff options
context:
space:
mode:
authorJames Falcon <james.falcon@canonical.com>2021-12-02 08:51:26 -0600
committerGitHub <noreply@github.com>2021-12-02 08:51:26 -0600
commitcf38c2cbc5875813fbb9858f45e5b95789b7ffea (patch)
treec345dc97de1c17488fe6c27697f48f0febae6de6 /cloudinit/sources
parenta1cf55e5e6331b9b3a4f9ceb412dd14c78abb5ea (diff)
downloadvyos-cloud-init-cf38c2cbc5875813fbb9858f45e5b95789b7ffea.tar.gz
vyos-cloud-init-cf38c2cbc5875813fbb9858f45e5b95789b7ffea.zip
Move GCE metadata fetch to init-local (SC-502) (#1122)
GCE currently fetches metadata after network has come up. There's no reason we can't fetch at init-local time, so update GCE to fetch at init-local time to be more performant and consistent with other datasources.
Diffstat (limited to 'cloudinit/sources')
-rw-r--r--cloudinit/sources/DataSourceGCE.py25
1 files changed, 21 insertions, 4 deletions
diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py
index 9f838bd4..b82fa410 100644
--- a/cloudinit/sources/DataSourceGCE.py
+++ b/cloudinit/sources/DataSourceGCE.py
@@ -4,6 +4,7 @@
import datetime
import json
+from contextlib import suppress as noop
from base64 import b64decode
@@ -13,6 +14,7 @@ from cloudinit import log as logging
from cloudinit import sources
from cloudinit import url_helper
from cloudinit import util
+from cloudinit.net.dhcp import EphemeralDHCPv4
LOG = logging.getLogger(__name__)
@@ -58,6 +60,7 @@ class GoogleMetadataFetcher(object):
class DataSourceGCE(sources.DataSource):
dsname = 'GCE'
+ perform_dhcp_setup = False
def __init__(self, sys_cfg, distro, paths):
sources.DataSource.__init__(self, sys_cfg, distro, paths)
@@ -73,10 +76,19 @@ class DataSourceGCE(sources.DataSource):
def _get_data(self):
url_params = self.get_url_params()
- ret = util.log_time(
- LOG.debug, 'Crawl of GCE metadata service',
- read_md, kwargs={'address': self.metadata_address,
- 'url_params': url_params})
+ network_context = noop()
+ if self.perform_dhcp_setup:
+ network_context = EphemeralDHCPv4(self.fallback_interface)
+ with network_context:
+ ret = util.log_time(
+ LOG.debug,
+ "Crawl of GCE metadata service",
+ read_md,
+ kwargs={
+ "address": self.metadata_address,
+ "url_params": url_params,
+ },
+ )
if not ret['success']:
if ret['platform_reports_gce']:
@@ -117,6 +129,10 @@ class DataSourceGCE(sources.DataSource):
return self.availability_zone.rsplit('-', 1)[0]
+class DataSourceGCELocal(DataSourceGCE):
+ perform_dhcp_setup = True
+
+
def _write_host_key_to_guest_attributes(key_type, key_value):
url = '%s/%s/%s' % (GUEST_ATTRIBUTES_URL, HOSTKEY_NAMESPACE, key_type)
key_value = key_value.encode('utf-8')
@@ -272,6 +288,7 @@ def platform_reports_gce():
# Used to match classes to dependencies.
datasources = [
+ (DataSourceGCELocal, (sources.DEP_FILESYSTEM,)),
(DataSourceGCE, (sources.DEP_FILESYSTEM, sources.DEP_NETWORK)),
]