Debian Codegraph @

blazarnova/scheduler/filters/blazar_filter.py

raw
1 # Copyright (c) 2013 Bull.
2 #
3 # All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16
17 from blazarnova.i18n import _
18
19 from nova.scheduler import filters
20 from oslo_config import cfg
21 from oslo_log import log as logging
22 from oslo_utils.strutils import bool_from_string
23
24 LOG = logging.getLogger(__name__)
25
26 FLAVOR_EXTRA_SPEC = "aggregate_instance_extra_specs:reservation"
27 FLAVOR_PREEMPTIBLE = "blazar:preemptible"
28
29 opts = [
30 cfg.StrOpt('aggregate_freepool_name',
31 default='freepool',
32 help='Name of the special aggregate where all hosts '
33 'are candidate for physical host reservation'),
34 cfg.BoolOpt('allow_preemptibles',
35 default=False,
36 help='Whether to allow preemptible instances to be scheduled '
37 'on hosts in the preemptible aggregate'),
38 cfg.StrOpt('preemptible_aggregate',
39 default='freepool',
40 help='Name of the aggregate where hosts can run preemptible '
41 'instances'),
42 cfg.StrOpt('project_id_key',
43 default='blazar:tenant',
44 help='Aggregate metadata value for key matching project_id'),
45 cfg.StrOpt('blazar_owner',
46 default='blazar:owner',
47 help='Aggregate metadata key for knowing owner project_id'),
48 cfg.StrOpt('blazar_az_prefix',
49 default='blazar_',
50 help='Prefix for Availability Zones created by Blazar')
51 ]
52
53 cfg.CONF.register_opts(opts, 'blazar:physical:host')
54
55
56 class BlazarFilter(filters.BaseHostFilter):
57 """Blazar Filter for nova-scheduler."""
58
59 run_filter_once_per_request = True
60
61 def fetch_blazar_pools(self, host_state):
62 # Get any reservation pools this host is part of
63 # Note this include possibly the freepool
64 aggregates = host_state.aggregates
65 pools = []
66 for agg in aggregates:
67 if (agg.availability_zone and
68 str(agg.availability_zone).startswith(
69 cfg.CONF['blazar:physical:host'].blazar_az_prefix)
70 # NOTE(hiro-kobayashi): following 2 lines are for keeping
71 # backward compatibility
72 or str(agg.availability_zone).startswith('blazar:')):
73 pools.append(agg)
74
75 if agg.name in [
76 cfg.CONF['blazar:physical:host'].aggregate_freepool_name,
77 cfg.CONF['blazar:physical:host'].preemptible_aggregate]:
78 pools.append(agg)
79
80 return pools
81
82 def host_reservation_request(self, host_state, spec_obj, requested_pools):
83
84 pools = self.fetch_blazar_pools(host_state)
85
86 for pool in [p for p in pools if p.name in requested_pools]:
87 # Check tenant is allowed to use this Pool
88
89 # NOTE(sbauza): Currently, the key is only the project_id,
90 # but later will possibly be blazar:tenant:{project_id}
91 key = spec_obj.project_id
92 access = pool.metadata.get(key)
93 if access:
94 return True
95 # NOTE(sbauza): We also need to check the blazar:owner key
96 # until we modify the reservation pool for including the
97 # project_id key as for any other extra project
98 owner = cfg.CONF['blazar:physical:host'].blazar_owner
99 owner_project_id = pool.metadata.get(owner)
100 if owner_project_id == spec_obj.project_id:
101 return True
102 LOG.info(_("Unauthorized request to use Pool "
103 "%(pool_id)s by tenant %(tenant_id)s"),
104 {'pool_id': pool.name,
105 'tenant_id': spec_obj.project_id})
106 return False
107 return False
108
109 def host_passes(self, host_state, spec_obj):
110 """Check if a host in a pool can be used for a request
111
112 A host is in a pool if it is a member of an aggregate that has
113 a metadata item with a key value of "blazar:owner"
114
115 If the user does not pass "reservation=<id>" as a hint then only
116 hosts which are not in a pool (including freepool) pass.
117
118 If the user does pass "reservation=<id>" as a hint then the host only
119 passes if it is a member of the specified pool and that pool
120 has a metadata key of either :
121 - "blazar:owner=tenant_id (which grants automatically all the
122 users from the tenant from which the request came from)
123 - or, "tenant_id=blazar:tenant" (which grants extra tenants for
124 the reservation)
125 """
126
127 # Find which Pools the user wants to use (if any)
128 requested_pools = spec_obj.get_scheduler_hint('reservation')
129 if isinstance(requested_pools, str):
130 requested_pools = [requested_pools]
131
132 # the request is host reservation
133 if requested_pools:
134 return self.host_reservation_request(host_state, spec_obj,
135 requested_pools)
136
137 extra_specs = spec_obj.flavor.extra_specs
138 # the request is instance reservation
139 if FLAVOR_EXTRA_SPEC in extra_specs.keys():
140 # Scheduling requests for instance reservation are processed by
141 # other Nova filters: AggregateInstanceExtraSpecsFilter,
142 # AggregateMultiTenancyIsolation, and
143 # ServerGroupAntiAffinityFilter. What BlazarFilter needs to
144 # do is just pass the host if the request has an instance
145 # reservation key.
146 return True
147
148 allow_preempt = cfg.CONF['blazar:physical:host'].allow_preemptibles
149 preemptible = bool_from_string(
150 extra_specs.get(FLAVOR_PREEMPTIBLE, False))
151 blazar_pools = self.fetch_blazar_pools(host_state)
152 # If the request is for a preemptible instance and they are allowed
153 if allow_preempt and preemptible:
154 if (len(blazar_pools) == 1 and blazar_pools[0].name ==
155 cfg.CONF['blazar:physical:host'].preemptible_aggregate):
156 # Pass host if it only belongs to the preemptibles aggregate
157 LOG.info("Host %s allowed for preemptibles" % host_state)
158 return True
159 return False
160
161 if blazar_pools:
162 # Host is in a blazar pool and non reservation request
163 LOG.info("Host is in a reservation aggregate or in the freepool")
164 return False
165
166 return True