sailesh1993 / rpms / cloud-init

Forked from rpms/cloud-init a year ago
Clone
Blob Blame History Raw
From 28e77dc7d641233b5793a159befc225fd3a8726b Mon Sep 17 00:00:00 2001
From: Eduardo Otubo <otubo@redhat.com>
Date: Mon, 4 May 2020 12:40:08 +0200
Subject: [PATCH 5/6] net: handle openstack dhcpv6-stateless configuration
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

RH-Author: Eduardo Otubo <otubo@redhat.com>
Message-id: <20200327152826.13343-6-otubo@redhat.com>
Patchwork-id: 94461
O-Subject: [RHEL-8.1.z/RHEL-8.2.z cloud-init PATCHv2 5/6] net: handle openstack dhcpv6-stateless configuration
Bugzilla: 1811753
RH-Acked-by: Cathy Avery <cavery@redhat.com>
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>

commit fac98983187c0984aa79c569c4b76cab90fd6f47
Author: Harald Jensås <hjensas@redhat.com>
Date:   Wed Oct 16 15:30:28 2019 +0000

    net: handle openstack dhcpv6-stateless configuration

    Openstack subnets can be configured to use SLAAC by setting
    ipv6_address_mode=dhcpv6-stateless. When this is the case
    the sysconfig interface configuration should use
    IPV6_AUTOCONF=yes and not set DHCPV6C=yes.

    This change sets the subnets type property to the full
    network['type'] from openstack metadata.

    cloudinit/net/sysconfig.py and cloudinit/net/eni.py
    are updated to support new subnet types:
      - 'ipv6_dhcpv6-stateless' => IPV6_AUTOCONF=yes
      - 'ipv6_dhcpv6-stateful' => DHCPV6C=yes

    Type 'dhcp6' in sysconfig is kept for backward compatibility
    with any implementations that set subnet_type == 'dhcp6'.

    LP: #1847517

Signed-off-by: Eduardo Otubo <otubo@redhat.com>
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
---
 cloudinit/net/eni.py                               |  7 +-
 cloudinit/net/sysconfig.py                         |  7 +-
 cloudinit/sources/helpers/openstack.py             |  3 +-
 .../unittests/test_datasource/test_configdrive.py  | 39 ++++++++++
 tests/unittests/test_net.py                        | 87 +++++++++++++++++++++-
 5 files changed, 139 insertions(+), 4 deletions(-)

diff --git a/cloudinit/net/eni.py b/cloudinit/net/eni.py
index 6423632..896a39b 100644
--- a/cloudinit/net/eni.py
+++ b/cloudinit/net/eni.py
@@ -405,8 +405,13 @@ class Renderer(renderer.Renderer):
                 else:
                     ipv4_subnet_mtu = subnet.get('mtu')
                 iface['inet'] = subnet_inet
-                if subnet['type'].startswith('dhcp'):
+                if (subnet['type'] == 'dhcp4' or subnet['type'] == 'dhcp6' or
+                        subnet['type'] == 'ipv6_dhcpv6-stateful'):
+                    # Configure network settings using DHCP or DHCPv6
                     iface['mode'] = 'dhcp'
+                elif subnet['type'] == 'ipv6_dhcpv6-stateless':
+                    # Configure network settings using SLAAC from RAs
+                    iface['mode'] = 'auto'
 
                 # do not emit multiple 'auto $IFACE' lines as older (precise)
                 # ifupdown complains
diff --git a/cloudinit/net/sysconfig.py b/cloudinit/net/sysconfig.py
index a4c7660..8b11dbb 100644
--- a/cloudinit/net/sysconfig.py
+++ b/cloudinit/net/sysconfig.py
@@ -328,10 +328,15 @@ class Renderer(renderer.Renderer):
         for i, subnet in enumerate(subnets, start=len(iface_cfg.children)):
             mtu_key = 'MTU'
             subnet_type = subnet.get('type')
