Skip to content

Commit

Permalink
feat: add requested resources info to servers endpoint
Browse files Browse the repository at this point in the history
fix #223
  • Loading branch information
lorenzo-cavazzi committed Mar 12, 2020
1 parent 4e916e1 commit dacb8a5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
18 changes: 18 additions & 0 deletions renku_notebooks/util/kubernetes_.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,23 @@ def get_pod_status(pod):
status.update(conditions_summary)
return status

def get_pod_resources(pod):
try:
resources = pod.spec.containers[0].resources.requests
# translate the cpu weird numeric string to a normal number
# ref: https://kubernetes.io/docs/concepts/configuration/
# manage-compute-resources-container/#how-pods-with-resource-limits-are-run
if (
"cpu" in resources
and isinstance(resources["cpu"], str)
and str.endswith(resources["cpu"], "m")
and resources["cpu"][:-1].isdigit()
):
resources["cpu"] = str(int(resources["cpu"][:-1]) / 1000)
except (AttributeError, IndexError):
resources = {}
return resources

def get_server_url(pod):
url = "/jupyterhub/user/{username}/{servername}/".format(
username=pod.metadata.annotations["hub.jupyter.org/username"],
Expand All @@ -176,6 +193,7 @@ def get_server_url(pod):
"started": isoformat(pod.status.start_time),
"status": get_pod_status(pod),
"url": get_server_url(pod),
"resources": get_pod_resources(pod),
}
for pod in pods
}
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,15 @@ def kubernetes_client(mocker):
},
],
},
"spec": {
"containers": [
{
"resources": {
"requests": {"cpu": "500m", "memory": "2147483648"}
}
}
]
},
}
]
}
Expand Down
12 changes: 12 additions & 0 deletions tests/rules.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
backend = "backend__2F"
passHostHeader = true

[frontends.frontend__2Fuser_2Fdummyuser_2Fdummyproject-d2e2d040_2F]
backend = "backend__2Fuser_2Fdummyuser_2Fdummyproject-d2e2d040_2F"
passHostHeader = true

[frontends.frontend__2F.routes.test]
rule = "PathPrefix:/"
data = "{\"hub\": true}"

[frontends.frontend__2Fuser_2Fdummyuser_2Fdummyproject-d2e2d040_2F.routes.test]
rule = "PathPrefix:/user/dummyuser/dummyproject-d2e2d040/"
data = "{\"user\": \"dummyuser\", \"server_name\": \"dummyproject-d2e2d040\"}"

[backends.backend__2F.servers.server1]
url = "http://127.0.0.1:8081"
weight = 1

[backends.backend__2Fuser_2Fdummyuser_2Fdummyproject-d2e2d040_2F.servers.server1]
url = "http://127.0.0.1:46139"
weight = 1
1 change: 1 addition & 0 deletions tests/test_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_can_get_created_notebooks(client, kubernetes_client):

response = client.get("/service/servers", headers=AUTHORIZED_HEADERS)
assert response.status_code == 200
print(response.json.get("servers"))
assert SERVER_NAME in response.json.get("servers")


Expand Down

0 comments on commit dacb8a5

Please sign in to comment.