-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Creating a log proxy to the new log listener in ESR
- Loading branch information
Showing
8 changed files
with
104 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ __pycache__/* | |
.pydevproject | ||
.settings | ||
.idea | ||
.python-version | ||
tags | ||
|
||
# Package files | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright Axis Communications AB. | ||
# | ||
# For a full list of individual contributors, please see the commit history. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""ETOS API log handler.""" | ||
from .router import ROUTER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Copyright Axis Communications AB. | ||
# | ||
# For a full list of individual contributors, please see the commit history. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""ETOS API log handler.""" | ||
import asyncio | ||
import logging | ||
from uuid import UUID | ||
from kubernetes import client, config | ||
from fastapi import APIRouter, HTTPException | ||
|
||
from sse_starlette.sse import EventSourceResponse | ||
from starlette.requests import Request | ||
import httpx | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
ROUTER = APIRouter() | ||
|
||
try: | ||
config.load_incluster_config() | ||
except config.ConfigException: | ||
try: | ||
config.load_config() | ||
except config.ConfigException: | ||
LOGGER.warning("Could not load a Kubernetes config") | ||
|
||
|
||
@ROUTER.get("/logs/{uuid}", tags=["logs"]) | ||
async def get_logs(uuid: UUID, request: Request): | ||
"""Get logs from an ETOS pod and stream them back as server sent events.""" | ||
corev1 = client.CoreV1Api() | ||
thread = corev1.list_namespaced_pod("etos-development", async_req=True) | ||
pod_list = thread.get() | ||
|
||
ip_addr = None | ||
for pod in pod_list.items: | ||
if pod.status.phase == "Running" and pod.metadata.name.startswith( | ||
f"suite-runner-{str(uuid)}" | ||
): | ||
ip_addr = pod.status.pod_ip | ||
if ip_addr is None: | ||
raise HTTPException( | ||
status_code=404, detail=f"Suite runner with UUID={uuid} not found" | ||
) | ||
|
||
async def sse(url): | ||
index = 0 | ||
while True: | ||
if await request.is_disconnected(): | ||
break | ||
try: | ||
response = httpx.get(url) | ||
lines = response.text.splitlines() | ||
for message in lines[index:]: | ||
yield {"id": index + 1, "event": "message", "data": message} | ||
index += 1 | ||
except httpx.RemoteProtocolError: | ||
LOGGER.exception("Failed to connect to pod %r", url) | ||
except IndexError: | ||
pass | ||
await asyncio.sleep(1) | ||
|
||
return EventSourceResponse( | ||
sse(f"http://{ip_addr}:8000/log"), | ||
) |