Skip to content

Commit

Permalink
Additional changes for dual health processes
Browse files Browse the repository at this point in the history
  • Loading branch information
NotChristianGarcia committed Oct 20, 2023
1 parent 2071dd0 commit 7af5989
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
1 change: 1 addition & 0 deletions deployment-template/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ kind: Service
metadata:
name: MAKEFILE_SERVICE_NAME-rabbitmq
spec:
type: NodePort
selector:
app: MAKEFILE_SERVICE_NAME-rabbitmq
ports:
Expand Down
29 changes: 2 additions & 27 deletions service/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from channels import CommandChannel
from kubernetes import client, config
from kubernetes_utils import get_current_k8_services, get_current_k8_pods, rm_container, rm_pvc, \
get_current_k8_pods, rm_service, KubernetesError, update_traefik_configmap, get_k8_logs, list_all_containers, run_k8_exec
get_current_k8_pods, rm_service, KubernetesError, get_k8_logs, list_all_containers, run_k8_exec
from codes import AVAILABLE, DELETING, STOPPED, ERROR, REQUESTED, COMPLETE, RESTART, ON, OFF
from stores import pg_store, SITE_TENANT_DICT
from models_pods import Pod
Expand Down Expand Up @@ -259,7 +259,7 @@ def check_k8_services():
continue

def check_db_pods(k8_pods):
"""Go through database for all tenants in this site. Delete/Create whatever is needed. Do proxy config stuff.
"""Go through database for all tenants in this site. Delete/Create whatever is needed.
"""
all_pods = []
stmt = select(Pod)
Expand Down Expand Up @@ -361,31 +361,6 @@ def check_db_pods(k8_pods):
logger.debug(f"Command Channel - Added msg for pod_id: {pod.pod_id}.")


### Proxy ports and config changes
# For proxy config later. proxy_info_x = {pod.k8_name: {routing_port, url}, ...}
tcp_proxy_info = {}
http_proxy_info = {}
postgres_proxy_info = {}
for pod in all_pods:
# Each pod can have up to 3 networking objects with custom filled port/protocol/name
for net_name, net_info in pod.networking.items():
if not isinstance(net_info, dict):
net_info = net_info.dict()

template_info = {"routing_port": net_info['port'],
"url": net_info['url']}
match net_info['protocol']:
case "tcp":
tcp_proxy_info[pod.k8_name] = template_info
case "http":
http_proxy_info[pod.k8_name] = template_info
case "postgres":
postgres_proxy_info[pod.k8_name] = template_info

# This functions only updates if config is out of date.
update_traefik_configmap(tcp_proxy_info, http_proxy_info, postgres_proxy_info)


def main():
# Try and run check_db_pods. Will try for 60 seconds until health is declared "broken".
logger.info("Top of health. Checking if db's are initialized.")
Expand Down
36 changes: 34 additions & 2 deletions service/health_central.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Does the following:
1. Does startup work for NFS mount
2. Goes through NFS mount and does cleaning
3. Deals with traefik logs and metrics in general
3. Deals with traefik proxy, logs, and metrics
"""

Expand Down Expand Up @@ -193,6 +193,37 @@ def nfs_folder_init(tenant):
raise BaseTapyException(msg)


def set_traefik_proxy():
all_pods = []
stmt = select(Pod)
for tenant in SITE_TENANT_DICT[conf.site_id]:
all_pods += pg_store[conf.site_id][tenant].run("execute", stmt, scalars=True, all=True)

### Proxy ports and config changes
# For proxy config later. proxy_info_x = {pod.k8_name: {routing_port, url}, ...}
tcp_proxy_info = {}
http_proxy_info = {}
postgres_proxy_info = {}
for pod in all_pods:
# Each pod can have up to 3 networking objects with custom filled port/protocol/name
for net_name, net_info in pod.networking.items():
if not isinstance(net_info, dict):
net_info = net_info.dict()

template_info = {"routing_port": net_info['port'],
"url": net_info['url']}
match net_info['protocol']:
case "tcp":
tcp_proxy_info[pod.k8_name] = template_info
case "http":
http_proxy_info[pod.k8_name] = template_info
case "postgres":
postgres_proxy_info[pod.k8_name] = template_info

# This functions only updates if config is out of date.
update_traefik_configmap(tcp_proxy_info, http_proxy_info, postgres_proxy_info)


def main():
# Try and run check_db_pods. Will try for 60 seconds until health is declared "broken".
logger.info("Top of health. Checking if db's are initialized.")
Expand All @@ -219,7 +250,8 @@ def main():
while True:
logger.info(f"Running pods health checks. Now: {time.time()}")
check_nfs_files()


set_traefik_proxy()
### Have a short wait
time.sleep(3)

Expand Down
3 changes: 2 additions & 1 deletion service/kubernetes_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,11 @@ def update_traefik_configmap(tcp_proxy_info: Dict[str, Dict[str, str]],
current_template = k8.read_namespaced_config_map(name='pods-traefik-conf', namespace=NAMESPACE)

if not current_template.data['traefik.yml'] == rendered_template:
logger.debug("Health found difference in Traefik configs, updated configmap.")
# Update the configmap with the new template immediately.
config_map = client.V1ConfigMap(data = {"traefik.yml": rendered_template})
k8.patch_namespaced_config_map(name='pods-traefik-conf', namespace=NAMESPACE, body=config_map)
# Auto updates proxxy pod. Changes take place according to kubelet sync frequency duration (60s default).
# Auto updates proxy pod. Changes take place according to kubelet sync frequency duration (60s default).

def get_traefik_configmap():
"""
Expand Down

0 comments on commit 7af5989

Please sign in to comment.