-            if subnet_type == 'dhcp6':
+            if subnet_type == 'dhcp6' or subnet_type == 'ipv6_dhcpv6-stateful':
                 # TODO need to set BOOTPROTO to dhcp6 on SUSE
                 iface_cfg['IPV6INIT'] = True
+                # Configure network settings using DHCPv6
                 iface_cfg['DHCPV6C'] = True
+            elif subnet_type == 'ipv6_dhcpv6-stateless':
+                iface_cfg['IPV6INIT'] = True
+                # Configure network settings using SLAAC from RAs
+                iface_cfg['IPV6_AUTOCONF'] = True
             elif subnet_type in ['dhcp4', 'dhcp']:
                 iface_cfg['BOOTPROTO'] = 'dhcp'
             elif subnet_type == 'static':
diff --git a/cloudinit/sources/helpers/openstack.py b/cloudinit/sources/helpers/openstack.py
index 9c29cea..9f2fd2d 100644
--- a/cloudinit/sources/helpers/openstack.py
+++ b/cloudinit/sources/helpers/openstack.py
@@ -585,7 +585,8 @@ def convert_net_json(network_json=None, known_macs=None):
             subnet = dict((k, v) for k, v in network.items()
                           if k in valid_keys['subnet'])
             if 'dhcp' in network['type']:
-                t = 'dhcp6' if network['type'].startswith('ipv6') else 'dhcp4'
+                t = (network['type'] if network['type'].startswith('ipv6')
+                     else 'dhcp4')
                 subnet.update({
                     'type': t,
                 })
diff --git a/tests/unittests/test_datasource/test_configdrive.py b/tests/unittests/test_datasource/test_configdrive.py
index dcdabea..ed4e9d5 100644
--- a/tests/unittests/test_datasource/test_configdrive.py
+++ b/tests/unittests/test_datasource/test_configdrive.py
@@ -503,6 +503,45 @@ class TestNetJson(CiTestCase):
                                                     known_macs=KNOWN_MACS)
         self.assertEqual(myds.network_config, network_config)
 
