Skip to content

Config reload endpoint #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyms/flask/app/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pyms.crypt.driver import CryptResource
from pyms.flask.app.utils import SingletonMeta, ReverseProxied
from pyms.flask.healthcheck import healthcheck_blueprint
from pyms.flask.configreload import configreload_blueprint
from pyms.flask.services.driver import ServicesResource, DriverService
from pyms.logger import CustomJsonFormatter
from pyms.utils import check_package_exists, import_from
Expand Down Expand Up @@ -218,6 +219,7 @@ def create_app(self):

# Initialize Blueprints
self.application.register_blueprint(healthcheck_blueprint)
self.application.register_blueprint(configreload_blueprint)

self.init_libs()
self.add_error_handlers()
Expand Down
4 changes: 4 additions & 0 deletions pyms/flask/configreload/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .configreload import configreload_blueprint


__all__ = ['configreload_blueprint']
15 changes: 15 additions & 0 deletions pyms/flask/configreload/configreload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from __future__ import unicode_literals, print_function, absolute_import, division
from flask import Blueprint
from flask import current_app

configreload_blueprint = Blueprint('configreload', __name__, static_url_path='/static')


@configreload_blueprint.route('/reload-config', methods=['POST'])
def reloadconfig():
"""
Reread configuration from file.
:return:
"""
current_app.ms.reload_conf()
return "OK"
2 changes: 1 addition & 1 deletion pyms/flask/healthcheck/healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@healthcheck_blueprint.route('/healthcheck', methods=['GET'])
def healthcheck():
"""Set a healtcheck to help other service to discover this microservice, like Kubernetes, AWS ELB, etc.
"""Set a healthcheck to help other service to discover this microservice, like Kubernetes, AWS ELB, etc.
:return:
"""
return "OK"
5 changes: 5 additions & 0 deletions tests/config-tests-reload1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pyms:
config:
DEBUG: true
TESTING: true
APP_NAME: "reload1"
5 changes: 5 additions & 0 deletions tests/config-tests-reload2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pyms:
config:
DEBUG: true
TESTING: true
APP_NAME: "reload2"
25 changes: 25 additions & 0 deletions tests/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,31 @@ def test_reverse_proxy_zuul(self):
self.assertEqual(200, response.status_code)


class ReloadTests(unittest.TestCase):
"""
Tests for configreload endpoints
"""

file1 = "config-tests-reload1.yml"
file2 = "config-tests-reload2.yml"

def setUp(self):
os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(os.path.dirname(os.path.abspath(__file__)),
self.file1)
ms = MyMicroservice(path=__file__)
self.app = ms.create_app()
self.client = self.app.test_client()
self.assertEqual("reload1", self.app.config["APP_NAME"])

def test_configreload(self):
os.environ[CONFIGMAP_FILE_ENVIRONMENT] = os.path.join(os.path.dirname(os.path.abspath(__file__)),
self.file2)
response = self.client.post('/reload-config')
self.assertEqual(b"OK", response.data)
self.assertEqual(200, response.status_code)
self.assertEqual("reload2", config()["APP_NAME"])


class MicroserviceTest(unittest.TestCase):
"""
Tests for Singleton
Expand Down