mirror of
https://github.com/ONLYOFFICE/Docker-Docs.git
synced 2025-04-18 15:04:02 +03:00
Add service that will watch on balancer configMap
Co-authored-by: danilapog <danil.titarenko@onlyoffice.com> Co-committed-by: danilapog <danil.titarenko@onlyoffice.com>
This commit is contained in:
parent
cf121ba60e
commit
2523cceaee
@ -15,8 +15,7 @@ RUN apt-get update -y && \
|
||||
rm -rf /var/lib/apt/lists/* && \
|
||||
wget -O /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.5/dumb-init_1.2.5_$(uname -m) && \
|
||||
chmod +x /usr/local/bin/dumb-init && \
|
||||
mkdir -p /ds_ep_observer \
|
||||
/cm_observer \
|
||||
mkdir -p /scripts \
|
||||
/var/tmp/docs_cache \
|
||||
/etc/nginx/mnt_config \
|
||||
/etc/nginx/lua && \
|
||||
@ -25,13 +24,15 @@ RUN apt-get update -y && \
|
||||
|
||||
COPY scripts/ds-ep-observer.py \
|
||||
scripts/ds-pod-observer.py \
|
||||
/ds_ep_observer/
|
||||
COPY scripts/balancer-shutdown.py /
|
||||
scripts/balancer-shutdown.py \
|
||||
scripts/balancer-cm-observer.py \
|
||||
/scripts/
|
||||
COPY config/balancer/conf.d/balancer-server.conf /etc/nginx/conf.d/
|
||||
COPY config/balancer/conf.d/handler-server.conf /etc/nginx/conf.d/
|
||||
COPY config/balancer/nginx.conf /usr/local/openresty/nginx/conf/
|
||||
COPY config/balancer/lua/configuration.lua /etc/nginx/lua/
|
||||
COPY config/balancer/lua/docs_balancer.lua /etc/nginx/lua/
|
||||
COPY config/balancer/balancer-lua.conf /etc/nginx/mnt_config/
|
||||
COPY --chmod=755 balancer-docker-entrypoint.py /docker_entrypoint.py
|
||||
|
||||
EXPOSE 80 443
|
||||
|
@ -38,9 +38,10 @@ def set_nginx_parameter():
|
||||
def running_services():
|
||||
try:
|
||||
running_nginx = ["/usr/local/openresty/bin/openresty", "-g", "daemon off;"]
|
||||
running_get_ds_ep = ["python3", "/ds_ep_observer/ds-ep-observer.py"]
|
||||
running_get_ds_pod = ["python3", "/ds_ep_observer/ds-pod-observer.py"]
|
||||
all_cmd = [running_nginx, running_get_ds_ep, running_get_ds_pod]
|
||||
running_cm_observer = ["python3", "/scripts/balancer-cm-observer.py"]
|
||||
running_get_ds_ep = ["python3", "/scripts/ds-ep-observer.py"]
|
||||
running_get_ds_pod = ["python3", "/scripts/ds-pod-observer.py"]
|
||||
all_cmd = [running_nginx, running_cm_observer, running_get_ds_ep, running_get_ds_pod]
|
||||
for cmd in all_cmd:
|
||||
cmd_process = subprocess.Popen(cmd)
|
||||
logger_endpoints_ds.info(f'The "{cmd_process.pid}" process has been running')
|
||||
|
3
config/balancer/balancer-lua.conf
Normal file
3
config/balancer/balancer-lua.conf
Normal file
@ -0,0 +1,3 @@
|
||||
default_type text/plain;
|
||||
return 200 "Hello, app is not ready. Please wait...\n";
|
||||
# Just simple placeholder that return 200 until configMap is not initiated;
|
75
scripts/balancer-cm-observer.py
Normal file
75
scripts/balancer-cm-observer.py
Normal file
@ -0,0 +1,75 @@
|
||||
from kubernetes import client, watch
|
||||
import os
|
||||
import logging
|
||||
import requests
|
||||
import json
|
||||
import time
|
||||
import subprocess
|
||||
|
||||
cm_name = "balancer-lua-config"
|
||||
cm_key = "balancer-lua.conf"
|
||||
cm_path = "/etc/nginx/mnt_config/balancer-lua.conf"
|
||||
|
||||
k8s_host = os.environ["KUBERNETES_SERVICE_HOST"]
|
||||
api_server = f'https://{k8s_host}'
|
||||
pathCrt = '/run/secrets/kubernetes.io/serviceaccount/ca.crt'
|
||||
pathToken = '/run/secrets/kubernetes.io/serviceaccount/token'
|
||||
pathNS = '/run/secrets/kubernetes.io/serviceaccount/namespace'
|
||||
|
||||
with open(pathToken, "r") as f_tok:
|
||||
token = f_tok.read()
|
||||
|
||||
with open(pathNS, "r") as f_ns:
|
||||
ns = f_ns.read()
|
||||
|
||||
configuration = client.Configuration()
|
||||
configuration.ssl_ca_cert = pathCrt
|
||||
configuration.host = api_server
|
||||
configuration.verify_ssl = True
|
||||
configuration.debug = False
|
||||
configuration.api_key = {"authorization": "Bearer " + token}
|
||||
client.Configuration.set_default(configuration)
|
||||
|
||||
v1 = client.CoreV1Api()
|
||||
|
||||
def init_logger(name):
|
||||
logger = logging.getLogger(name)
|
||||
formatter = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
||||
logger.setLevel(logging.DEBUG)
|
||||
stdout = logging.StreamHandler()
|
||||
stdout.setFormatter(logging.Formatter(formatter))
|
||||
stdout.setLevel(logging.DEBUG)
|
||||
logger.addHandler(stdout)
|
||||
logger.info('Running Docs-Balancer configMap observer service...')
|
||||
|
||||
# Watch for changes in the ConfigMap
|
||||
def watch_configmap_changes():
|
||||
field_selector = f"metadata.name={cm_name}"
|
||||
while True:
|
||||
try:
|
||||
w = watch.Watch()
|
||||
for event in w.stream(v1.list_namespaced_config_map, namespace=ns, field_selector=field_selector):
|
||||
try:
|
||||
f = open(cm_path, "w")
|
||||
f.write(event['object'].data[cm_key])
|
||||
f.close()
|
||||
reload_nginx()
|
||||
except Exception as msg_write_cm:
|
||||
logger_cm_observer.error(f'Cant write in config file...{msg_write_cm}')
|
||||
except Exception as msg_get_cm:
|
||||
logger_cm_observer.warning(f'Trying to get cm data...{msg_get_cm}')
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
def reload_nginx():
|
||||
try:
|
||||
subprocess.run(['nginx', '-s', 'reload'], check=True)
|
||||
except Exception as quit_msg:
|
||||
logger_cm_observer.error(f'Failed nginx reload attempt: "{quit_msg}"\n')
|
||||
|
||||
# Init logger
|
||||
init_logger('balancer')
|
||||
logger_cm_observer = logging.getLogger('balancer.cm')
|
||||
|
||||
# Start watching for changes
|
||||
watch_configmap_changes()
|
@ -93,7 +93,8 @@ def ngx_shutdown():
|
||||
def shutdown_services():
|
||||
services = [
|
||||
'ds-ep-observer.py',
|
||||
'ds-pod-observer.py'
|
||||
'ds-pod-observer.py',
|
||||
'balancer-cm-observer.py'
|
||||
]
|
||||
try:
|
||||
for service in services:
|
||||
|
Loading…
x
Reference in New Issue
Block a user