+    def test_network_config_conversion_dhcp6(self):
+        """Test some ipv6 input network json and check the expected
+           conversions."""
+        in_data = {
+            'links': [
+                {'vif_id': '2ecc7709-b3f7-4448-9580-e1ec32d75bbd',
+                 'ethernet_mac_address': 'fa:16:3e:69:b0:58',
+                 'type': 'ovs', 'mtu': None, 'id': 'tap2ecc7709-b3'},
+                {'vif_id': '2f88d109-5b57-40e6-af32-2472df09dc33',
+                 'ethernet_mac_address': 'fa:16:3e:d4:57:ad',
+                 'type': 'ovs', 'mtu': None, 'id': 'tap2f88d109-5b'},
+            ],
+            'networks': [
+                {'link': 'tap2ecc7709-b3', 'type': 'ipv6_dhcpv6-stateless',
+                 'network_id': '6d6357ac-0f70-4afa-8bd7-c274cc4ea235',
+                 'id': 'network0'},
+                {'link': 'tap2f88d109-5b', 'type': 'ipv6_dhcpv6-stateful',
+                 'network_id': 'd227a9b3-6960-4d94-8976-ee5788b44f54',
+                 'id': 'network1'},
+            ]
+        }
+        out_data = {
+            'version': 1,
+            'config': [
+                {'mac_address': 'fa:16:3e:69:b0:58',
+                 'mtu': None,
+                 'name': 'enp0s1',
+                 'subnets': [{'type': 'ipv6_dhcpv6-stateless'}],
+                 'type': 'physical'},
+                {'mac_address': 'fa:16:3e:d4:57:ad',
+                 'mtu': None,
+                 'name': 'enp0s2',
+                 'subnets': [{'type': 'ipv6_dhcpv6-stateful'}],
+                 'type': 'physical'}
+            ],
+        }
+        conv_data = openstack.convert_net_json(in_data, known_macs=KNOWN_MACS)
+        self.assertEqual(out_data, conv_data)
+
     def test_network_config_conversions(self):
         """Tests a bunch of input network json and checks the
            expected conversions."""
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
index df5658d..70d13f3 100644
--- a/tests/unittests/test_net.py
+++ b/tests/unittests/test_net.py
@@ -712,6 +712,82 @@ NETWORK_CONFIGS = {
                 """),
         },
     },
+    'dhcpv6_stateless': {
+        'expected_eni': textwrap.dedent("""\
+        auto lo
+        iface lo inet loopback
+
+        auto iface0
+        iface iface0 inet6 auto
+    """).rstrip(' '),
+        'expected_netplan': textwrap.dedent("""
+        network:
+            version: 2
+            ethernets:
+                iface0:
+                    dhcp6: true
+    """).rstrip(' '),
+        'yaml': textwrap.dedent("""\
+        version: 1
+        config:
+          - type: 'physical'
+            name: 'iface0'
+            subnets:
+            - {'type': 'ipv6_dhcpv6-stateless'}
+    """).rstrip(' '),
+        'expected_sysconfig': {
+            'ifcfg-iface0': textwrap.dedent("""\
+            BOOTPROTO=none
+            DEVICE=iface0
+            IPV6_AUTOCONF=yes
+            IPV6INIT=yes
+            DEVICE=iface0
+            NM_CONTROLLED=no
+            ONBOOT=yes
+            STARTMODE=auto
+            TYPE=Ethernet
+            USERCTL=no
+            """),
+        },
+    },
+    'dhcpv6_stateful': {
+        'expected_eni': textwrap.dedent("""\
+        auto lo
+        iface lo inet loopback
+
+        auto iface0
+        iface iface0 inet6 dhcp
+    """).rstrip(' '),
+        'expected_netplan': textwrap.dedent("""
+        network:
+            version: 2
+            ethernets:
+                iface0:
+                    dhcp6: true
+    """).rstrip(' '),
+        'yaml': textwrap.dedent("""\
+        version: 1
+        config:
+          - type: 'physical'
+            name: 'iface0'
+            subnets:
+            - {'type': 'ipv6_dhcpv6-stateful'}
+    """).rstrip(' '),
+        'expected_sysconfig': {
+            'ifcfg-iface0': textwrap.dedent("""\
+            BOOTPROTO=none
+            DEVICE=iface0
+            DHCPV6C=yes
+            IPV6INIT=yes
+            DEVICE=iface0
+            NM_CONTROLLED=no
+            ONBOOT=yes
+            STARTMODE=auto
+            TYPE=Ethernet
+            USERCTL=no
+            """),
+        },
+    },
     'all': {
         'expected_eni': ("""\
 auto lo
@@ -2300,8 +2376,11 @@ USERCTL=no
             }
         }
 
+
+    def test_dhcpv6_stateless_config(self):
+        entry = NETWORK_CONFIGS['dhcpv6_stateless']
         found = self._render_and_read(network_config=yaml.load(entry['yaml']))
-        self._compare_files_to_expected(entry['expected_sysconfig'], found)
+        self._compare_files_to_expected(entry[self.expected_name], found)
         self._assert_headers(found)
 
     def test_from_v2_route_metric(self):
@@ -2334,6 +2413,12 @@ USERCTL=no
             self._compare_files_to_expected(
                 expected, self._render_and_read(network_config=v2data))
 
+    def test_dhcpv6_stateful_config(self):
+        entry = NETWORK_CONFIGS['dhcpv6_stateful']
+        found = self._render_and_read(network_config=yaml.load(entry['yaml']))
+        self._compare_files_to_expected(entry[self.expected_name], found)
+        self._assert_headers(found)
+
 
 class TestOpenSuseSysConfigRendering(CiTestCase):
 
-- 
1.8.3.1