Skip to content

Commit

Permalink
feat: service name are now dynamic and no longer hardcoded
Browse files Browse the repository at this point in the history
  • Loading branch information
antidodo committed Jul 5, 2024
1 parent 4d21fe2 commit fa2c99a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
Empty file added src/k8s/__init__.py
Empty file.
24 changes: 18 additions & 6 deletions src/k8s/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional
import os
import time
from src.utils.other import get_project_data_source
from src.utils.other import get_project_data_source, get_element_by_substring
from kubernetes import client, config


Expand Down Expand Up @@ -145,7 +145,9 @@ def _create_nginx_config_map(analysis_name: str,
core_client = client.CoreV1Api()

# extract data sources
data_sources = get_project_data_source(analysis_env['KEYCLOAK_TOKEN'], analysis_env['PROJECT_ID'])
service_names = get_service_names(namespace)
hub_adapter_service_name = get_element_by_substring(service_names,'hub-adapter-service')
data_sources = get_project_data_source(analysis_env['KEYCLOAK_TOKEN'], analysis_env['PROJECT_ID'],hub_adapter_service_name,namespace)

# get the service ip of the message broker and analysis service
message_broker_service_ip = core_client.read_namespaced_service(name="flame-node-node-message-broker", namespace=namespace).spec.cluster_ip
Expand All @@ -160,7 +162,13 @@ def _create_nginx_config_map(analysis_name: str,

# analysis_ip = core_client.read_namespaced_pod(name=analysis_name, namespace=namespace).spec.cluster_ip
# analysis_service_ip = core_client.read_namespaced_service(name=analysis_service_name, namespace=namespace).spec.cluster_ip
kong_proxy_name = get_element_by_substring(service_names, 'kong-proxy')

result_service_name = get_element_by_substring(service_names, 'result-service')

hub_adapter_service_name = get_element_by_substring(service_names, 'hub-adapter-service')

message_broker_service_name = get_element_by_substring(service_names, 'message-broker')
data = {
"nginx.conf": f"""
worker_processes 1;
Expand All @@ -186,26 +194,26 @@ def _create_nginx_config_map(analysis_name: str,
}}
# analysis deployment to kong
location /kong {{
proxy_pass http://flame-node-kong-proxy;
proxy_pass http://{kong_proxy_name};
allow {analysis_ip};
deny all;
}}
location /storage {{
proxy_pass http://flame-node-node-result-service:8080;
proxy_pass http://{result_service_name}:8080;
allow {analysis_ip};
deny all;
}}
location /hub-adapter {{
proxy_pass http://flame-node-hub-adapter-service:5000;
proxy_pass http://{hub_adapter_service_name}:5000;
allow {analysis_ip};
deny all;
}}
# analysis deplyoment to message broker
location /message-broker {{
proxy_pass http://flame-node-node-message-broker;
proxy_pass http://{message_broker_service_name};
allow {analysis_ip};
deny all;
}}
Expand Down Expand Up @@ -324,6 +332,10 @@ def get_logs(name: str, pod_ids: Optional[list[str]], namespace: str = 'default'
return [core_client.read_namespaced_pod_log(pod.metadata.name, namespace)
for pod in pods.items]

def get_service_names(namespace: str = 'default') -> list[str]:
core_client = client.CoreV1Api()
return [service.metadata.name for service in core_client.list_namespaced_service(namespace=namespace).items]


def _get_pods(name: str, namespace: str = 'default') -> list[str]:
core_client = client.CoreV1Api()
Expand Down
17 changes: 14 additions & 3 deletions src/utils/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from httpx import AsyncClient
import asyncio



def create_image_address(analysis_id: str) -> str:
return f"{_add_slash(os.getenv('HARBOR_URL'))}"\
f"{_add_slash(os.getenv('NODE_NAME'))}"\
Expand All @@ -12,21 +14,30 @@ def _add_slash(string: str) -> str:
return string + ('' if string.endswith('/') else '/')


def get_project_data_source(keycloak_token, project_id) -> dict:
def get_project_data_source(keycloak_token, project_id,hub_adapter_service_name ,namespace: str = 'default') -> dict:
"""
Get data sources for a project from the node hub adapter service using the keycloak token
:param keycloak_token:
:param project_id:
:return:
"""
client = AsyncClient(base_url="http://flame-node-hub-adapter-service:5000",
client = AsyncClient(base_url=f"http://{hub_adapter_service_name}:5000",
headers={"Authorization": f"Bearer {keycloak_token}",
"accept": "application/json"})
return asyncio.run(call_sources(client, project_id))

def get_element_by_substring(data: list[str], substring: str) -> str:
"""
Get the smallest element in a list that contains a substring
:param data:
:param substring:
:return:
"""
matching_elements = [element for element in data if substring in element]
return min(matching_elements, key=len) if matching_elements else None

async def call_sources(client, project_id) -> list[dict[str, str]]:
response = await client.get(f"/kong/datastore/{project_id}")
response = await client.get(f"/kong/datastore?project_id={project_id}")
response.raise_for_status()
return response.json()

0 comments on commit fa2c99a

Please sign in to comment.