Separate validating NIC data into a new plugin

This continues effort started in blueprint plugin-architecture
to minimize discoverd core.

Change-Id: Iee423678445ba2d282633bb2b44389acbdeb5adb
This commit is contained in:
Dmitry Tantsur 2014-12-08 15:12:02 +01:00
parent 0047d87f2d
commit c92d516625
6 changed files with 67 additions and 33 deletions

View File

@ -44,8 +44,10 @@
; Comma-separated list of enabled hooks for processing pipeline.
; Hook 'scheduler' updates the node with the minimum properties required by the
; Nova scheduler. Do not exclude it unless you really know what you're doing.
;processing_hooks = scheduler
; Nova scheduler. Hook 'validate_interfaces' ensures that valid NIC data was
; provided by the ramdisk.
; Do not exclude these two unless you really know what you're doing.
;processing_hooks = scheduler,validate_interfaces
; Debug mode enabled/disabled.
;debug = false

View File

@ -25,7 +25,7 @@ DEFAULTS = {
'ironic_retry_attempts': '5',
'ironic_retry_period': '5',
'database': '',
'processing_hooks': 'scheduler',
'processing_hooks': 'scheduler,validate_interfaces',
'timeout': '3600',
'clean_up_period': '60',
'power_off_after_discovery': 'false',

View File

@ -40,34 +40,8 @@ def process(node_info):
bmc_address = node_info.get('ipmi_address')
compat = conf.getboolean('discoverd', 'ports_for_inactive_interfaces')
if 'interfaces' not in node_info and 'macs' in node_info:
LOG.warning('Using "macs" field is deprecated, please '
'update your discovery ramdisk')
node_info['interfaces'] = {'dummy%d' % i: {'mac': m}
for i, m in enumerate(node_info['macs'])}
compat = True
valid_interfaces = {
n: iface for n, iface in node_info['interfaces'].items()
if utils.is_valid_mac(iface['mac']) and (compat or iface.get('ip'))
}
valid_macs = [iface['mac'] for iface in valid_interfaces.values()]
if valid_interfaces != node_info['interfaces']:
LOG.warning(
'The following interfaces were invalid or not eligible in '
'discovery data for node with BMC %(ipmi_address)s and were '
'excluded: %(invalid)s',
{'invalid': {n: iface
for n, iface in node_info['interfaces'].items()
if n not in valid_interfaces},
'ipmi_address': bmc_address})
LOG.info('Eligible interfaces are %s', valid_interfaces)
node_info['interfaces'] = valid_interfaces
node_info['macs'] = valid_macs
cached_node = node_cache.pop_node(bmc_address=bmc_address, mac=valid_macs)
cached_node = node_cache.pop_node(bmc_address=bmc_address,
mac=node_info.get('macs'))
ironic = utils.get_client()
try:
node = ironic.node.get(cached_node.uuid)
@ -94,7 +68,7 @@ def _process_node(ironic, node, node_info):
hooks = plugins_base.processing_hooks_manager()
ports = {}
for mac in node_info['macs']:
for mac in (node_info.get('macs') or ()):
try:
port = ironic.port.create(node_uuid=node.uuid, address=mac)
ports[mac] = port

View File

@ -0,0 +1,56 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Hook to validate network interfaces."""
import logging
from ironic_discoverd import conf
from ironic_discoverd.plugins import base
from ironic_discoverd import utils
LOG = logging.getLogger('ironic_discoverd.plugins.validate_interfaces')
class ValidateInterfacesHook(base.ProcessingHook):
def pre_discover(self, node_info):
bmc_address = node_info.get('ipmi_address')
compat = conf.getboolean('discoverd', 'ports_for_inactive_interfaces')
if 'interfaces' not in node_info and 'macs' in node_info:
LOG.warning('Using "macs" field is deprecated, please '
'update your discovery ramdisk')
node_info['interfaces'] = {
'dummy%d' % i: {'mac': m}
for i, m in enumerate(node_info['macs'])}
compat = True
valid_interfaces = {
n: iface for n, iface in node_info['interfaces'].items()
if utils.is_valid_mac(iface['mac']) and (compat or iface.get('ip'))
}
valid_macs = [iface['mac'] for iface in valid_interfaces.values()]
if valid_interfaces != node_info['interfaces']:
LOG.warning(
'The following interfaces were invalid or not eligible in '
'discovery data for node with BMC %(ipmi_address)s and were '
'excluded: %(invalid)s',
{'invalid': {n: iface
for n, iface in node_info['interfaces'].items()
if n not in valid_interfaces},
'ipmi_address': bmc_address})
LOG.info('Eligible interfaces are %s', valid_interfaces)
node_info['interfaces'] = valid_interfaces
node_info['macs'] = valid_macs

View File

@ -87,7 +87,8 @@ class TestProcess(BaseTest):
def _do_test(self, client_mock, pop_mock, filters_mock, pre_mock,
post_mock):
plugins_base._HOOKS_MGR = None
conf.CONF.set('discoverd', 'processing_hooks', 'scheduler,example')
conf.CONF.set('discoverd', 'processing_hooks',
'scheduler,validate_interfaces,example')
cli = client_mock.return_value

View File

@ -23,6 +23,7 @@ setup(
],
'ironic_discoverd.hooks': [
"scheduler = ironic_discoverd.plugins.scheduler:SchedulerHook",
"validate_interfaces = ironic_discoverd.plugins.validate_interfaces:ValidateInterfacesHook",
"example = ironic_discoverd.plugins.example:ExampleProcessingHook",
],
},