Skip to content

Commit

Permalink
fix: get game server name from game allocation (#1471)
Browse files Browse the repository at this point in the history
  • Loading branch information
razvan-pro committed Feb 26, 2021
1 parent 498a5d5 commit 05d1e95
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions aimmo-game-creator/game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,24 +221,7 @@ def _remove_path_from_ingress(self, game_id):

self.networking_api.patch_namespaced_ingress("aimmo-ingress", "default", patch)

def _create_game_service(self, game_id):
result = self.custom_objects_api.list_namespaced_custom_object(
group="agones.dev",
version="v1",
namespace="default",
plural="gameservers",
label_selector=f"game-id={game_id}",
)
game_servers = result["items"]

if len(game_servers) == 0:
raise Exception(f"No game server found for game ID {game_id}.")
elif len(game_servers) > 1:
raise Exception(f"More than one game server found for game ID {game_id}.")

game_server = game_servers[0]
game_server_name = game_server["metadata"]["name"]

def _create_game_service(self, game_id, game_server_name):
service_manifest = kubernetes.client.V1ServiceSpec(
selector={"agones.dev/gameserver": game_server_name},
ports=[
Expand Down Expand Up @@ -292,7 +275,7 @@ def _delete_game_secret(self, game_id):

def _create_game_server_allocation(
self, game_id: int, worksheet_id: int, retry_count: int = 0
):
) -> str:
result = self.custom_objects_api.create_namespaced_custom_object(
group="allocation.agones.dev",
version="v1",
Expand All @@ -318,10 +301,15 @@ def _create_game_server_allocation(
},
)
if result["status"]["state"] == "UnAllocated" and retry_count < 5:
LOGGER.warning(
f"Failed to create game, retrying... retry_count={retry_count}"
)
time.sleep(5)
self._create_game_server_allocation(
return self._create_game_server_allocation(
game_id, worksheet_id, retry_count=retry_count + 1
)
else:
return result["status"]["gameServerName"]

def _delete_game_server(self, game_id):
result = self.custom_objects_api.list_namespaced_custom_object(
Expand All @@ -344,8 +332,10 @@ def _delete_game_server(self, game_id):

def create_game(self, game_id, game_data):
self._create_game_secret(game_id)
self._create_game_server_allocation(game_id, game_data["worksheet_id"])
self._create_game_service(game_id)
game_server_name = self._create_game_server_allocation(
game_id, game_data["worksheet_id"]
)
self._create_game_service(game_id, game_server_name)
self._add_path_to_ingress(game_id)
LOGGER.info("Game started - {}".format(game_id))

Expand Down

0 comments on commit 05d1e95

Please sign in to comment.