fix k8s things

This commit is contained in:
2024-03-18 22:53:43 +02:00
parent f6a728ef1a
commit 77b78ec751
2 changed files with 54 additions and 37 deletions

18
k8s.py
View File

@ -2,6 +2,8 @@ import base64
import json import json
import yaml import yaml
import logging import logging
import threading
import time
from kubernetes import client, config as kube_config from kubernetes import client, config as kube_config
from kubernetes.client.rest import ApiException from kubernetes.client.rest import ApiException
@ -45,12 +47,19 @@ def write_config(config):
) )
log.info("Updated config in Kubernetes ConfigMap [config-outfleet]") log.info("Updated config in Kubernetes ConfigMap [config-outfleet]")
NAMESPACE = False NAMESPACE = False
SERVERS = list() SERVERS = list()
CONFIG = None CONFIG = None
V1 = None V1 = None
def reload_config():
global CONFIG
while True:
CONFIG = yaml.safe_load(V1.read_namespaced_config_map(name="config-outfleet", namespace=NAMESPACE).data['config.yaml'])
log.debug(f"Synced system config with ConfigMap [config-outfleet].")
time.sleep(30)
try: try:
kube_config.load_incluster_config() kube_config.load_incluster_config()
V1 = client.CoreV1Api() V1 = client.CoreV1Api()
@ -60,15 +69,18 @@ try:
log.info(f"Found Kubernetes environment. Deployed to namespace '{NAMESPACE}'") log.info(f"Found Kubernetes environment. Deployed to namespace '{NAMESPACE}'")
try: try:
CONFIG = yaml.safe_load(V1.read_namespaced_config_map(name="config-outfleet", namespace=NAMESPACE).data['config.yaml']) CONFIG = yaml.safe_load(V1.read_namespaced_config_map(name="config-outfleet", namespace=NAMESPACE).data['config.yaml'])
log.info(f"ConfigMap loaded from Kubernetes API. Servers: {len(CONFIG['servers'])}, Clients: {len(CONFIG['clients'])}") log.info(f"ConfigMap loaded from Kubernetes API. Servers: {len(CONFIG['servers'])}, Clients: {len(CONFIG['clients'])}. Started monitoring for changes every minute.")
except Exception as e: except Exception as e:
try: try:
write_config({"clients": [], "servers": {}, "ui_hostname": "accessible-address.com"}) write_config({"clients": [], "servers": {}, "ui_hostname": "accessible-address.com"})
CONFIG = yaml.safe_load(V1.read_namespaced_config_map(name="config-outfleet", namespace=NAMESPACE).data['config.yaml']) CONFIG = yaml.safe_load(V1.read_namespaced_config_map(name="config-outfleet", namespace=NAMESPACE).data['config.yaml'])
log.info("Created new ConfigMap [config-outfleet]") log.info("Created new ConfigMap [config-outfleet]. Started monitoring for changes every minute.")
except Exception as e: except Exception as e:
log.info(f"Failed to create new ConfigMap [config-outfleet] {e}") log.info(f"Failed to create new ConfigMap [config-outfleet] {e}")
thread = threading.Thread(target=reload_config)
thread.start()
except: except:
log.info("Kubernetes environment not detected") log.info("Kubernetes environment not detected")
except: except:
log.info("Kubernetes environment not detected") log.info("Kubernetes environment not detected")

View File

@ -1,3 +1,5 @@
import threading
import time
import yaml import yaml
import logging import logging
from datetime import datetime from datetime import datetime
@ -54,6 +56,7 @@ def random_string(length=64):
def update_state(): def update_state():
while True:
global SERVERS global SERVERS
global CLIENTS global CLIENTS
global BROKEN_SERVERS global BROKEN_SERVERS
@ -76,7 +79,7 @@ def update_state():
local_server_id=local_server_id, local_server_id=local_server_id,
) )
SERVERS.append(server) SERVERS.append(server)
log.info( log.debug(
"Server state updated: %s, [%s]", "Server state updated: %s, [%s]",
server.info()["name"], server.info()["name"],
local_server_id, local_server_id,
@ -90,6 +93,7 @@ def update_state():
log.warning("Can't access server: %s - %s", server_config["url"], e) log.warning("Can't access server: %s - %s", server_config["url"], e)
CLIENTS = config.get("clients", dict()) CLIENTS = config.get("clients", dict())
time.sleep(40)
@app.route("/", methods=["GET", "POST"]) @app.route("/", methods=["GET", "POST"])
@ -396,5 +400,6 @@ def sync():
if __name__ == "__main__": if __name__ == "__main__":
update_state() thread = threading.Thread(target=update_state)
thread.start()
app.run(host="0.0.0.0") app.run(host="0.0.0.0")