Skip to content

Commit

Permalink
fix: Update k8s api (#1715)
Browse files Browse the repository at this point in the history
* fix: Update deprecated K8s APIs

* fix: Update deprecated K8s APIs

* Unfix pipenv version?

* Try really old pipenv version

* Try downgrading pipenv in dockerfiles

* Try upgrading it instead

* Try removing --deploy just to test

* Bring back deploy

* Update lockfiles

* Downgrade pipenv again

* Add path_type arg

* Remove line
  • Loading branch information
faucomte97 committed Oct 20, 2022
1 parent 45e0fcf commit 3ec86ac
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 359 deletions.
2 changes: 1 addition & 1 deletion aimmo-game-creator/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ verify_ssl = true
name = "pypi"

[packages]
kubernetes = "==21.7.0"
kubernetes = "==24.2.0"
docker = "<6"
aimmo-game-creator = { editable = true, path = "." }
google-cloud-logging = "*"
Expand Down
314 changes: 163 additions & 151 deletions aimmo-game-creator/Pipfile.lock

Large diffs are not rendered by default.

72 changes: 17 additions & 55 deletions aimmo-game-creator/game_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ def update(self):
LOGGER.error("Failed to obtain game data")
LOGGER.exception(ex)
else:
games_to_add = {
id: running_games[id] for id in self._data.add_new_games(running_games)
}
games_to_add = {id: running_games[id] for id in self._data.add_new_games(running_games)}

# Add missing games
self._parallel_map(self.recreate_game, games_to_add.items())
Expand Down Expand Up @@ -158,7 +156,7 @@ class KubernetesGameManager(GameManager):
"""Manages games running on Kubernetes cluster"""

def __init__(self, *args, **kwargs):
self.networking_api = kubernetes.client.NetworkingV1beta1Api()
self.networking_api = kubernetes.client.NetworkingV1Api()
self.api: CoreV1Api = kubernetes.client.CoreV1Api()
self.secret_creator = TokenSecretCreator()
self.api_client: ApiClient = ApiClient()
Expand All @@ -183,27 +181,21 @@ def _create_game_name(game_id):

def _add_path_to_ingress(self, game_id):
game_name = KubernetesGameManager._create_game_name(game_id)
backend = kubernetes.client.NetworkingV1beta1IngressBackend(game_name, 80)
path = kubernetes.client.NetworkingV1beta1HTTPIngressPath(
backend, f"/{game_name}(/|$)(.*)"
)
backend = kubernetes.client.V1IngressBackend(game_name, 80)
path = kubernetes.client.V1HTTPIngressPath(backend, f"/{game_name}(/|$)(.*)", path_type="exact")

patch = [{"op": "add", "path": "/spec/rules/0/http/paths/-", "value": path}]

# This exception is usually triggered locally where there is no ingress.
try:
self.networking_api.patch_namespaced_ingress(
"aimmo-ingress", "default", patch
)
self.networking_api.patch_namespaced_ingress("aimmo-ingress", "default", patch)
except ApiException as e:
LOGGER.exception(e)

def _remove_path_from_ingress(self, game_id):
game_name = KubernetesGameManager._create_game_name(game_id)
backend = kubernetes.client.NetworkingV1beta1IngressBackend(game_name, 80)
path = kubernetes.client.NetworkingV1beta1HTTPIngressPath(
backend, f"/{game_name}(/|$)(.*)"
)
backend = kubernetes.client.V1IngressBackend(game_name, 80)
path = kubernetes.client.V1HTTPIngressPath(backend, f"/{game_name}(/|$)(.*)", path_type="exact")
try:
ingress = self.networking_api.list_namespaced_ingress("default").items[0]
# These exceptions are usually triggered locally where there is no ingress.
Expand All @@ -219,33 +211,21 @@ def _remove_path_from_ingress(self, game_id):
except ValueError:
return

patch = [
{
"op": "remove",
"path": "/spec/rules/0/http/paths/{}".format(index_to_delete),
}
]
patch = [{"op": "remove", "path": "/spec/rules/0/http/paths/{}".format(index_to_delete)}]

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

def _create_game_service(self, game_id, game_server_name):
service_manifest = kubernetes.client.V1ServiceSpec(
selector={"agones.dev/gameserver": game_server_name},
ports=[
kubernetes.client.V1ServicePort(
name="tcp", protocol="TCP", port=80, target_port=5000
)
],
ports=[kubernetes.client.V1ServicePort(name="tcp", protocol="TCP", port=80, target_port=5000)],
)

service_metadata = kubernetes.client.V1ObjectMeta(
name=KubernetesGameManager._create_game_name(game_id),
labels={"app": "aimmo-game", "game_id": game_id},
name=KubernetesGameManager._create_game_name(game_id), labels={"app": "aimmo-game", "game_id": game_id}
)

service = kubernetes.client.V1Service(
metadata=service_metadata, spec=service_manifest
)
service = kubernetes.client.V1Service(metadata=service_metadata, spec=service_manifest)
self.api.create_namespaced_service(K8S_NAMESPACE, service)

def _delete_game_service(self, game_id):
Expand All @@ -260,9 +240,7 @@ def _delete_game_service(self, game_id):
LOGGER.info("Removing service: {}".format(resource.metadata.name))
self.api.delete_namespaced_service(resource.metadata.name, K8S_NAMESPACE)

def _create_game_server_allocation(
self, game_id: int, game_data: dict, retry_count: int = 0
) -> str:
def _create_game_server_allocation(self, game_id: int, game_data: dict, retry_count: int = 0) -> str:
result = self.custom_objects_api.create_namespaced_custom_object(
group="allocation.agones.dev",
version="v1",
Expand All @@ -275,23 +253,14 @@ def _create_game_server_allocation(
"spec": {
"required": {"matchLabels": {"agones.dev/fleet": "aimmo-game"}},
"scheduling": "Packed",
"metadata": {
"labels": {
"game-id": game_id,
},
"annotations": game_data,
},
"metadata": {"labels": {"game-id": game_id}, "annotations": game_data},
},
},
)
if result["status"]["state"] == "UnAllocated" and retry_count < 60:
LOGGER.warning(
f"Failed to create game, retrying... retry_count={retry_count}"
)
LOGGER.warning(f"Failed to create game, retrying... retry_count={retry_count}")
time.sleep(5)
return self._create_game_server_allocation(
game_id, game_data, retry_count=retry_count + 1
)
return self._create_game_server_allocation(game_id, game_data, retry_count=retry_count + 1)
else:
return result["status"]["gameServerName"]

Expand All @@ -307,11 +276,7 @@ def _delete_game_server(self, game_id):
for game_server in game_servers_to_delete:
name = game_server["metadata"]["name"]
self.custom_objects_api.delete_namespaced_custom_object(
group="agones.dev",
version="v1",
namespace="default",
plural="gameservers",
name=name,
group="agones.dev", version="v1", namespace="default", plural="gameservers", name=name
)

def create_game(self, game_id, game_data):
Expand All @@ -327,10 +292,7 @@ def delete_game(self, game_id):

def delete_unknown_games(self):
gameservers = self.custom_objects_api.list_namespaced_custom_object(
group="agones.dev",
version="v1",
namespace="default",
plural="gameservers",
group="agones.dev", version="v1", namespace="default", plural="gameservers"
)
# running games are gameservers that have a game-id label
running_game_ids = set(
Expand Down
2 changes: 1 addition & 1 deletion aimmo-game-creator/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
name="aimmo-game-creator",
packages=find_packages(),
include_package_data=True,
install_requires=["eventlet", "kubernetes >= 6.0.0"],
install_requires=["eventlet"],
tests_require=["httmock"],
test_suite="tests",
zip_safe=False,
Expand Down
2 changes: 1 addition & 1 deletion aimmo-game-worker/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ verify_ssl = true
name = "pypi"

[packages]
kubernetes = "==21.7.0"
kubernetes = "==24.2.0"
docker = "<6"
aimmo-game-worker = {editable = true, path = "."}
restrictedpython = "==4.0.b7"
Expand Down
28 changes: 14 additions & 14 deletions aimmo-game-worker/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion aimmo-game/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ black = "*"
[packages]
aimmo-game = {editable = true,path = "."}
oauthlib = "==3.2.0"
kubernetes = "==21.7.0"
kubernetes = "==24.2.0"
docker = "<6"

[requires]
Expand Down
Loading

0 comments on commit 3ec86ac

Please sign in to comment.