252d83
From 51a90ecbdf1f3900183d8ec641eeb4571decf6dc Mon Sep 17 00:00:00 2001
252d83
From: Eduardo Otubo <otubo@redhat.com>
252d83
Date: Wed, 4 Nov 2020 12:37:54 +0100
252d83
Subject: [PATCH] network: Fix type and respect name when rendering vlan in
252d83
 sysconfig. (#541)
252d83
252d83
RH-Author: Eduardo Terrell Ferrari Otubo (eterrell)
252d83
RH-MergeRequest: 19: network: Fix type and respect name when rendering vlan in sysconfig. (#541)
252d83
RH-Commit: [1/1] 75bea46017397082c5763125a5f35806c2f840e9 (eterrell/cloud-init)
252d83
RH-Bugzilla: 1881462
252d83
252d83
commit 8439b191ec2f336d544cab86dba2860f969cd5b8
252d83
Author: Eduardo Otubo <otubo@redhat.com>
252d83
Date:   Tue Sep 15 18:00:00 2020 +0200
252d83
252d83
    network: Fix type and respect name when rendering vlan in sysconfig. (#541)
252d83
252d83
    Prior to this change, vlans were rendered in sysconfig with
252d83
    'TYPE=Ethernet', and incorrectly rendered the PHYSDEV based on
252d83
    the name of the vlan device rather than the 'link' provided
252d83
    in the network config.
252d83
252d83
    The change here fixes:
252d83
     * rendering of TYPE=Ethernet for a vlan
252d83
     * adds a warning if the configured device name is not supported
252d83
       per the RHEL 7 docs "11.5. Naming Scheme for VLAN Interfaces"
252d83
252d83
    LP: #1788915
252d83
    LP: #1826608
252d83
    RHBZ: #1861871
252d83
252d83
Signed-off-by: Eduardo Otubo <otubo@redhat.com>
252d83
---
252d83
 cloudinit/net/sysconfig.py                     | 32 +++++++++-
252d83
 tests/unittests/test_distros/test_netconfig.py | 81 ++++++++++++++++++++++++++
252d83
 tests/unittests/test_net.py                    |  4 --
252d83
 3 files changed, 112 insertions(+), 5 deletions(-)
252d83
252d83
diff --git a/cloudinit/net/sysconfig.py b/cloudinit/net/sysconfig.py
252d83
index c078898..078636a 100644
252d83
--- a/cloudinit/net/sysconfig.py
252d83
+++ b/cloudinit/net/sysconfig.py
252d83
@@ -99,6 +99,10 @@ class ConfigMap(object):
252d83
     def __len__(self):
252d83
         return len(self._conf)
252d83
 
252d83
+    def skip_key_value(self, key, val):
252d83
+        """Skip the pair key, value if it matches a certain rule."""
252d83
+        return False
252d83
+
252d83
     def to_string(self):
252d83
         buf = io.StringIO()
252d83
         buf.write(_make_header())
252d83
@@ -106,6 +110,8 @@ class ConfigMap(object):
252d83
             buf.write("\n")
252d83
         for key in sorted(self._conf.keys()):
252d83
             value = self._conf[key]
252d83
+            if self.skip_key_value(key, value):
252d83
+                continue
252d83
             if isinstance(value, bool):
252d83
                 value = self._bool_map[value]
252d83
             if not isinstance(value, str):
252d83
@@ -214,6 +220,7 @@ class NetInterface(ConfigMap):
252d83
         'bond': 'Bond',
252d83
         'bridge': 'Bridge',
252d83
         'infiniband': 'InfiniBand',
252d83
+        'vlan': 'Vlan',
252d83
     }
252d83
 
252d83
     def __init__(self, iface_name, base_sysconf_dir, templates,
252d83
@@ -267,6 +274,11 @@ class NetInterface(ConfigMap):
252d83
             c.routes = self.routes.copy()
252d83
         return c
252d83
 
252d83
+    def skip_key_value(self, key, val):
252d83
+        if key == 'TYPE' and val == 'Vlan':
252d83
+            return True
252d83
+        return False
252d83
+
252d83
 
252d83
 class Renderer(renderer.Renderer):
252d83
     """Renders network information in a /etc/sysconfig format."""
252d83
@@ -701,7 +713,16 @@ class Renderer(renderer.Renderer):
252d83
                 iface_cfg['ETHERDEVICE'] = iface_name[:iface_name.rfind('.')]
252d83
             else:
252d83
                 iface_cfg['VLAN'] = True
252d83
-                iface_cfg['PHYSDEV'] = iface_name[:iface_name.rfind('.')]
252d83
+                iface_cfg.kind = 'vlan'
252d83
+
252d83
+                rdev = iface['vlan-raw-device']
252d83
+                supported = _supported_vlan_names(rdev, iface['vlan_id'])
252d83
+                if iface_name not in supported:
252d83
+                    LOG.info(
252d83
+                        "Name '%s' for vlan '%s' is not officially supported"
252d83
+                        "by RHEL. Supported: %s",
252d83
+                        iface_name, rdev, ' '.join(supported))
252d83
+                iface_cfg['PHYSDEV'] = rdev
252d83
 
252d83
             iface_subnets = iface.get("subnets", [])
252d83
             route_cfg = iface_cfg.routes
252d83
@@ -909,6 +930,15 @@ class Renderer(renderer.Renderer):
252d83
                             "\n".join(netcfg) + "\n", file_mode)
252d83
 
252d83
 
252d83
+def _supported_vlan_names(rdev, vid):
252d83
+    """Return list of supported names for vlan devices per RHEL doc
252d83
+    11.5. Naming Scheme for VLAN Interfaces."""
252d83
+    return [
252d83
+        v.format(rdev=rdev, vid=int(vid))
252d83
+        for v in ("{rdev}{vid:04}", "{rdev}{vid}",
252d83
+                  "{rdev}.{vid:04}", "{rdev}.{vid}")]
252d83
+
252d83
+
252d83
 def available(target=None):
252d83
     sysconfig = available_sysconfig(target=target)
252d83
     nm = available_nm(target=target)
252d83
diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
252d83
index f9fc3a1..a1df066 100644
252d83
--- a/tests/unittests/test_distros/test_netconfig.py
252d83
+++ b/tests/unittests/test_distros/test_netconfig.py
252d83
@@ -541,6 +541,87 @@ class TestNetCfgDistroRedhat(TestNetCfgDistroBase):
252d83
                                V1_NET_CFG_IPV6,
252d83
                                expected_cfgs=expected_cfgs.copy())
252d83
 
252d83
+    def test_vlan_render_unsupported(self):
252d83
+        """Render officially unsupported vlan names."""
252d83
+        cfg = {
252d83
+            'version': 2,
252d83
+            'ethernets': {
252d83
+                'eth0': {'addresses': ["192.10.1.2/24"],
252d83
+                         'match': {'macaddress': "00:16:3e:60:7c:df"}}},
252d83
+            'vlans': {
252d83
+                'infra0': {'addresses': ["10.0.1.2/16"],
252d83
+                           'id': 1001, 'link': 'eth0'}},
252d83
+        }
252d83
+        expected_cfgs = {
252d83
+            self.ifcfg_path('eth0'): dedent("""\
252d83
+                BOOTPROTO=none
252d83
+                DEVICE=eth0
252d83
+                HWADDR=00:16:3e:60:7c:df
252d83
+                IPADDR=192.10.1.2
252d83
+                NETMASK=255.255.255.0
252d83
+                NM_CONTROLLED=no
252d83
+                ONBOOT=yes
252d83
+                TYPE=Ethernet
252d83
+                USERCTL=no
252d83
+                """),
252d83
+            self.ifcfg_path('infra0'): dedent("""\
252d83
+                BOOTPROTO=none
252d83
+                DEVICE=infra0
252d83
+                IPADDR=10.0.1.2
252d83
+                NETMASK=255.255.0.0
252d83
+                NM_CONTROLLED=no
252d83
+                ONBOOT=yes
252d83
+                PHYSDEV=eth0
252d83
+                USERCTL=no
252d83
+                VLAN=yes
252d83
+                """),
252d83
+            self.control_path(): dedent("""\
252d83
+                NETWORKING=yes
252d83
+                """),
252d83
+        }
252d83
+        self._apply_and_verify(
252d83
+            self.distro.apply_network_config, cfg,
252d83
+            expected_cfgs=expected_cfgs)
252d83
+
252d83
+    def test_vlan_render(self):
252d83
+        cfg = {
252d83
+            'version': 2,
252d83
+            'ethernets': {
252d83
+                'eth0': {'addresses': ["192.10.1.2/24"]}},
252d83
+            'vlans': {
252d83
+                'eth0.1001': {'addresses': ["10.0.1.2/16"],
252d83
+                              'id': 1001, 'link': 'eth0'}},
252d83
+        }
252d83
+        expected_cfgs = {
252d83
+            self.ifcfg_path('eth0'): dedent("""\
252d83
+                BOOTPROTO=none
252d83
+                DEVICE=eth0
252d83
+                IPADDR=192.10.1.2
252d83
+                NETMASK=255.255.255.0
252d83
+                NM_CONTROLLED=no
252d83
+                ONBOOT=yes
252d83
+                TYPE=Ethernet
252d83
+                USERCTL=no
252d83
+                """),
252d83
+            self.ifcfg_path('eth0.1001'): dedent("""\
252d83
+                BOOTPROTO=none
252d83
+                DEVICE=eth0.1001
252d83
+                IPADDR=10.0.1.2
252d83
+                NETMASK=255.255.0.0
252d83
+                NM_CONTROLLED=no
252d83
+                ONBOOT=yes
252d83
+                PHYSDEV=eth0
252d83
+                USERCTL=no
252d83
+                VLAN=yes
252d83
+                """),
252d83
+            self.control_path(): dedent("""\
252d83
+                NETWORKING=yes
252d83
+                """),
252d83
+        }
252d83
+        self._apply_and_verify(
252d83
+            self.distro.apply_network_config, cfg,
252d83
+            expected_cfgs=expected_cfgs)
252d83
+
252d83
 
252d83
 class TestNetCfgDistroOpensuse(TestNetCfgDistroBase):
252d83
 
252d83
diff --git a/tests/unittests/test_net.py b/tests/unittests/test_net.py
252d83
index d7a7a65..c033745 100644
252d83
--- a/tests/unittests/test_net.py
252d83
+++ b/tests/unittests/test_net.py
252d83
@@ -1656,7 +1656,6 @@ pre-down route del -net 10.0.0.0/8 gw 11.0.0.1 metric 3 || true
252d83
                 DHCLIENT_SET_DEFAULT_ROUTE=no
252d83
                 ONBOOT=yes
252d83
                 PHYSDEV=bond0
252d83
-                TYPE=Ethernet
252d83
                 USERCTL=no
252d83
                 VLAN=yes"""),
252d83
             'ifcfg-br0': textwrap.dedent("""\
252d83
@@ -1699,7 +1698,6 @@ pre-down route del -net 10.0.0.0/8 gw 11.0.0.1 metric 3 || true
252d83
                 NETMASK1=255.255.255.0
252d83
                 ONBOOT=yes
252d83
                 PHYSDEV=eth0
252d83
-                TYPE=Ethernet
252d83
                 USERCTL=no
252d83
                 VLAN=yes"""),
252d83
             'ifcfg-eth1': textwrap.dedent("""\
252d83
@@ -2302,7 +2300,6 @@ iface bond0 inet6 static
252d83
                 NETMASK1=255.255.255.0
252d83
                 ONBOOT=yes
252d83
                 PHYSDEV=en0
252d83
-                TYPE=Ethernet
252d83
                 USERCTL=no
252d83
                 VLAN=yes"""),
252d83
         },
252d83
@@ -3409,7 +3406,6 @@ USERCTL=no
252d83
                 NM_CONTROLLED=no
252d83
                 ONBOOT=yes
252d83
                 PHYSDEV=eno1
252d83
-                TYPE=Ethernet
252d83
                 USERCTL=no
252d83
                 VLAN=yes
252d83
                 """)
252d83
-- 
252d83
1.8.3.1
252d83