2024-03-18 16:07:48 +02:00
import base64
import json
2024-03-19 01:11:08 +02:00
import uuid
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-19 01:11:08 +02:00
import lib
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-19 01:11:08 +02:00
def discovery_servers ( ) :
global CONFIG
interval = 10
log = logging . getLogger ( " OutFleet.discovery " )
with lib . lock :
while True :
pods = V1 . list_namespaced_pod ( NAMESPACE , label_selector = " app=shadowbox " )
log . debug ( f " Started discovery thread every { interval } " )
for pod in pods . items :
log . debug ( f " Found Outline server pod { pod . metadata . name } " )
container_log = V1 . read_namespaced_pod_log ( name = pod . metadata . name , namespace = NAMESPACE , container = ' manager-config-json ' )
secret = json . loads ( container_log . replace ( ' \' ' , ' \" ' ) )
config = lib . get_config ( )
config_servers = find_server ( secret , config [ " servers " ] )
#log.info(f"config_servers {config_servers}")
if len ( config_servers ) > 0 :
log . debug ( f " Already exist " )
pass
else :
config [ " servers " ] [ str ( uuid . uuid4 ( ) ) ] = {
" cert " : secret [ " certSha256 " ] ,
" name " : f " { pod . metadata . name } " ,
" comment " : f " { pod . spec . node_name } " ,
" url " : secret [ " apiUrl " ] ,
}
write_config ( config )
log . info ( f " Added discovered server " )
time . sleep ( interval )
def find_server ( search_data , servers ) :
found_servers = { }
for server_id , server_info in servers . items ( ) :
if server_info [ " url " ] == search_data [ " apiUrl " ] and server_info [ " cert " ] == search_data [ " certSha256 " ] :
found_servers [ server_id ] = server_info
return found_servers
2024-03-18 18:53:38 +02:00
def write_config ( config ) :
config_map = client . V1ConfigMap (
api_version = " v1 " ,
kind = " ConfigMap " ,
metadata = client . V1ObjectMeta (
2024-03-19 01:44:38 +02:00
name = f " config-outfleet " ,
2024-03-18 18:53:38 +02:00
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-19 01:44:38 +02:00
name = " config-outfleet " ,
2024-03-18 18:53:38 +02:00
namespace = NAMESPACE ,
body = config_map ,
)
2024-03-19 01:44:38 +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 :
2024-03-19 01:44:38 +02:00
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]. " )
2024-03-18 22:53:43 +02:00
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 :
2024-03-19 01:44:38 +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 ( 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-19 01:44:38 +02:00
CONFIG = yaml . safe_load ( V1 . read_namespaced_config_map ( name = " config-outfleet " , namespace = NAMESPACE ) . data [ ' config.yaml ' ] )
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-19 01:44:38 +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-19 01:11:08 +02:00
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