Skip to content

Commit

Permalink
Add support for flavor reservation
Browse files Browse the repository at this point in the history
Add support in usage enforcement to support flavor reservation.
This differs from host reservation charging, as with flavor reservation
not all of the host will be used, and so we charge proportional to this
usage.
  • Loading branch information
Mark-Powers committed Jan 22, 2025
1 parent 6871300 commit f964fc5
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions balance_service/enforcement/usage_enforcement.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def check_usage_against_allocation(self, data):
self._date_from_string(lease["end_date"])
),
hourly_cost=self._get_reservation_sus(
reservation["resource_type"], reservation["allocations"]
reservation
),
)
new_charge.save()
Expand Down Expand Up @@ -269,7 +269,7 @@ def check_usage_against_allocation_update(self, data):
self._check_alloc_expiration_date(new_lease, alloc, approved_alloc)
for reservation in new_lease["reservations"]:
new_hourly_cost = self._get_reservation_sus(
reservation["resource_type"], reservation["allocations"]
reservation
)
if not end_date_changed:
# check if hourly_cost changed
Expand All @@ -280,7 +280,7 @@ def check_usage_against_allocation_update(self, data):
if len(old_reservation) > 0:
old_reservation = old_reservation[0]
old_hourly_cost = self._get_reservation_sus(
old_reservation["resource_type"], old_reservation["allocations"]
old_reservation
)
if new_hourly_cost == old_hourly_cost:
# nothing changed
Expand Down Expand Up @@ -350,16 +350,29 @@ def __get_billrate(self, resource, resource_type=None):

return DEFAULT_SU_FACTOR

def _get_reservation_sus(self, resource_type, allocations):
return sum(self.__get_billrate(a, resource_type) for a in allocations)
def _get_reservation_sus(self, reservation):
resource_type = reservation["resource_type"]
allocations = reservation["allocations"]
if resource_type == "flavor:instance":
# There is 1 allocation per reservation["amount"]
running_total = 0
for alloc in allocations:
su_factor = self.__get_billrate(alloc, resource_type)
# What propotion of the host is being used by this reservation
host_usage = alloc.get("vcpus", reservation["vcpus"]) / reservation["vcpus"]
running_total += su_factor * host_usage
return running_total
else:
return sum(self.__get_billrate(a, resource_type) for a in allocations)

def _total_su_factor(self, lease_values):
"""Gets the total SUs for the lease.
See https://docs.openstack.org/blazar/latest/admin/usage-enforcement.html#externalservicefilter
"""
total_su_factor = 0

for reservation in lease_values["reservations"]:
resource_type = reservation["resource_type"]
allocations = reservation["allocations"]
total_su_factor += self._get_reservation_sus(resource_type, allocations)
total_su_factor += self._get_reservation_sus(reservation)

return total_su_factor

Expand Down

0 comments on commit f964fc5

Please sign in to comment.