From f78229f659a75cfa35f155a1d36077c61ed70ba4 Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Mon, 15 Jun 2020 16:05:17 +0200 Subject: [PATCH] Allow setting other fields on discovery Adds a new option to set arbitrary fields on newly discovered nodes, which is useful to e.g. set non-default driver interfaces. Change-Id: I2e23fde9c5aae00ed39bd60c204e7c3eb029586a Story: #2007771 Task: #40091 --- doc/source/user/usage.rst | 13 ++++++++--- ironic_inspector/conf/discovery.py | 2 ++ ironic_inspector/plugins/discovery.py | 2 +- .../test/unit/test_plugins_discovery.py | 23 +++++++++++++++++++ .../enroll-node-fields-3f4e22213fd90307.yaml | 5 ++++ 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/enroll-node-fields-3f4e22213fd90307.yaml diff --git a/doc/source/user/usage.rst b/doc/source/user/usage.rst index ff539f3cd..4728efda0 100644 --- a/doc/source/user/usage.rst +++ b/doc/source/user/usage.rst @@ -274,9 +274,16 @@ Ironic first. For discovery, the configuration file option ``node_not_found_hook`` should be set to load the hook called ``enroll``. This hook will enroll the unidentified -node into Ironic using the ``fake-hardware`` hardware type. (This is -a configurable option; set ``enroll_node_driver``, in the **ironic-inspector** -configuration file, to the Ironic hardware type or classic driver you want.) +node into Ironic using the ``fake-hardware`` hardware type. This is +a configurable option: set ``enroll_node_driver`` in the **ironic-inspector** +configuration file to the hardware type you want. You can also configure +arbitrary fields to set on discovery, for example: + +.. code-block:: ini + + [discovery] + enroll_node_driver = ipmi + enroll_node_fields = management_interface:noop,resource_class:baremetal The ``enroll`` hook will also set the ``ipmi_address`` property on the new node, if its available in the introspection data we received, diff --git a/ironic_inspector/conf/discovery.py b/ironic_inspector/conf/discovery.py index 12cfeead3..8d3b9c4d0 100644 --- a/ironic_inspector/conf/discovery.py +++ b/ironic_inspector/conf/discovery.py @@ -21,6 +21,8 @@ _OPTS = [ default='fake-hardware', help=_('The name of the Ironic driver used by the enroll ' 'hook when creating a new node in Ironic.')), + cfg.DictOpt('enroll_node_fields', default={}, + help=_('Additional fields to set on newly discovered nodes.')), cfg.ListOpt('enabled_bmc_address_version', default=['4', '6'], help=_('IP version of BMC address that will be ' diff --git a/ironic_inspector/plugins/discovery.py b/ironic_inspector/plugins/discovery.py index 13857f379..867465284 100644 --- a/ironic_inspector/plugins/discovery.py +++ b/ironic_inspector/plugins/discovery.py @@ -62,7 +62,7 @@ def _check_existing_nodes(introspection_data, node_driver_info, ironic): def enroll_node_not_found_hook(introspection_data, **kwargs): - node_attr = {} + node_attr = CONF.discovery.enroll_node_fields.copy() ironic = ir_utils.get_client() node_driver_info = _extract_node_driver_info(introspection_data) diff --git a/ironic_inspector/test/unit/test_plugins_discovery.py b/ironic_inspector/test/unit/test_plugins_discovery.py index ab6ce0435..7b260e846 100644 --- a/ironic_inspector/test/unit/test_plugins_discovery.py +++ b/ironic_inspector/test/unit/test_plugins_discovery.py @@ -117,6 +117,29 @@ class TestEnrollNodeNotFoundHook(test_base.NodeTest): {}, {}, self.ironic) self.assertEqual({'auto_discovered': True}, introspection_data) + @mock.patch.object(node_cache, 'create_node', autospec=True) + @mock.patch.object(ir_utils, 'get_client', autospec=True) + @mock.patch.object(discovery, '_check_existing_nodes', autospec=True) + def test_enroll_with_fields(self, mock_check_existing, + mock_client, mock_create_node): + mock_client.return_value = self.ironic + discovery.CONF.set_override('enroll_node_fields', + {'power_interface': 'other'}, + 'discovery') + mock_check_existing = copy_call_args(mock_check_existing) + introspection_data = {} + + discovery.enroll_node_not_found_hook(introspection_data) + + mock_create_node.assert_called_once_with('fake-hardware', + ironic=self.ironic, + driver_info={}, + provision_state='enroll', + power_interface='other') + mock_check_existing.assert_called_once_with( + {}, {}, self.ironic) + self.assertEqual({'auto_discovered': True}, introspection_data) + def test__check_existing_nodes_new_mac(self): self.ironic.ports.return_value = [] introspection_data = {'macs': self.macs} diff --git a/releasenotes/notes/enroll-node-fields-3f4e22213fd90307.yaml b/releasenotes/notes/enroll-node-fields-3f4e22213fd90307.yaml new file mode 100644 index 000000000..a3960be0b --- /dev/null +++ b/releasenotes/notes/enroll-node-fields-3f4e22213fd90307.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Adds a new configuration option ``[discovery]enroll_node_fields`` that + specifies additional fields to set on a node (e.g. driver interfaces).