欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Neutron 代码阅读笔记

程序员文章站 2022-07-12 13:16:43
...

Neutron代码架构主要分为三个部分:
1、server :负责RESTfulAPI的路由分发,把请求路由到相应的plugin处理
2、plugin:负责处理具体的资源业务组装,校验,入库,然后通过rpc发给agent具体处理
3、agent:具体的执行任务

neutron server

  • 服务入口:\neutron\cmd\eventlet\server__init__.py:main()
  • API路由分发:/neutron/api/v2/router.py
class APIRouter(base_wsgi.Router):

    @classmethod
    def factory(cls, global_config, **local_config):
        if cfg.CONF.web_ramework == 'pecan':
            return pecan_app.v2_factory(global_config, **local_config)
        return cls(**local_config)
    def __init__(self, **local_config):
        mapper = routes_mapper.Mapper()
        manager.init()

主要是: manager.init()

  • 生成单例模式的NeutronManager:/neutron/manager.py
def init():
    """Call to load the plugins (core+services) machinery."""
    if not directory.is_loaded():
        NeutronManager.get_instance()
  • 加载core plugin和services plugins: /neutron/manager.py
class NeutronManager(object):
    """Neutron's Manager class.
    Neutron's Manager class is responsible for parsing a config file and
    instantiating the correct plugin that concretely implements
    neutron_plugin_base class.
    """
    _instance = None
    __trace_args__ = {"name": "rpc"}
    def __init__(self, options=None, config_file=None):
        # If no options have been provided, create an empty dict
        if not options:
            options = {}
        msg = validate_pre_plugin_load()

        if msg:
            LOG.critical(msg)
            raise Exception(msg)

        plugin_provider = cfg.CONF.core_plugin
        LOG.info("Loading core plugin: %s", plugin_provider)
        plugin = self._get_plugin_instance(CORE_PLUGINS_NAMESPACE,
                                           plugin_provider)
        directory.add_plugin(lib_const.CORE, plugin)
        msg = validate_post_plugin_load()
        if msg:
            LOG.critical(msg)
            raise Exception(msg)
	   # 加载 core plugin --------------------------------------------------------------------
        self._load_services_from_core_plugin(plugin)
        # 加载 services plugin ---------------------------------------------------------------
        self._load_service_plugins()
        # Used by pecan WSGI
        self.resource_plugin_mappings = {}
        self.resource_controller_mappings = {}
        self.path_prefix_resource_mappings = defaultdict(list)
  • 分别加载配置文件中的plugin:
    /etc/kolla/neutron-server/neutron.conf
[DEFAULT]
debug = True
api_paste_config = /usr/share/neutron/api-paste.ini
...
core_plugin = ml2
service_plugins = firewall,qos,router,cnpconn
  • service plugins的加载代码:
    def _load_service_plugins(self):
        """Loads service plugins.
        Starts from the core plugin and checks if it supports
        advanced services then loads classes provided in configuration.
        """
        # 配置文件neutron.conf中定义的service_plugins
        plugin_providers = cfg.CONF.service_plugins
        # 代码中写死的 默认的service_plugins
        plugin_providers.extend(self._get_default_service_plugins())
        LOG.debug("Loading service plugins: %s", plugin_providers)
        for provider in plugin_providers:
            if provider == '':
                continue
           ...
  • 默认的plugin位置:/neutron/plugins/common/constants.py
    constants.py中的默认service plugin和neutron.conf中定义的service plugin一个位置有就可以了。
DEFAULT_SERVICE_PLUGINS = {
    'auto_allocate': 'auto-allocated-topology',
    'tag': 'tag',
    'timestamp': 'timestamp',
    'network_ip_availability': 'network-ip-availability',
    'flavors': 'flavors',
    'revisions': 'revisions',
}

总结:要加载自定义组件有两种方式:
1、将写好的plugins以配置文件方式加入neutron.conf中;
2、在constants.py中DEFAULT_SERVICE_PLUGINS下加入plugin

相关标签: openstack