Skip to content

Commit

Permalink
Inspect node group reservation, use attached placement policy
Browse files Browse the repository at this point in the history
  • Loading branch information
mr0re1 authored and bsngardner committed Oct 4, 2023
1 parent a947724 commit 9017a00
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## \[5.9.1\]

- Use reservation placement policy if placement is enabled, and a reservation is
specified.

## \[5.9.0\]

- Remove spurious log message on resume, referring to "Reservation name".
Expand Down
12 changes: 11 additions & 1 deletion scripts/resume.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,21 @@ def instance_properties(partition, model, placement_group, labels=None):
}

if node_group.reservation_name:
reservation_name = node_group.reservation_name
reservation = lkp.reservation(reservation_name)
props.reservationAffinity = {
"consumeReservationType": "SPECIFIC_RESERVATION",
"key": "compute.googleapis.com/reservation-name",
"values": [node_group.reservation_name],
"values": [reservation_name],
}

placement = util.reservation_placement(reservation)
if placement:
props.resourcePolicies = [placement]
log.info(f"reservation {reservation_name} is being used with placement policy {placement}")
else:
props.resourcePolicies = []
log.info(f"reservation {reservation_name} is being used without a placement policy")

return props

Expand Down
33 changes: 33 additions & 0 deletions scripts/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ def is_exclusive_node(node):
)


def reservation_placement(reservation):
"""
Inspects reservation object, returns placement policy name if one is found.
"""
url = reservation.get("resourcePolicies", {}).get("placement")
if url:
return trim_self_link(url)
return None


def compute_service(credentials=None, user_agent=USER_AGENT, version="v1"):
"""Make thread-safe compute service handle
creates a new Http for each request
Expand Down Expand Up @@ -1629,6 +1639,29 @@ def clear_template_info_cache(self):
cache.clear()
self.template_info.cache_clear()

@lru_cache()
def reservation(self, name):
"""
See https://cloud.google.com/compute/docs/reference/rest/v1/reservations
"""
resp = ensure_execute(
self.compute.reservations().aggregatedList(
project=self.project, filter=f"name={name}"
)
)
reservation = None
for _, e in resp["items"].items():
for r in e.get("reservations", []):
assert (
reservation is None
), f"multiple reservations '{name}' found in '{self.project}'."
reservation = r

assert (
reservation is not None
), f"reservation '{name}' not found in '{self.project}'."
return reservation


# Define late globals
cfg = load_config_file(CONFIG_FILE)
Expand Down

0 comments on commit 9017a00

Please sign in to comment.