#nova/api/openstack/compute/servers.py ServersController
def create(self,req,body)
# nova/compute/manager.py ComputeManger
def build_and_run_instance()
def _do_build_and_run_instace()
def _build_and_run_instance()
def _build_resources():
network_info = self._build_networks_for_instance()
与 neutron 交互
def _build_networks_for_instance(self, context, instance,
requested_networks, security_groups):
if strutils.bool_from_string(
instance.system_metadata.get('network_allocated', 'False')):
self.network_api.setup_instance_network_on_host(
context, instance, instance.host)
return self.network_api.get_instance_nw_info(context, instance)
if not self.is_neutron_security_groups:
security_groups = []
macs = self.driver.macs_for_instance(instance)
network_info = self._allocate_network(context, instance,
requested_networks, macs, security_groups)
return network_info
分两种情况:是否之前分配过
第一种
def _update_port_binding_for_instance
第二种
# 异步网络资源分配
def _allocate_network_async()
# 向neutron发送请求
def allocate_for_instance():
neutron = get_client(context)
requests_and_created_ports = self._create_ports_for_instance(
context, instance, ordered_networks, nets, neutron,
security_group_ids)
available_macs = _filter_hypervisor_macs(
instance, requested_ports_dict, macs)
admin_client = get_client(context, admin=True)
ordered_nets, ordered_port_ids, preexisting_port_ids, \
created_port_ids = self._update_ports_for_instance(
context, instance,
neutron, admin_client, requests_and_created_ports, nets,
bind_host_id, available_macs, requested_ports_dict)
#如果 NetworkRequest 已经包含 port_id,则元组对的 created_port_id 为 None,这意味着用户请求了特定的预存在的端口,因此此处不会创建端口。这些端口稍后会在 _update_ports_for_instance 方法中进行更新,以绑定到实例和计算主机。
def _create_ports_for_instance()
def _update_ports_for_instance()
def _update_port()
# neutronclient\v2_0\client.py
def update_port(self, port, body=None, revision_number=None):
"""Updates a port."""
return self._update_resource(self.port_path % (port), body=body,
revision_number=revision_number)
def _update_resource(self, path, **kwargs):
return self.put(path, **kwargs)
def put(self, action, body=None, headers=None, params=None):
return self.retry_request("PUT", action, body=body,
headers=headers, params=params)
def retry_request()
def do_request()
# SessionClient
def request()