Skip to content

Commit 5d708e4

Browse files
authored
Get namespace from kubernetes or environment variable (#42)
1 parent ecbab2e commit 5d708e4

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/etos_api/routers/logs/router.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
"""ETOS API log handler."""
17+
import os
1718
import asyncio
1819
import logging
1920
from uuid import UUID
@@ -24,6 +25,7 @@
2425
from starlette.requests import Request
2526
import httpx
2627

28+
NAMESPACE_FILE = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
2729
LOGGER = logging.getLogger(__name__)
2830
ROUTER = APIRouter()
2931

@@ -36,11 +38,23 @@
3638
LOGGER.warning("Could not load a Kubernetes config")
3739

3840

41+
def namespace() -> str:
42+
"""Get current namespace if available."""
43+
if not os.path.isfile(NAMESPACE_FILE):
44+
LOGGER.warning(
45+
"Not running in Kubernetes. Cannot figure out namespace. "
46+
"Defaulting to environment variable 'ETOS_NAMESPACE'."
47+
)
48+
return os.getenv("ETOS_NAMESPACE")
49+
with open(NAMESPACE_FILE, encoding="utf-8") as namespace_file:
50+
return namespace_file.read()
51+
52+
3953
@ROUTER.get("/logs/{uuid}", tags=["logs"])
4054
async def get_logs(uuid: UUID, request: Request):
4155
"""Get logs from an ETOS pod and stream them back as server sent events."""
4256
corev1 = client.CoreV1Api()
43-
thread = corev1.list_namespaced_pod("etos-development", async_req=True)
57+
thread = corev1.list_namespaced_pod(namespace(), async_req=True)
4458
pod_list = thread.get()
4559

4660
ip_addr = None

0 commit comments

Comments
 (0)