autoconf - init refactoring before k8s integration
This commit is contained in:
parent
0597074438
commit
01bba1d3f6
23
autoconf/src/Controller.py
Normal file
23
autoconf/src/Controller.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from Config import Config
|
||||||
|
|
||||||
|
class ControllerType(Enum) :
|
||||||
|
DOCKER = 1
|
||||||
|
SWARM = 2
|
||||||
|
KUBERNETES = 3
|
||||||
|
|
||||||
|
class Controller(ABC) :
|
||||||
|
|
||||||
|
def __init__(self, type) :
|
||||||
|
self.__config = Config.from_controller_type(type)
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_env(self) :
|
||||||
|
pass
|
||||||
|
|
||||||
|
def gen_conf(self, env) :
|
||||||
|
return self.__config.gen(env)
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def process_events(self) :
|
||||||
|
pass
|
||||||
32
autoconf/src/DockerController.py
Normal file
32
autoconf/src/DockerController.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import docker
|
||||||
|
from Controller import Controller, ControllerType
|
||||||
|
import utils
|
||||||
|
|
||||||
|
class DockerController(Controller) :
|
||||||
|
|
||||||
|
def __init__(self) :
|
||||||
|
super().__init__(ControllerType.DOCKER)
|
||||||
|
# TODO : honor env vars like DOCKER_HOST
|
||||||
|
self.__client = docker.DockerClient(base_url='unix:///var/run/docker.sock')
|
||||||
|
|
||||||
|
def __get_instances(self) :
|
||||||
|
return self.__client.containers.list(filters={"label" : "bunkerized-nginx.AUTOCONF"})
|
||||||
|
|
||||||
|
def __get_containers(self) :
|
||||||
|
return self.__client.containers.list(filters={"label" : "bunkerized-nginx.SERVER_NAME"})
|
||||||
|
|
||||||
|
def get_env(self) :
|
||||||
|
env = {}
|
||||||
|
for instance in self._get_instances() :
|
||||||
|
for variable in instance.attrs["Config"]["Env"] :
|
||||||
|
env[variable.split("=")[0]] = variable.replace(variable.split("=")[0] + "=", "", 1)
|
||||||
|
pass
|
||||||
|
|
||||||
|
def process_events(self, current_env) :
|
||||||
|
old_env = current_env
|
||||||
|
for event in client.events(decode=True, filter={"type": "container", "label": ["bunkerized-nginx.AUTOCONF", "bunkerized-nginx.SERVER_NAME"]}) :
|
||||||
|
new_env = self.get_env()
|
||||||
|
if new_env != old_env :
|
||||||
|
if (self.gen_conf(new_env)) :
|
||||||
|
old_env = new_env
|
||||||
|
utils.log("[*] Successfully generated new configuration")
|
||||||
@ -1,14 +1,16 @@
|
|||||||
from kubernetes import client, config, watch
|
from kubernetes import client, config, watch
|
||||||
from threading import Thread, Lock
|
from threading import Thread, Lock
|
||||||
|
|
||||||
|
from Controller import Controller
|
||||||
|
|
||||||
class IngressController :
|
class IngressController :
|
||||||
|
|
||||||
def __init__(self) :
|
def __init__(self) :
|
||||||
config.load_kube_config()
|
super().__init__()
|
||||||
|
config.load_incluster_config()
|
||||||
self.__api = client.CoreV1Api()
|
self.__api = client.CoreV1Api()
|
||||||
self.__extensions_api = client.ExtensionsV1beta1Api()
|
self.__extensions_api = client.ExtensionsV1beta1Api()
|
||||||
self.__lock = Lock()
|
self.__lock = Lock()
|
||||||
self.__last_conf = {}
|
|
||||||
|
|
||||||
def __annotations_to_env(self, annotations, service=False) :
|
def __annotations_to_env(self, annotations, service=False) :
|
||||||
env = {}
|
env = {}
|
||||||
@ -28,15 +30,17 @@ class IngressController :
|
|||||||
prefix = ""
|
prefix = ""
|
||||||
if "host" in rule :
|
if "host" in rule :
|
||||||
prefix = rule["host"] + "_"
|
prefix = rule["host"] + "_"
|
||||||
|
if not "http" in rule or not "paths" in rule["http"] :
|
||||||
|
continue
|
||||||
for path in rule["http"]["paths"] :
|
for path in rule["http"]["paths"] :
|
||||||
env[prefix + "USE_REVERSE_PROXY"] = "yes"
|
env[prefix + "USE_REVERSE_PROXY"] = "yes"
|
||||||
env[prefix + "REVERSE_PROXY_URL"] = path["path"]
|
env[prefix + "REVERSE_PROXY_URL"] = path["path"]
|
||||||
env[prefix + "REVERSE_PROXY_HOST"] = "http://" + path["backend"]["serviceName"] + ":" + str(path["backend"]["servicePort"])
|
env[prefix + "REVERSE_PROXY_HOST"] = "http://" + path["backend"]["serviceName"] + ":" + str(path["backend"]["servicePort"])
|
||||||
return env
|
return env
|
||||||
|
|
||||||
def gen_conf(self) :
|
def get_env(self) :
|
||||||
ingresses = self.get_ingresses()
|
ingresses = self.__get_ingresses()
|
||||||
services = self.get_services()
|
services = self.__get_services()
|
||||||
env = {}
|
env = {}
|
||||||
for ingress in ingresses :
|
for ingress in ingresses :
|
||||||
if ingress.metadata.annotations == None :
|
if ingress.metadata.annotations == None :
|
||||||
@ -49,16 +53,12 @@ class IngressController :
|
|||||||
continue
|
continue
|
||||||
if "bunkerized-nginx.AUTOCONF" in service.metadata.annotations :
|
if "bunkerized-nginx.AUTOCONF" in service.metadata.annotations :
|
||||||
env.update(self.__annotations_to_env(service.metadata.annotations, service=True))
|
env.update(self.__annotations_to_env(service.metadata.annotations, service=True))
|
||||||
if self.__last_conf != env :
|
return env
|
||||||
self.__last_conf = env
|
|
||||||
print("*** NEW CONF ***")
|
|
||||||
for k, v in env.items() :
|
|
||||||
print(k + " = " + v)
|
|
||||||
|
|
||||||
def get_ingresses(self) :
|
def __get_ingresses(self) :
|
||||||
return self.__extensions_api.list_ingress_for_all_namespaces(watch=False).items
|
return self.__extensions_api.list_ingress_for_all_namespaces(watch=False).items
|
||||||
|
|
||||||
def get_services(self) :
|
def __get_services(self) :
|
||||||
return self.__api.list_service_for_all_namespaces(watch=False).items
|
return self.__api.list_service_for_all_namespaces(watch=False).items
|
||||||
|
|
||||||
def watch_ingress(self) :
|
def watch_ingress(self) :
|
||||||
@ -5,6 +5,9 @@ from ReloadServer import run_reload_server
|
|||||||
import utils
|
import utils
|
||||||
import docker, os, stat, sys, select, threading
|
import docker, os, stat, sys, select, threading
|
||||||
|
|
||||||
|
# Check if we are in Swarm mode
|
||||||
|
swarm = os.getenv("SWARM_MODE") == "yes"
|
||||||
|
if swarm :
|
||||||
# Connect to the endpoint
|
# Connect to the endpoint
|
||||||
endpoint = "/var/run/docker.sock"
|
endpoint = "/var/run/docker.sock"
|
||||||
if not os.path.exists(endpoint) or not stat.S_ISSOCK(os.stat(endpoint).st_mode) :
|
if not os.path.exists(endpoint) or not stat.S_ISSOCK(os.stat(endpoint).st_mode) :
|
||||||
@ -16,9 +19,6 @@ except Exception as e :
|
|||||||
utils.log("[!] Can't instantiate DockerClient : " + str(e))
|
utils.log("[!] Can't instantiate DockerClient : " + str(e))
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
|
|
||||||
# Check if we are in Swarm mode
|
|
||||||
swarm = os.getenv("SWARM_MODE") == "yes"
|
|
||||||
|
|
||||||
# Our object to process events
|
# Our object to process events
|
||||||
api = ""
|
api = ""
|
||||||
if swarm :
|
if swarm :
|
||||||
Loading…
x
Reference in New Issue
Block a user