Ryan Harper has proposed merging 
~raharper/cloud-init:fix/oracle-second-vnics-config-update into 
cloud-init:master.

Commit message:
Oracle: Render secondary vnic IP and MTU values only
    
When rendering secondary vnic configuration from IMDS, only emit
configuration for the IP and MTU values only.  Add support to mutate
either a v1 or a v2 network_config input.

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

For more details, see:
https://code.launchpad.net/~raharper/cloud-init/+git/cloud-init/+merge/371906
-- 
Your team cloud-init commiters is requested to review the proposed merge of 
~raharper/cloud-init:fix/oracle-second-vnics-config-update into 
cloud-init:master.
diff --git a/cloudinit/sources/DataSourceOracle.py b/cloudinit/sources/DataSourceOracle.py
index 6e73f56..5a5e5af 100644
--- a/cloudinit/sources/DataSourceOracle.py
+++ b/cloudinit/sources/DataSourceOracle.py
@@ -51,8 +51,8 @@ def _add_network_config_from_opc_imds(network_config):
     include the secondary VNICs.
 
     :param network_config:
-        A v1 network config dict with the primary NIC already configured.  This
-        dict will be mutated.
+        A v1 or v2 network config dict with the primary NIC already configured.
+        This dict will be mutated.
 
     :raises:
         Exceptions are not handled within this function.  Likely exceptions are
@@ -88,20 +88,27 @@ def _add_network_config_from_opc_imds(network_config):
             LOG.debug('Interface with MAC %s not found; skipping', mac_address)
             continue
         name = interfaces_by_mac[mac_address]
-        subnet = {
-            'type': 'static',
-            'address': vnic_dict['privateIp'],
-            'netmask': vnic_dict['subnetCidrBlock'].split('/')[1],
-            'gateway': vnic_dict['virtualRouterIp'],
-            'control': 'manual',
-        }
-        network_config['config'].append({
-            'name': name,
-            'type': 'physical',
-            'mac_address': mac_address,
-            'mtu': MTU,
-            'subnets': [subnet],
-        })
+
+        if network_config['version'] == 1:
+            subnet = {
+                'type': 'static',
+                'address': vnic_dict['privateIp'],
+                'netmask': vnic_dict['subnetCidrBlock'].split('/')[1],
+                'gateway': vnic_dict['virtualRouterIp'],
+                'control': 'manual',
+            }
+            network_config['config'].append({
+                'name': name,
+                'type': 'physical',
+                'mac_address': mac_address,
+                'mtu': MTU,
+                'subnets': [subnet],
+            })
+        elif network_config['version'] == 2:
+            network_config['ethernets'][name] = {
+                'addresses': [vnic_dict['privateIp']],
+                'mtu': MTU, 'dhcp4': False, 'dhcp6': False,
+                'match': {'macaddress': mac_address}}
 
 
 class DataSourceOracle(sources.DataSource):
diff --git a/cloudinit/sources/tests/test_oracle.py b/cloudinit/sources/tests/test_oracle.py
index 3ddf7df..f7864ae 100644
--- a/cloudinit/sources/tests/test_oracle.py
+++ b/cloudinit/sources/tests/test_oracle.py
@@ -526,6 +526,18 @@ class TestNetworkConfigFromOpcImds(test_helpers.CiTestCase):
             'Interface with MAC 00:00:17:02:2b:b1 not found; skipping',
             self.logs.getvalue())
 
+    def test_missing_mac_skipped_v2(self):
+        self.m_readurl.return_value = OPC_VM_SECONDARY_VNIC_RESPONSE
+        self.m_get_interfaces_by_mac.return_value = {}
+
+        network_config = {'version': 2, 'ethernets': {'primary': {'nic': {}}}}
+        oracle._add_network_config_from_opc_imds(network_config)
+
+        self.assertEqual(1, len(network_config['ethernets']))
+        self.assertIn(
+            'Interface with MAC 00:00:17:02:2b:b1 not found; skipping',
+            self.logs.getvalue())
+
     def test_secondary_nic(self):
         self.m_readurl.return_value = OPC_VM_SECONDARY_VNIC_RESPONSE
         mac_addr, nic_name = '00:00:17:02:2b:b1', 'ens3'
@@ -553,4 +565,28 @@ class TestNetworkConfigFromOpcImds(test_helpers.CiTestCase):
         self.assertEqual('10.0.0.1', subnet_cfg['gateway'])
         self.assertEqual('manual', subnet_cfg['control'])
 
+    def test_secondary_nic_v2(self):
+        self.m_readurl.return_value = OPC_VM_SECONDARY_VNIC_RESPONSE
+        mac_addr, nic_name = '00:00:17:02:2b:b1', 'ens3'
+        self.m_get_interfaces_by_mac.return_value = {
+            mac_addr: nic_name,
+        }
+
+        network_config = {'version': 2, 'ethernets': {'primary': {'nic': {}}}}
+        oracle._add_network_config_from_opc_imds(network_config)
+
+        # The input is mutated
+        self.assertEqual(2, len(network_config['ethernets']))
+
+        secondary_nic_cfg = network_config['ethernets']['ens3']
+        self.assertFalse(secondary_nic_cfg['dhcp4'])
+        self.assertFalse(secondary_nic_cfg['dhcp6'])
+        self.assertEqual(mac_addr, secondary_nic_cfg['match']['macaddress'])
+        self.assertEqual(9000, secondary_nic_cfg['mtu'])
+
+        self.assertEqual(1, len(secondary_nic_cfg['addresses']))
+        # These values are hard-coded in OPC_VM_SECONDARY_VNIC_RESPONSE
+        self.assertEqual('10.0.0.231', secondary_nic_cfg['addresses'][0])
+
+
 # vi: ts=4 expandtab
_______________________________________________
Mailing list: https://launchpad.net/~cloud-init-dev
Post to     : cloud-init-dev@lists.launchpad.net
Unsubscribe : https://launchpad.net/~cloud-init-dev
More help   : https://help.launchpad.net/ListHelp

Reply via email to