sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init 10 months ago
Clone
9796c9
From 40c52ce1f4049449b04f93226721f63af874c5c7 Mon Sep 17 00:00:00 2001
9796c9
From: Eduardo Dobay <edudobay@users.noreply.github.com>
9796c9
Date: Wed, 6 Apr 2022 01:28:01 -0300
9796c9
Subject: [PATCH] Support EC2 tags in instance metadata (#1309)
9796c9
9796c9
Add support for newer EC2 metadata versions (up to 2021-03-23), so that
9796c9
tags can be retrieved from the `ds.meta_data.tags` field, as well as
9796c9
with any new fields that might have been added since the 2018-09-24
9796c9
version.
9796c9
---
9796c9
 cloudinit/sources/DataSourceEc2.py  |  5 +++--
9796c9
 doc/rtd/topics/datasources/ec2.rst  | 28 ++++++++++++++++++++++------
9796c9
 tests/unittests/sources/test_ec2.py | 26 +++++++++++++++++++++++++-
9796c9
 tools/.github-cla-signers           |  1 +
9796c9
 4 files changed, 51 insertions(+), 9 deletions(-)
9796c9
9796c9
diff --git a/cloudinit/sources/DataSourceEc2.py b/cloudinit/sources/DataSourceEc2.py
9796c9
index 03b3870c2e..a030b4987b 100644
9796c9
--- a/cloudinit/sources/DataSourceEc2.py
9796c9
+++ b/cloudinit/sources/DataSourceEc2.py
9796c9
@@ -61,8 +61,9 @@ class DataSourceEc2(sources.DataSource):
9796c9
     min_metadata_version = "2009-04-04"
9796c9
 
9796c9
     # Priority ordered list of additional metadata versions which will be tried
9796c9
-    # for extended metadata content. IPv6 support comes in 2016-09-02
9796c9
-    extended_metadata_versions = ["2018-09-24", "2016-09-02"]
9796c9
+    # for extended metadata content. IPv6 support comes in 2016-09-02.
9796c9
+    # Tags support comes in 2021-03-23.
9796c9
+    extended_metadata_versions = ["2021-03-23", "2018-09-24", "2016-09-02"]
9796c9
 
9796c9
     # Setup read_url parameters per get_url_params.
9796c9
     url_max_wait = 120
9796c9
diff --git a/doc/rtd/topics/datasources/ec2.rst b/doc/rtd/topics/datasources/ec2.rst
9796c9
index 94e4158d93..77232269b9 100644
9796c9
--- a/doc/rtd/topics/datasources/ec2.rst
9796c9
+++ b/doc/rtd/topics/datasources/ec2.rst
9796c9
@@ -38,11 +38,26 @@ Userdata is accessible via the following URL:
9796c9
     GET http://169.254.169.254/2009-04-04/user-data
9796c9
     1234,fred,reboot,true | 4512,jimbo, | 173,,,
9796c9
 
9796c9
-Note that there are multiple versions of this data provided, cloud-init
9796c9
-by default uses **2009-04-04** but newer versions can be supported with
9796c9
-relative ease (newer versions have more data exposed, while maintaining
9796c9
-backward compatibility with the previous versions).
9796c9
-Version **2016-09-02** is required for secondary IP address support.
9796c9
+Note that there are multiple EC2 Metadata versions of this data provided
9796c9
+to instances. cloud-init will attempt to use the most recent API version it
9796c9
+supports in order to get latest API features and instance-data. If a given
9796c9
+API version is not exposed to the instance, those API features will be
9796c9
+unavailable to the instance.
9796c9
+
9796c9
+
9796c9
++----------------+----------------------------------------------------------+
9796c9
++ EC2 version    | supported instance-data/feature                          |
9796c9
++================+==========================================================+
9796c9
++ **2021-03-23** | Required for Instance tag support. This feature must be  |
9796c9
+|                | enabled individually on each instance.  See the          |
9796c9
+|                | `EC2 tags user guide`_.                                  |
9796c9
++----------------+----------------------------------------------------------+
9796c9
+| **2016-09-02** | Required for secondary IP address support.               |
9796c9
++----------------+----------------------------------------------------------+
9796c9
+| **2009-04-04** | Minimum supports EC2 API version for meta-data and       |
9796c9
+|                | user-data.                                               |
9796c9
++----------------+----------------------------------------------------------+
9796c9
+
9796c9
 
9796c9
 To see which versions are supported from your cloud provider use the following
9796c9
 URL:
9796c9
@@ -71,7 +86,7 @@ configuration (in `/etc/cloud/cloud.cfg` or `/etc/cloud/cloud.cfg.d/`).
9796c9
 
9796c9
 The settings that may be configured are:
9796c9
 
9796c9
- * **metadata_urls**: This list of urls will be searched for an Ec2
9796c9
+ * **metadata_urls**: This list of urls will be searched for an EC2
9796c9
    metadata service. The first entry that successfully returns a 200 response
9796c9
    for <url>/<version>/meta-data/instance-id will be selected.
9796c9
    (default: ['http://169.254.169.254', 'http://instance-data:8773']).
9796c9
@@ -121,4 +136,5 @@ Notes
9796c9
    For example: the primary NIC will have a DHCP route-metric of 100,
9796c9
    the next NIC will be 200.
9796c9
 
9796c9
+.. _EC2 tags user guide: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#work-with-tags-in-IMDS
9796c9
 .. vi: textwidth=79
9796c9
diff --git a/tests/unittests/sources/test_ec2.py b/tests/unittests/sources/test_ec2.py
9796c9
index b376660d3e..7c8a5ea5ef 100644
9796c9
--- a/tests/unittests/sources/test_ec2.py
9796c9
+++ b/tests/unittests/sources/test_ec2.py
9796c9
@@ -210,6 +210,17 @@
9796c9
 
9796c9
 M_PATH_NET = "cloudinit.sources.DataSourceEc2.net."
9796c9
 
9796c9
+TAGS_METADATA_2021_03_23 = {
9796c9
+    **DEFAULT_METADATA,
9796c9
+    "tags": {
9796c9
+        "instance": {
9796c9
+            "Environment": "production",
9796c9
+            "Application": "test",
9796c9
+            "TagWithoutValue": "",
9796c9
+        }
9796c9
+    },
9796c9
+}
9796c9
+
9796c9
 
9796c9
 def _register_ssh_keys(rfunc, base_url, keys_data):
9796c9
     """handle ssh key inconsistencies.
9796c9
@@ -670,7 +681,7 @@ def test_aws_token_redacted(self):
9796c9
         logs_with_redacted = [log for log in all_logs if REDACT_TOK in log]
9796c9
         logs_with_token = [log for log in all_logs if "API-TOKEN" in log]
9796c9
         self.assertEqual(1, len(logs_with_redacted_ttl))
9796c9
-        self.assertEqual(81, len(logs_with_redacted))
9796c9
+        self.assertEqual(83, len(logs_with_redacted))
9796c9
         self.assertEqual(0, len(logs_with_token))
9796c9
 
9796c9
     @mock.patch("cloudinit.net.dhcp.maybe_perform_dhcp_discovery")
9796c9
@@ -811,6 +822,19 @@ def test_ec2_local_performs_dhcp_on_non_bsd(
9796c9
         )
9796c9
         self.assertIn("Crawl of metadata service took", self.logs.getvalue())
9796c9
 
9796c9
+    def test_get_instance_tags(self):
9796c9
+        ds = self._setup_ds(
9796c9
+            platform_data=self.valid_platform_data,
9796c9
+            sys_cfg={"datasource": {"Ec2": {"strict_id": False}}},
9796c9
+            md={"md": TAGS_METADATA_2021_03_23},
9796c9
+        )
9796c9
+        self.assertTrue(ds.get_data())
9796c9
+        self.assertIn("tags", ds.metadata)
9796c9
+        self.assertIn("instance", ds.metadata["tags"])
9796c9
+        instance_tags = ds.metadata["tags"]["instance"]
9796c9
+        self.assertEqual(instance_tags["Application"], "test")
9796c9
+        self.assertEqual(instance_tags["Environment"], "production")
9796c9
+
9796c9
 
9796c9
 class TestGetSecondaryAddresses(test_helpers.CiTestCase):
9796c9
 
9796c9
diff --git a/tools/.github-cla-signers b/tools/.github-cla-signers
9796c9
index a8f2fd55e1..5f05dba907 100644
9796c9
--- a/tools/.github-cla-signers
9796c9
+++ b/tools/.github-cla-signers
9796c9
@@ -28,6 +28,7 @@ dermotbradley
9796c9
 dhensby
9796c9
 eandersson
9796c9
 eb3095
9796c9
+edudobay
9796c9
 emmanuelthome
9796c9
 eslerm
9796c9
 esposem