前一段时间看了openstack filter类的代码在这里做一个简单的记录:
filter类示例,如果一个compute结点满足条件就返回True,否则返回False:
class MyFilter(filters.BaseHostFilter):
def host_passes(self, host_state, filter_properties):
free_ram = host_state.free_ram_mb
requested_ram = filter_properties.get('instance_type')['memory_mb']
if(free_ram > requested_ram ):
return True
else:
return False
添加自定义的Filter主要有一下两步:
Put “my_filter.py” to the /usr/share/pyshared/nova/scheduler/filters/folder, make a soft link of it in the /usr/lib/python2.7/dist-packages/nova/scheduler/filters / folder. The class name in the “my_filter.py” file is “MyFilter”.
In the nova.conf file configuration as follows:
scheduler_available_filters = nova.scheduler.filters.all_filters
scheduler_default_filters = RetryFilter, …… , MyFilter
host_state主要参数,host_state其实是openstack中的一个类:
self.host = host
self.nodename = node
self.update_capabilities(capabilities, service)
# Mutable available resources.
# These will change as resources are virtually "consumed".
self.total_usable_ram_mb = 0
self.total_usable_disk_gb = 0
self.disk_mb_used = 0
self.free_ram_mb = 0
self.free_disk_mb = 0
self.vcpus_total = 0
self.vcpus_used = 0
# Additional host information from the compute node stats:
self.vm_states = {}
self.task_states = {}
self.num_instances = 0
self.num_instances_by_project = {}
self.num_instances_by_os_type = {}
self.num_io_ops = 0
# Other information
self.host_ip = None
self.hypervisor_type = None
self.hypervisor_version = None
self.hypervisor_hostname = None
self.cpu_info = None
self.supported_instances = None
# Resource oversubscription values for the compute host:
self.limits = {}
# Generic metrics from compute nodes
self.metrics = {}
self.updated = None
filter_properties数据:
key: instance_type, value: {u'root_gb': 1,
u'name': u'm1.tiny',
u'ephemeral_gb': 0,
u'memory_mb': 512,
u'vcpus': 1,
u'extra_specs': {},
u'swap': 0,
u'rxtx_factor': 1.0,
u'flavorid': u'1',
u'vcpu_weight': None,
u'id': 2}
key: config_options, value: {}
key: retry, value: {'num_attempts': 1,
'hosts': []}
key: context, value:
key: request_spec, value:
{u'num_instances': 1,
u'block_device_mapping':
[{u'instance_uuid': u'b45bd6d8-aee1-47a7-a6e6-3a9596629418',
u'guest_format': None, u'boot_index': 0,
u'delete_on_termination': True,
u'no_device': None,
u'connection_info': None,
u'snapshot_id': None,
u'device_name': None,
u'disk_bus': None,
u'image_id':
u'114b7f75-0888-4ad3-b4fc-4f759068cf3d',
u'source_type': u'image',
u'device_type': u'disk',
u'volume_id': None,
u'destination_type': u'local',
u'volume_size': None}],
u'image': {u'status': u'active',
u'name': u'cirros-0.3.2-x86_64',
u'deleted': False,
u'container_format': u'bare',
u'created_at': u'2014-07-23T10:53:32.000000',
u'disk_format': u'qcow2',
u'updated_at': u'2014-07-23T10:53:33.000000',
u'id': u'114b7f75-0888-4ad3-b4fc-4f759068cf3d',
u'owner': u'b6714662cfe844dda85594b3c2ed0176',
u'min_ram': 0,
u'checksum': u'64d7c1cd2b6f60c92c14662941cb7913',
u'min_disk': 0,
u'is_public': True,
u'deleted_at': None,
u'properties': {},
u'size': 13167616},
u'instance_type': {u'root_gb': 1,
u'name': u'm1.tiny',
u'ephemeral_gb': 0,
u'memory_mb': 512,
u'vcpus': 1,
u'extra_specs': {},
u'swap': 0,
u'rxtx_factor': 1.0,
u'flavorid': u'1',
u'vcpu_weight': None,
u'id': 2},
u'instance_properties':
{u'vm_state': u'building',
u'availability_zone': None,
u'terminated_at': None,
u'ephemeral_gb': 0,
u'instance_type_id': 2,
u'user_data': None,
u'cleaned': False,
u'vm_mode': None,
u'deleted_at': None,
u'reservation_id': u'r-b2tta803',
u'id': 79,
u'security_groups':
[{u'project_id': u'b6714662cfe844dda85594b3c2ed0176',
u'user_id': u'b8f5586fd05a4cfd8d78ba69b0b0eb8a',
u'description': u'default',
u'deleted': False,
u'created_at': u'2014-07-23T09:57:31.000000',
u'updated_at': None,
u'deleted_at': None,
u'id': 1,
u'name': u'default'}],
u'disable_terminate': False,
u'display_name': u'test1',
u'uuid': u'b45bd6d8-aee1-47a7-a6e6-3a9596629418',
u'default_swap_device': None,
u'info_cache': {u'instance_uuid': u'b45bd6d8-aee1-47a7-a6e6-3a9596629418',
u'deleted': False,
u'created_at': u'2014-08-19T01:53:46.000000',
u'updated_at': None,
u'network_info': [],
u'deleted_at': None},
u'hostname': u'test1',
u'launched_on': None,
u'display_description': u'test1',
u'key_data': u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC9HYBP22bdhlOy1w6xnlBtS27pQ8f5cuBKbp8wYoBSNkcm7Ely4hCpCWIhN0EAE3DqYQDFXAzJmY8azBYSaBqw8c8lak1jpfDk6k04Hu8wGkj3VWesRbx6ncbtn82SDRl5ZVXdNd8b8lQMU+nWdT4QHQX7hEM/CBjm1P7lcNZYWnHlxNpXorh8qcRp8stjpRsBvOKC4IqpJKpDNCeKZMZXFOpe3kGbOidvnxEoOKJtqWxxZB3dsKLV/i4+JyBExH4P8IS3h0dPXCesDwYxzZzszZP5Nzyx7YDNzyqLvtOdST/nwyBvM7POww1yoIa4pqiToDuUFwpchYypLVxNnry9 Generated by Nova\n', u'kernel_id': u'', u'power_state': 0, u'default_ephemeral_device': None, u'progress': 0, u'project_id': u'b6714662cfe844dda85594b3c2ed0176',
u'launched_at': None,
u'scheduled_at': None,
u'node': None,
u'ramdisk_id': u'',
u'access_ip_v6': None,
u'access_ip_v4': None,
u'deleted': False,
u'key_name': u'kp1',
u'updated_at': None,
u'host': None,
u'ephemeral_key_uuid': None,
u'architecture': None,
u'user_id': u'b8f5586fd05a4cfd8d78ba69b0b0eb8a',
u'system_metadata':
{u'image_min_disk': u'1',
u'instance_type_memory_mb': u'512',
u'instance_type_swap': u'0',
u'instance_type_vcpu_weight': None,
u'instance_type_root_gb': u'1',
u'instance_type_id': u'2',
u'instance_type_name': u'm1.tiny',
u'instance_type_ephemeral_gb': u'0',
u'instance_type_rxtx_factor': u'1.0',
u'instance_type_flavorid': u'1',
u'image_container_format': u'bare',
u'instance_type_vcpus': u'1',
u'image_min_ram': u'0',
u'image_disk_format': u'qcow2',
u'image_base_image_ref': u'114b7f75-0888-4ad3-b4fc-4f759068cf3d'},
u'task_state': u'scheduling',
u'shutdown_terminate': False,
u'cell_name': None,
u'root_gb': 1,
u'locked': False,
u'name': u'instance-0000004f',
u'created_at': u'2014-08-19T01:53:46.000000',
u'locked_by': None,
u'launch_index': 0,
u'metadata': {},
u'memory_mb': 512,
u'vcpus': 1,
u'image_ref': u'114b7f75-0888-4ad3-b4fc-4f759068cf3d',
u'root_device_name': None,
u'auto_disk_config': False,
u'os_type': None,
u'config_drive': u''},
u'security_group': [u'default'],
u'instance_uuids': [u'b45bd6d8-aee1-47a7-a6e6-3a9596629418']}
key: os_type, value: None
key: project_id, value: b6714662cfe844dda85594b3c2ed0176
key: scheduler_hints, value: {}