4e5835
From 12b8abf9bf245393cf4dc0bf2b0595bf4f3fbff1 Mon Sep 17 00:00:00 2001
4e5835
From: Eduardo Otubo <otubo@redhat.com>
4e5835
Date: Mon, 26 Oct 2020 14:17:23 -0400
4e5835
Subject: [PATCH] network: Fix type and respect name when rendering vlan in
4e5835
 sysconfig. (#541)
4e5835
4e5835
RH-Author: Eduardo Otubo <otubo@redhat.com>
4e5835
Message-id: <20201026141723.31631-1-otubo@redhat.com>
4e5835
Patchwork-id: 98714
4e5835
O-Subject: [RHEL-7.9.z cloud-init PATCH] network: Fix type and respect name when rendering vlan in sysconfig. (#541)
4e5835
Bugzilla: 1861871
4e5835
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
4e5835
RH-Acked-by: Cathy Avery <cavery@redhat.com>
4e5835
4e5835
BZ: 1861871
4e5835
BRANCH: rhel7/master-19.4
4e5835
BREW: 32418261
4e5835
4e5835
commit 8439b191ec2f336d544cab86dba2860f969cd5b8
4e5835
Author: Eduardo Otubo <otubo@redhat.com>
4e5835
Date:   Tue Sep 15 18:00:00 2020 +0200
4e5835
4e5835
    network: Fix type and respect name when rendering vlan in sysconfig. (#541)
4e5835
4e5835
    Prior to this change, vlans were rendered in sysconfig with
4e5835
    'TYPE=Ethernet', and incorrectly rendered the PHYSDEV based on
4e5835
    the name of the vlan device rather than the 'link' provided
4e5835
    in the network config.
4e5835
4e5835
    The change here fixes:
4e5835
     * rendering of TYPE=Ethernet for a vlan
4e5835
     * adds a warning if the configured device name is not supported
4e5835
       per the RHEL 7 docs "11.5. Naming Scheme for VLAN Interfaces"
4e5835
4e5835
    LP: #1788915
4e5835
    LP: #1826608
4e5835
    RHBZ: #1861871
4e5835
4e5835
Conflicts:
4e5835
* A hunk on cloudinit/net/sysconfig.py could not apply cleanly as it
4e5835
depends on a verification on the distro flavor, which is not implemented
4e5835
on cloud-init-19.4.
4e5835
* Couple of hunks could not apply cleanly on tests/unittests/test_net.py
4e5835
because the definition of unit test response moved a little bit.
4e5835
4e5835
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
4e5835
Signed-off-by: Jon Maloy <jmaloy.redhat.com>
4e5835
---
4e5835
 cloudinit/net/sysconfig.py                    | 32 +++++++-
4e5835
 .../unittests/test_distros/test_netconfig.py  | 81 +++++++++++++++++++
4e5835
 tests/unittests/test_net.py                   |  4 -
4e5835
 3 files changed, 112 insertions(+), 5 deletions(-)
4e5835
4e5835
diff --git a/cloudinit/net/sysconfig.py b/cloudinit/net/sysconfig.py
4e5835
index 810b2830..4b4ed097 100644
4e5835
--- a/cloudinit/net/sysconfig.py
4e5835
+++ b/cloudinit/net/sysconfig.py
4e5835
@@ -95,6 +95,10 @@ class ConfigMap(object):
4e5835
     def __len__(self):
4e5835
         return len(self._conf)
4e5835
 
4e5835
+    def skip_key_value(self, key, val):
4e5835
+        """Skip the pair key, value if it matches a certain rule."""
4e5835
+        return False
4e5835
+
4e5835
     def to_string(self):
4e5835
         buf = six.StringIO()
4e5835
         buf.write(_make_header())
4e5835
@@ -102,6 +106,8 @@ class ConfigMap(object):
4e5835
             buf.write("\n")
4e5835
         for key in sorted(self._conf.keys()):
4e5835
             value = self._conf[key]
4e5835
+            if self.skip_key_value(key, value):
4e5835
+                continue
4e5835
             if isinstance(value, bool):
4e5835
                 value = self._bool_map[value]
4e5835
             if not isinstance(value, six.string_types):
4e5835
@@ -207,6 +213,7 @@ class NetInterface(ConfigMap):
4e5835
         'bond': 'Bond',
4e5835
         'bridge': 'Bridge',
4e5835
         'infiniband': 'InfiniBand',
4e5835
+        'vlan': 'Vlan',
4e5835
     }
4e5835
 
4e5835
     def __init__(self, iface_name, base_sysconf_dir, templates,
4e5835
@@ -260,6 +267,11 @@ class NetInterface(ConfigMap):
4e5835
             c.routes = self.routes.copy()
4e5835
         return c
4e5835
 
4e5835
+    def skip_key_value(self, key, val):
4e5835
+        if key == 'TYPE' and val == 'Vlan':
4e5835
+            return True
4e5835
+        return False
4e5835
+
4e5835
 
4e5835
 class Renderer(renderer.Renderer):
4e5835
     """Renders network information in a /etc/sysconfig format."""
4e5835
@@ -599,7 +611,16 @@ class Renderer(renderer.Renderer):
4e5835
             iface_name = iface['name']
4e5835
             iface_cfg = iface_contents[iface_name]
4e5835
             iface_cfg['VLAN'] = True
4e5835
-            iface_cfg['PHYSDEV'] = iface_name[:iface_name.rfind('.')]
4e5835
+            iface_cfg.kind = 'vlan'
4e5835
+
4e5835
+            rdev = iface['vlan-raw-device']
4e5835
+            supported = _supported_vlan_names(rdev, iface['vlan_id'])
4e5835
+            if iface_name not in supported:
4e5835
+                LOG.info(
4e5835
+                    "Name '%s' for vlan '%s' is not officially supported"
4e5835
+                    "by RHEL. Supported: %s",
4e5835
+                    iface_name, rdev, ' '.join(supported))
4e5835
+            iface_cfg['PHYSDEV'] = rdev
4e5835
 
4e5835
             iface_subnets = iface.get("subnets", [])
4e5835
             route_cfg = iface_cfg.routes
4e5835
@@ -771,6 +792,15 @@ class Renderer(renderer.Renderer):
4e5835
                             "\n".join(netcfg) + "\n", file_mode)
4e5835
 
4e5835
 
4e5835
+def _supported_vlan_names(rdev, vid):
4e5835
+    """Return list of supported names for vlan devices per RHEL doc
4e5835
+    11.5. Naming Scheme for VLAN Interfaces."""
4e5835
+    return [
4e5835
+        v.format(rdev=rdev, vid=int(vid))
4e5835
+        for v in ("{rdev}{vid:04}", "{rdev}{vid}",
4e5835
+                  "{rdev}.{vid:04}", "{rdev}.{vid}")]
4e5835
+
4e5835
+
4e5835
 def available(target=None):
4e5835
     sysconfig = available_sysconfig(target=target)
4e5835
     nm = available_nm(target=target)
4e5835
diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
4e5835
index 67209955..4ea42030 100644
4e5835
--- a/tests/unittests/test_distros/test_netconfig.py
4e5835
+++ b/tests/unittests/test_distros/test_netconfig.py
4e5835
@@ -526,6 +526,87 @@ class TestNetCfgDistroRedhat(TestNetCfgDistroBase):
4e5835
                                V1_NET_CFG_IPV6,
4e5835
                                expected_cfgs=expected_cfgs.copy())
4e5835
 
4e5835
+    def test_vlan_render_unsupported(self):
4e5835
+        """Render officially unsupported vlan names."""
4e5835
+        cfg = {
4e5835
+            'version': 2,
4e5835
+            'ethernets': {
4e5835
+                'eth0': {'addresses': ["192.10.1.2/24"],
4e5835
+                         'match': {'macaddress': "00:16:3e:60:7c:df"}}},
4e5835
+            'vlans': {
4e5835
+                'infra0': {'addresses': ["10.0.1.2/16"],
4e5835
+                           'id': 1001, 'link': 'eth0'}},
4e5835
+        }
4e5835
+        expected_cfgs = {
4e5835
+            self.ifcfg_path('eth0'): dedent("""\
4e5835
+                BOOTPROTO=none
4e5835
+                DEVICE=eth0
4e5835
+                HWADDR=00:16:3e:60:7c:df
4e5835
+                IPADDR=192.10.1.2
4e5835
+                NETMASK=255.255.255.0
4e5835
+                NM_CONTROLLED=no
4e5835
+                ONBOOT=yes
4e5835
+                TYPE=Ethernet
4e5835
+                USERCTL=no
4e5835
+                """),
4e5835
+            self.ifcfg_path('infra0'): dedent("""\
4e5835
+                BOOTPROTO=none
4e5835
+                DEVICE=infra0
4e5835
+                IPADDR=10.0.1.2
4e5835
+                NETMASK=255.255.0.0
4e5835
+                NM_CONTROLLED=no
4e5835
+                ONBOOT=yes
4e5835
+                PHYSDEV=eth0
4e5835
+                USERCTL=no
4e5835
+                VLAN=yes
4e5835
+                """),
4e5835
+            self.control_path(): dedent("""\
4e5835
+                NETWORKING=yes
4e5835
+                """),
4e5835
+        }
4e5835
+        self._apply_and_verify(
4e5835
+            self.distro.apply_network_config, cfg,
4e5835
+            expected_cfgs=expected_cfgs)
4e5835
+
4e5835
+    def test_vlan_render(self):
4e5835
+        cfg = {
4e5835
+            'version': 2,
4e5835
+            'ethernets': {
4e5835
+                'eth0': {'addresses': ["192.10.1.2/24"]}},
4e5835
+            'vlans': {
4e5835
+                'eth0.1001': {'addresses': ["10.0.1.2/16"],
4e5835
+                              'id': 1001, 'link': 'eth0'}},
4e5835
+        }
4e5835
+        expected_cfgs = {
4e5835
+            self.ifcfg_path('eth0'): dedent("""\
4e5835
+                BOOTPROTO=none
4e5835
+                DEVICE=eth0
4e5835
+                IPADDR=192.10.1.2
4e5835
+                NETMASK=255.255.255.0
4e5835
+                NM_CONTROLLED=no
4e5835
+                ONBOOT=yes
4e5835
+                TYPE=Ethernet
4e5835
+                USERCTL=no
4e5835
+                """),
4e5835
+            self.ifcfg_path('eth0.1001'): dedent("""\
4e5835
+                BOOTPROTO=none
4e5835
+                DEVICE=eth0.1001
4e5835
+                IPADDR=10.0.1.2
4e5835
+                NETMASK=255.255.0.0
4e5835
+                NM_CONTROLLED=no
4e5835
+                ONBOOT=yes
4e5835
+                PHYSDEV=eth0
4e5835
+                USERCTL=no
4e5835
+                VLAN=yes
4e5835
+                """),
4e5835
+            self.control_path(): dedent("""\
4e5835
+                NETWORKING=yes
4e5835
+                """),
4e5835
+        }
4e5835
+        self._apply_and_verify(
4e5835
+            self.distro.apply_network_config, cfg,
4e5835
+            expected_cfgs=expected_cfgs)
4e5835
+
4e5835
 
4e5835
 class TestNetCfgDistroOpensuse(TestNetCfgDistroBase):
4e5835
 
4e5835
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
4e5835
index a931a3e4..2eedb127 100644
4e5835
--- a/tests/unittests/test_net.py
4e5835
+++ b/tests/unittests/test_net.py
4e5835
@@ -1496,7 +1496,6 @@ pre-down route del -net 10.0.0.0/8 gw 11.0.0.1 metric 3 || true
4e5835
                 ONBOOT=yes
4e5835
                 PHYSDEV=bond0
4e5835
                 STARTMODE=auto
4e5835
-                TYPE=Ethernet
4e5835
                 USERCTL=no
4e5835
                 VLAN=yes"""),
4e5835
             'ifcfg-br0': textwrap.dedent("""\
4e5835
@@ -1541,7 +1540,6 @@ pre-down route del -net 10.0.0.0/8 gw 11.0.0.1 metric 3 || true
4e5835
                 ONBOOT=yes
4e5835
                 PHYSDEV=eth0
4e5835
                 STARTMODE=auto
4e5835
-                TYPE=Ethernet
4e5835
                 USERCTL=no
4e5835
                 VLAN=yes"""),
4e5835
             'ifcfg-eth1': textwrap.dedent("""\
4e5835
@@ -2163,7 +2161,6 @@ iface bond0 inet6 static
4e5835
                 ONBOOT=yes
4e5835
                 PHYSDEV=en0
4e5835
                 STARTMODE=auto
4e5835
-                TYPE=Ethernet
4e5835
                 USERCTL=no
4e5835
                 VLAN=yes"""),
4e5835
         },
4e5835
@@ -3180,7 +3177,6 @@ USERCTL=no
4e5835
                 ONBOOT=yes
4e5835
                 PHYSDEV=eno1
4e5835
                 STARTMODE=auto
4e5835
-                TYPE=Ethernet
4e5835
                 USERCTL=no
4e5835
                 VLAN=yes
4e5835
                 """)
4e5835
-- 
4e5835
2.18.2
4e5835