diff options
author | Chris Patterson <cpatterson@microsoft.com> | 2023-01-12 17:14:03 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-12 16:14:03 -0600 |
commit | 3e1751c564814212cda69c7d91d0aad61840edc6 (patch) | |
tree | ebc675360ffc239aae867004c3ca58be37ecf680 | |
parent | e776bc48db2eaf2f8596b1bab74469732a69649b (diff) | |
download | cloud-init-git-3e1751c564814212cda69c7d91d0aad61840edc6.tar.gz |
sources/azure: minor refactor for metadata source detection logic (#1936)
- Initialize md and cfg to the fallback used when no OVF is found
and IMDS is required.
- Rename metadata_source -> ovf_source and drop usage of "IMDS" as
a valid value.
- Set `self.seed` to "IMDS" when ovf_source is unset.
- Remove late check for metadata source. This is already done
by the earlier check where we'll fail with "No OVF or IMDS
available".
- Move "Found provisioning metadata" diagnostic up to where we
read OVF. Suggesting it was "IMDS" prior to querying IMDS
is misleading.
- Add warning when falling back to IMDS-only provisioning.
Signed-off-by: Chris Patterson <cpatterson@microsoft.com>
-rw-r--r-- | cloudinit/sources/DataSourceAzure.py | 50 |
1 files changed, 21 insertions, 29 deletions
diff --git a/cloudinit/sources/DataSourceAzure.py b/cloudinit/sources/DataSourceAzure.py index 55b8db85..8ee1bea7 100644 --- a/cloudinit/sources/DataSourceAzure.py +++ b/cloudinit/sources/DataSourceAzure.py @@ -527,11 +527,10 @@ class DataSourceAzure(sources.DataSource): # it determines the value of ret. More specifically, the first one in # the candidate list determines the path to take in order to get the # metadata we need. - ovf_is_accessible = False - metadata_source = None - md = {} + ovf_source = None + md = {"local-hostname": ""} + cfg = {"system_info": {"default_user": {"name": ""}}} userdata_raw = "" - cfg = {} files = {} for src in list_possible_azure_ds(self.seed_dir, ddir): @@ -549,8 +548,12 @@ class DataSourceAzure(sources.DataSource): self._iso_dev = src else: md, userdata_raw, cfg, files = load_azure_ds_dir(src) - ovf_is_accessible = True - metadata_source = src + + ovf_source = src + report_diagnostic_event( + "Found provisioning metadata in %s" % ovf_source, + logger_func=LOG.debug, + ) break except NonAzureDataSource: report_diagnostic_event( @@ -562,26 +565,25 @@ class DataSourceAzure(sources.DataSource): report_diagnostic_event( "%s was not mountable" % src, logger_func=LOG.debug ) - md = {"local-hostname": ""} - cfg = {"system_info": {"default_user": {"name": ""}}} - metadata_source = "IMDS" continue except BrokenAzureDataSource as exc: msg = "BrokenAzureDataSource: %s" % exc report_diagnostic_event(msg, logger_func=LOG.error) raise sources.InvalidMetaDataException(msg) - - report_diagnostic_event( - "Found provisioning metadata in %s" % metadata_source, - logger_func=LOG.debug, - ) + else: + msg = ( + "Unable to find provisioning media, falling back to IMDS " + "metadata. Be aware that IMDS metadata does not support " + "admin passwords or custom-data (user-data only)." + ) + report_diagnostic_event(msg, logger_func=LOG.warning) # If we read OVF from attached media, we are provisioning. If OVF # is not found, we are probably provisioning on a system which does # not have UDF support. In either case, require IMDS metadata. # If we require IMDS metadata, try harder to obtain networking, waiting # for at least 20 minutes. Otherwise only wait 5 minutes. - requires_imds_metadata = bool(self._iso_dev) or not ovf_is_accessible + requires_imds_metadata = bool(self._iso_dev) or ovf_source is None timeout_minutes = 20 if requires_imds_metadata else 5 try: self._setup_ephemeral_networking(timeout_minutes=timeout_minutes) @@ -593,7 +595,7 @@ class DataSourceAzure(sources.DataSource): else: imds_md = {} - if not imds_md and not ovf_is_accessible: + if not imds_md and ovf_source is None: msg = "No OVF or IMDS available" report_diagnostic_event(msg) raise sources.InvalidMetaDataException(msg) @@ -619,7 +621,7 @@ class DataSourceAzure(sources.DataSource): # Report errors if IMDS network configuration is missing data. self.validate_imds_network_metadata(imds_md=imds_md) - self.seed = metadata_source + self.seed = ovf_source or "IMDS" crawled_data.update( { "cfg": cfg, @@ -646,7 +648,7 @@ class DataSourceAzure(sources.DataSource): "disable_password" ] = imds_disable_password - if metadata_source == "IMDS" and not crawled_data["files"]: + if self.seed == "IMDS" and not crawled_data["files"]: try: contents = build_minimal_ovf( username=imds_username, # pyright: ignore @@ -675,17 +677,7 @@ class DataSourceAzure(sources.DataSource): "Bad userdata in IMDS", logger_func=LOG.warning ) - if not metadata_source: - msg = "No Azure metadata found" - report_diagnostic_event(msg, logger_func=LOG.error) - raise sources.InvalidMetaDataException(msg) - else: - report_diagnostic_event( - "found datasource in %s" % metadata_source, - logger_func=LOG.debug, - ) - - if metadata_source == ddir: + if ovf_source == ddir: report_diagnostic_event( "using files cached in %s" % ddir, logger_func=LOG.debug ) |