2024-03-18 16:07:48 +02:00
import base64
import json
2024-03-18 18:53:38 +02:00
import yaml
2024-03-18 16:07:48 +02:00
import logging
2024-03-18 22:53:43 +02:00
import threading
import time
2024-03-18 20:36:07 +02:00
from kubernetes import client , config as kube_config
2024-03-18 16:07:48 +02:00
from kubernetes . client . rest import ApiException
logging . basicConfig (
level = logging . INFO ,
format = " %(asctime)s - %(name)s - %(levelname)s - %(message)s " ,
datefmt = " %d - % m- % Y % H: % M: % S " ,
)
log = logging . getLogger ( " OutFleet.k8s " )
file_handler = logging . FileHandler ( " sync.log " )
file_handler . setLevel ( logging . DEBUG )
formatter = logging . Formatter (
" %(asctime)s - %(name)s - %(levelname)s - %(message)s "
)
file_handler . setFormatter ( formatter )
log . addHandler ( file_handler )
2024-03-18 18:53:38 +02:00
def write_config ( config ) :
config_map = client . V1ConfigMap (
api_version = " v1 " ,
kind = " ConfigMap " ,
metadata = client . V1ObjectMeta (
name = f " config-outfleet " ,
labels = {
" app " : " outfleet " ,
}
) ,
data = { " config.yaml " : yaml . dump ( config ) }
)
try :
2024-03-18 20:02:33 +02:00
api_response = V1 . create_namespaced_config_map (
2024-03-18 18:53:38 +02:00
namespace = NAMESPACE ,
body = config_map ,
)
except ApiException as e :
2024-03-18 20:02:33 +02:00
api_response = V1 . patch_namespaced_config_map (
2024-03-18 18:53:38 +02:00
name = " config-outfleet " ,
namespace = NAMESPACE ,
body = config_map ,
)
2024-03-18 21:30:34 +02:00
log . info ( " Updated config in Kubernetes ConfigMap [config-outfleet] " )
2024-03-18 18:53:38 +02:00
NAMESPACE = False
SERVERS = list ( )
CONFIG = None
2024-03-18 20:02:33 +02:00
V1 = None
2024-03-18 16:07:48 +02:00
2024-03-18 22:53:43 +02:00
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 )
2024-03-18 16:07:48 +02:00
try :
2024-03-18 20:36:07 +02:00
kube_config . load_incluster_config ( )
2024-03-18 20:02:33 +02:00
V1 = client . CoreV1Api ( )
2024-03-18 20:36:07 +02:00
try :
with open ( " /var/run/secrets/kubernetes.io/serviceaccount/namespace " ) as f :
NAMESPACE = f . read ( ) . strip ( )
log . info ( f " Found Kubernetes environment. Deployed to namespace ' { NAMESPACE } ' " )
try :
CONFIG = yaml . safe_load ( V1 . read_namespaced_config_map ( name = " config-outfleet " , namespace = NAMESPACE ) . data [ ' config.yaml ' ] )
2024-03-18 22:53:43 +02:00
log . info ( f " ConfigMap loaded from Kubernetes API. Servers: { len ( CONFIG [ ' servers ' ] ) } , Clients: { len ( CONFIG [ ' clients ' ] ) } . Started monitoring for changes every minute. " )
2024-03-18 20:36:07 +02:00
except Exception as e :
2024-03-18 20:42:14 +02:00
try :
2024-03-18 21:30:34 +02:00
write_config ( { " clients " : [ ] , " servers " : { } , " ui_hostname " : " accessible-address.com " } )
2024-03-18 20:42:14 +02:00
CONFIG = yaml . safe_load ( V1 . read_namespaced_config_map ( name = " config-outfleet " , namespace = NAMESPACE ) . data [ ' config.yaml ' ] )
2024-03-18 22:53:43 +02:00
log . info ( " Created new ConfigMap [config-outfleet]. Started monitoring for changes every minute. " )
2024-03-18 20:42:14 +02:00
except Exception as e :
2024-03-18 22:08:43 +02:00
log . info ( f " Failed to create new ConfigMap [config-outfleet] { e } " )
2024-03-18 22:53:43 +02:00
thread = threading . Thread ( target = reload_config )
thread . start ( )
2024-03-18 20:36:07 +02:00
except :
2024-03-18 21:30:34 +02:00
log . info ( " Kubernetes environment not detected " )
2024-03-18 19:50:12 +02:00
except :
2024-03-18 16:07:48 +02:00
log . info ( " Kubernetes environment not detected " )
2024-03-18 22:53:43 +02:00