← Back to team overview

cloud-init-dev team mailing list archive

[Merge] ~loki-8/cloud-init:fix/1792454-netork-configuration-v2-and-translate_network into cloud-init:master

 

Thomas Berger has proposed merging ~loki-8/cloud-init:fix/1792454-netork-configuration-v2-and-translate_network into cloud-init:master.

Commit message:
"restore" the netmask in translate_network for v2

if a DataSource does provide a network configuration in version 2,
the result of net_util.translate_network lacks a netmask.

But the netmask key is used by multiple distributions.

We check for a missing netmask and the existens of a network prefix
in translate_network and restore the original netmask to keep
compatibility for these distribtuions until they movde to
the v2 network configuration.

LP: #1792454


Requested reviews:
  cloud-init commiters (cloud-init-dev)

For more details, see:
https://code.launchpad.net/~loki-8/cloud-init/+git/cloud-init/+merge/354893
-- 
Your team cloud-init commiters is requested to review the proposed merge of ~loki-8/cloud-init:fix/1792454-netork-configuration-v2-and-translate_network into cloud-init:master.
diff --git a/cloudinit/distros/net_util.py b/cloudinit/distros/net_util.py
index 1ce1aa7..dd5ccfa 100644
--- a/cloudinit/distros/net_util.py
+++ b/cloudinit/distros/net_util.py
@@ -67,6 +67,10 @@
 #     }
 # }
 
+from cloudinit.net.network_state import (
+    net_prefix_to_ipv4_mask, mask_and_ipv4_to_bcast_addr)
+
+
 def translate_network(settings):
     # Get the standard cmd, args from the ubuntu format
     entries = []
@@ -134,6 +138,20 @@ def translate_network(settings):
                     val = info[k].strip().lower()
                     if val:
                         iface_info[k] = val
+            # for network configuration v2, no netmask is provided
+            if 'address' in iface_info:
+                if 'netmask' not in iface_info:
+                    # check if the address has a network prefix
+                    addr, _, prefix = iface_info['address'].partition('/')
+                    if prefix:
+                        iface_info['netmask'] = \
+                            net_prefix_to_ipv4_mask(prefix)
+                        iface_info['address'] = addr
+                        # if we set the netmask, we also can set the broadcast
+                        iface_info['broadcast'] = \
+                            mask_and_ipv4_to_bcast_addr(
+                                iface_info['netmask'], addr)
+
             # Name server info provided??
             if 'dns-nameservers' in info:
                 iface_info['dns-nameservers'] = info['dns-nameservers'].split()
diff --git a/tests/unittests/test_distros/test_netconfig.py b/tests/unittests/test_distros/test_netconfig.py
index 740fb76..4d66e51 100644
--- a/tests/unittests/test_distros/test_netconfig.py
+++ b/tests/unittests/test_distros/test_netconfig.py
@@ -34,6 +34,19 @@ auto eth1
 iface eth1 inet dhcp
 '''
 
+BASE_NET_CFG_FROM_V2 = '''
+auto lo
+iface lo inet loopback
+
+auto eth0
+iface eth0 inet static
+    address 192.168.1.5/24
+    gateway 192.168.1.254
+
+auto eth1
+iface eth1 inet dhcp
+'''
+
 BASE_NET_CFG_IPV6 = '''
 auto lo
 iface lo inet loopback
@@ -457,6 +470,35 @@ class TestNetCfgDistroRedhat(TestNetCfgDistroBase):
                                BASE_NET_CFG,
                                expected_cfgs=expected_cfgs.copy())
 
+    def test_simple_write_rh_from_v2eni(self):
+        expected_cfgs = {
+            self.ifcfg_path('lo'): dedent("""\
+                DEVICE="lo"
+                ONBOOT=yes
+                """),
+            self.ifcfg_path('eth0'): dedent("""\
+                DEVICE="eth0"
+                BOOTPROTO="static"
+                NETMASK="255.255.255.0"
+                IPADDR="192.168.1.5"
+                ONBOOT=yes
+                GATEWAY="192.168.1.254"
+                BROADCAST="192.168.1.255"
+                """),
+            self.ifcfg_path('eth1'): dedent("""\
+                DEVICE="eth1"
+                BOOTPROTO="dhcp"
+                ONBOOT=yes
+                """),
+            self.control_path(): dedent("""\
+                NETWORKING=yes
+                """),
+        }
+        # rh_distro.apply_network(BASE_NET_CFG, False)
+        self._apply_and_verify(self.distro.apply_network,
+                               BASE_NET_CFG_FROM_V2,
+                               expected_cfgs=expected_cfgs.copy())
+
     def test_apply_network_config_rh(self):
         expected_cfgs = {
             self.ifcfg_path('eth0'): dedent("""\

Follow ups