mirror of
https://github.com/house-of-vanity/OutFleet.git
synced 2025-07-07 01:24:06 +00:00
fix V1 api define
This commit is contained in:
37
k8s.py
37
k8s.py
@ -2,7 +2,7 @@ import base64
|
|||||||
import json
|
import json
|
||||||
import yaml
|
import yaml
|
||||||
import logging
|
import logging
|
||||||
from kubernetes import client, config
|
from kubernetes import client, config as kube_config
|
||||||
from kubernetes.client.rest import ApiException
|
from kubernetes.client.rest import ApiException
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
@ -51,25 +51,28 @@ CONFIG = None
|
|||||||
V1 = None
|
V1 = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
config.load_incluster_config()
|
kube_config.load_incluster_config()
|
||||||
V1 = client.CoreV1Api()
|
V1 = client.CoreV1Api()
|
||||||
with open("/var/run/secrets/kubernetes.io/serviceaccount/namespace") as f:
|
try:
|
||||||
NAMESPACE = f.read().strip()
|
with open("/var/run/secrets/kubernetes.io/serviceaccount/namespace") as f:
|
||||||
log.info(f"Found Kubernetes environment. Deployed to namespace '{NAMESPACE}'")
|
NAMESPACE = f.read().strip()
|
||||||
CONFIG = yaml.safe_load(V1.read_namespaced_config_map(name="config-outfleet", namespace=NAMESPACE).data['config.yaml'])
|
log.info(f"Found Kubernetes environment. Deployed to namespace '{NAMESPACE}'")
|
||||||
log.info(f"ConfigMap config.yaml loaded from Kubernetes API. Servers: {len(CONFIG['servers'])}, Clients: {len(CONFIG['clients'])}")
|
try:
|
||||||
|
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'])}")
|
||||||
|
except Exception as e:
|
||||||
|
log.error("ConfigMap not found. {e}")
|
||||||
|
except:
|
||||||
|
pass
|
||||||
except:
|
except:
|
||||||
log.info("Kubernetes environment not detected")
|
log.info("Kubernetes environment not detected")
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
CONFIG = yaml.safe_load(V1.read_namespaced_config_map(name="config-outfleet", namespace=NAMESPACE).data['config.yaml'])
|
if not CONFIG:
|
||||||
log.info(f"ConfigMap config.yaml loaded from Kubernetes API. Servers: {len(CONFIG['servers'])}, Clients: {len(CONFIG['clients'])}")
|
log.info(f"Creating new ConfigMap [config-outfleet]")
|
||||||
except ApiException as e:
|
write_config({"clients": [], "servers": [], "ui_hostname": "accessible-address.com"})
|
||||||
log.warning(f"ConfigMap not found. Fisrt run?")
|
CONFIG = yaml.safe_load(V1.read_namespaced_config_map(name="config-outfleet", namespace=NAMESPACE).data['config.yaml'])
|
||||||
|
except:
|
||||||
if not CONFIG:
|
pass
|
||||||
log.info(f"Creating new ConfigMap [config-outfleet]")
|
|
||||||
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'])
|
|
||||||
|
|
||||||
|
12
lib.py
12
lib.py
@ -23,7 +23,9 @@ parser.add_argument(
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
def get_config():
|
def get_config():
|
||||||
if not k8s.NAMESPACE:
|
if k8s.CONFIG:
|
||||||
|
return k8s.CONFIG
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
with open(args.config, "r") as file:
|
with open(args.config, "r") as file:
|
||||||
config = yaml.safe_load(file)
|
config = yaml.safe_load(file)
|
||||||
@ -35,18 +37,16 @@ def get_config():
|
|||||||
log.error(f"Couldn't create config. {exp}")
|
log.error(f"Couldn't create config. {exp}")
|
||||||
return None
|
return None
|
||||||
return config
|
return config
|
||||||
else:
|
|
||||||
return k8s.CONFIG
|
|
||||||
|
|
||||||
def write_config(config):
|
def write_config(config):
|
||||||
if not k8s.NAMESPACE:
|
if k8s.CONFIG:
|
||||||
|
k8s.write_config(config)
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
with open(args.config, "w") as file:
|
with open(args.config, "w") as file:
|
||||||
yaml.safe_dump(config, file)
|
yaml.safe_dump(config, file)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
log.error(f"Couldn't write Outfleet config: {e}")
|
log.error(f"Couldn't write Outfleet config: {e}")
|
||||||
else:
|
|
||||||
k8s.write_config(config)
|
|
||||||
|
|
||||||
|
|
||||||
class ServerDict(TypedDict):
|
class ServerDict(TypedDict):
|
||||||
|
Reference in New Issue
Block a user