Skip to content

Commit ecbab2e

Browse files
authored
Add log proxy (#41)
* Creating a log proxy to the new log listener in ESR
1 parent da34873 commit ecbab2e

File tree

8 files changed

+104
-3
lines changed

8 files changed

+104
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ __pycache__/*
2121
.pydevproject
2222
.settings
2323
.idea
24+
.python-version
2425
tags
2526

2627
# Package files

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ ARG TZ
99
ENV TZ=$TZ
1010

1111
COPY --from=build /src/dist/*.whl /tmp
12+
1213
# hadolint ignore=DL3013
1314
# hadolint ignore=DL3008
14-
1515
RUN apt-get update && \
1616
apt-get install -y gcc libc-dev tzdata --no-install-recommends && \
1717
pip install --no-cache-dir /tmp/*.whl && \
1818
apt-get purge -y --auto-remove gcc libc-dev && \
19-
rm -rf /var/lib/apt/lists/*
19+
rm -rf /var/lib/apt/lists/*
2020

2121
RUN groupadd -r etos && useradd -r -m -s /bin/false -g etos etos
2222
USER etos

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ uvicorn~=0.22
1818
fastapi~=0.96.0
1919
aiohttp[speedups]~=3.8
2020
gql[requests]~=3.4
21+
httpx~=0.24
22+
kubernetes~=26.1
23+
sse-starlette~=1.6

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ install_requires =
3030
fastapi~=0.96.0
3131
aiohttp[speedups]~=3.8
3232
gql[requests]~=3.4
33+
httpx~=0.24
34+
kubernetes~=26.1
35+
sse-starlette~=1.6
3336

3437
# Require a specific Python version, e.g. Python 2.7 or >= 3.4
3538
python_requires = >=3.4

src/etos_api/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ async def redirect_head_to_root():
5353
APP.include_router(routers.etos.ROUTER)
5454
APP.include_router(routers.selftest.ROUTER)
5555
APP.include_router(routers.environment_provider.ROUTER)
56+
APP.include_router(routers.logs.ROUTER)

src/etos_api/routers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
"""ETOS API routers module."""
17-
from . import environment_provider, etos, selftest
17+
from . import environment_provider, etos, selftest, logs
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright Axis Communications AB.
2+
#
3+
# For a full list of individual contributors, please see the commit history.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
"""ETOS API log handler."""
17+
from .router import ROUTER
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Copyright Axis Communications AB.
2+
#
3+
# For a full list of individual contributors, please see the commit history.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
"""ETOS API log handler."""
17+
import asyncio
18+
import logging
19+
from uuid import UUID
20+
from kubernetes import client, config
21+
from fastapi import APIRouter, HTTPException
22+
23+
from sse_starlette.sse import EventSourceResponse
24+
from starlette.requests import Request
25+
import httpx
26+
27+
LOGGER = logging.getLogger(__name__)
28+
ROUTER = APIRouter()
29+
30+
try:
31+
config.load_incluster_config()
32+
except config.ConfigException:
33+
try:
34+
config.load_config()
35+
except config.ConfigException:
36+
LOGGER.warning("Could not load a Kubernetes config")
37+
38+
39+
@ROUTER.get("/logs/{uuid}", tags=["logs"])
40+
async def get_logs(uuid: UUID, request: Request):
41+
"""Get logs from an ETOS pod and stream them back as server sent events."""
42+
corev1 = client.CoreV1Api()
43+
thread = corev1.list_namespaced_pod("etos-development", async_req=True)
44+
pod_list = thread.get()
45+
46+
ip_addr = None
47+
for pod in pod_list.items:
48+
if pod.status.phase == "Running" and pod.metadata.name.startswith(
49+
f"suite-runner-{str(uuid)}"
50+
):
51+
ip_addr = pod.status.pod_ip
52+
if ip_addr is None:
53+
raise HTTPException(
54+
status_code=404, detail=f"Suite runner with UUID={uuid} not found"
55+
)
56+
57+
async def sse(url):
58+
index = 0
59+
while True:
60+
if await request.is_disconnected():
61+
break
62+
try:
63+
response = httpx.get(url)
64+
lines = response.text.splitlines()
65+
for message in lines[index:]:
66+
yield {"id": index + 1, "event": "message", "data": message}
67+
index += 1
68+
except httpx.RemoteProtocolError:
69+
LOGGER.exception("Failed to connect to pod %r", url)
70+
except IndexError:
71+
pass
72+
await asyncio.sleep(1)
73+
74+
return EventSourceResponse(
75+
sse(f"http://{ip_addr}:8000/log"),
76+
)

0 commit comments

Comments
 (0)