# agent

包括 agent.py、agent\_api.py、agent\_device\_driver.py agent\_manager.py 等模块，实现 LBaaS 服务的 agent 部分。

## agent.py

按照标准流程，启动了一个 LbaasAgentService 服务，主代码如下。

```python
def main():
    cfg.CONF.register_opts(OPTS)
    cfg.CONF.register_opts(manager.OPTS)
    # import interface options just in case the driver uses namespaces
    cfg.CONF.register_opts(interface.OPTS)
    config.register_interface_driver_opts_helper(cfg.CONF)
    config.register_agent_state_opts_helper(cfg.CONF)
    config.register_root_helper(cfg.CONF)

    common_config.init(sys.argv[1:])
    config.setup_logging()

    mgr = manager.LbaasAgentManager(cfg.CONF)
    svc = LbaasAgentService(
        host=cfg.CONF.host,
        topic=topics.LOADBALANCER_AGENT,
        manager=mgr
    )
    service.launch(svc).wait()
```

其中，管理服务类为 agent\_manager 中的 LbaasAgentManager 类。

LbaasAgentService 类是一个 rpc 服务类，定期执行管理服务类中的周期性任务。

```python
class LbaasAgentService(n_rpc.Service):
    def start(self):
        super(LbaasAgentService, self).start()
        self.tg.add_timer(
            cfg.CONF.periodic_interval,
            self.manager.run_periodic_tasks,
            None,
            None
        )
```

## agent\_manager.py

主要就是定义了 LbaasAgentManager 类，继承自 n\_rpc.RpcCallback 和 periodic\_task.PeriodicTasks。

作为 agent 中的管理类。它实现了 LBaaS 中定义的各种操作 API，包括：

* agent\_updated
* collect\_stats
* create\_member
* create\_pool
* create\_pool\_health\_monitor
* create\_vip
* delete\_member
* delete\_pool
* delete\_pool\_health\_monitor
* delete\_vip
* initialize\_service\_hook
* periodic\_resync
* remove\_orphans
* sync\_state
* update\_member
* update\_pool
* update\_pool\_health\_monitor
* update\_vip

  这些操作又是通过调用更下一层的 driver 来实现。

同时，它定义了一个 rpc api 调用成员，向 topics.LOADBALANCER\_PLUGIN 主题发送消息。

```python
self.plugin_rpc = agent_api.LbaasAgentApi(
            topics.LOADBALANCER_PLUGIN,
            self.context,
            self.conf.host
        )
```

## agent\_api.py

主要定义了 LbaasAgentApi 类，继承自 n\_rpc.RpcProxy，也是一个典型的 RPC 代理类。

这个类在 LbaasAgentManager 类中使用，用来负责替 agent 向指定的主题发送消息，调用监听者的对应方法。主要包括：

* get\_logical\_device
* get\_ready\_devices
* plug\_vip\_port
* pool\_deployed
* pool\_destroyed
* unplug\_vip\_port
* update\_pool\_stats
* update\_status

## agent\_device\_driver.py

主要定义了 AgentDeviceDriver 类，是一个抽象类，抽象出来支持 LBaaS agent 所需要 API 的一个驱动类。这些 API 包括对 pool、member 等资源的 CRUD 等。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yeasy.gitbook.io/openstack_code_neutron/neutron/services/loadbalancer/agent.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
