From f17dd7a04bd384aaef1bb679dbab0b9749294ea7 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Mon, 12 Jul 2021 13:06:25 +0900 Subject: [PATCH] Add optional healthcheck middleware This change introduces a new option, [healthcheck] enabled, which enables the healthcheck middleware in mistral-api pipeline. This middleware allows status check at /healthcheck path, which is useful for load balancers or any monitoring services to validate health of its backend services. This change is created based on the same change proposed to ironic[1]. [1] 6f439414bdcef9fc02f844f475ec798d48d42558 Co-Authored-By: Jim Rollenhagen Change-Id: I9bf3de8a5ae6a8c9346285147732b773a3667f7e --- mistral/api/app.py | 9 ++++-- mistral/config.py | 14 +++++++++ .../tests/unit/api/test_oslo_middleware.py | 30 +++++++++++++++++-- .../notes/healthcheck-b2757de5e49a868b.yaml | 5 ++++ tools/config/config-generator.mistral.conf | 1 + 5 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 releasenotes/notes/healthcheck-b2757de5e49a868b.yaml diff --git a/mistral/api/app.py b/mistral/api/app.py index e26b37a4a..e0c20f7a3 100644 --- a/mistral/api/app.py +++ b/mistral/api/app.py @@ -15,7 +15,8 @@ from oslo_config import cfg import oslo_middleware.cors as cors_middleware -import oslo_middleware.http_proxy_to_wsgi as http_proxy_to_wsgi_middleware +from oslo_middleware import healthcheck +from oslo_middleware import http_proxy_to_wsgi import osprofiler.web import pecan @@ -85,7 +86,11 @@ def setup_app(config=None): ) # Create HTTPProxyToWSGI wrapper - app = http_proxy_to_wsgi_middleware.HTTPProxyToWSGI(app, cfg.CONF) + app = http_proxy_to_wsgi.HTTPProxyToWSGI(app, cfg.CONF) + + # Create a healthcheck wrapper + if cfg.CONF.healthcheck.enabled: + app = healthcheck.Healthcheck(app, cfg.CONF) # Create a CORS wrapper, and attach mistral-specific defaults that must be # included in all CORS responses. diff --git a/mistral/config.py b/mistral/config.py index 8a4b2dc3f..3d16454af 100644 --- a/mistral/config.py +++ b/mistral/config.py @@ -705,6 +705,17 @@ yaql_opts = [ ) ] + +healthcheck_opts = [ + cfg.BoolOpt('enabled', + default=False, + help=_('Enable the health check endpoint at /healthcheck. ' + 'Note that this is unauthenticated. More information ' + 'is available at ' + 'https://docs.openstack.org/oslo.middleware/latest/' + 'reference/healthcheck_plugins.html.')), +] + CONF = cfg.CONF LEGACY_ACTION_PROVIDER_GROUP = 'legacy_action_provider' @@ -723,6 +734,7 @@ ACTION_LOGGING_GROUP = 'action_logging' PROFILER_GROUP = profiler.list_opts()[0][0] KEYCLOAK_OIDC_GROUP = "keycloak_oidc" YAQL_GROUP = "yaql" +HEALTHCHECK_GROUP = 'healthcheck' KEYSTONE_GROUP = "keystone" @@ -760,6 +772,7 @@ CONF.register_opts(coordination_opts, group=COORDINATION_GROUP) CONF.register_opts(profiler_opts, group=PROFILER_GROUP) CONF.register_opts(keycloak_oidc_opts, group=KEYCLOAK_OIDC_GROUP) CONF.register_opts(yaql_opts, group=YAQL_GROUP) +CONF.register_opts(healthcheck_opts, group=HEALTHCHECK_GROUP) loading.register_session_conf_options(CONF, KEYSTONE_GROUP) CLI_OPTS = [ @@ -807,6 +820,7 @@ def list_opts(): (PROFILER_GROUP, profiler_opts), (KEYCLOAK_OIDC_GROUP, keycloak_oidc_opts), (YAQL_GROUP, yaql_opts), + (HEALTHCHECK_GROUP, healthcheck_opts), (ACTION_HEARTBEAT_GROUP, action_heartbeat_opts), (ACTION_LOGGING_GROUP, action_logging_opts), (None, default_group_opts) diff --git a/mistral/tests/unit/api/test_oslo_middleware.py b/mistral/tests/unit/api/test_oslo_middleware.py index ab7038dcf..6adceaec0 100644 --- a/mistral/tests/unit/api/test_oslo_middleware.py +++ b/mistral/tests/unit/api/test_oslo_middleware.py @@ -16,7 +16,8 @@ from mistral.tests.unit.api import base from oslo_config import cfg -from oslo_middleware import http_proxy_to_wsgi as http_proxy_to_wsgi_middleware +from oslo_middleware import healthcheck +from oslo_middleware import http_proxy_to_wsgi class TestHTTPProxyToWSGIMiddleware(base.APITest): @@ -28,15 +29,38 @@ class TestHTTPProxyToWSGIMiddleware(base.APITest): def setUp(self): # Make sure the HTTPProxyToWSGI options are registered - cfg.CONF.register_opts(http_proxy_to_wsgi_middleware.OPTS, + cfg.CONF.register_opts(http_proxy_to_wsgi.OPTS, 'oslo_middleware') # Enable proxy headers parsing in HTTPProxyToWSGI middleware. self.override_config( "enable_proxy_headers_parsing", - "True", + True, group='oslo_middleware' ) # Create the application. super(TestHTTPProxyToWSGIMiddleware, self).setUp() + + +class TestHealthcheckMiddleware(base.APITest): + """Test oslo_middleware Healthcheck. + + It checks that oslo_middleware middleware Healthcheck is executed + when enabled. + """ + + def setUp(self): + # Make sure the Healthcheck options are registered + cfg.CONF.register_opts(healthcheck.OPTS, + 'oslo_middleware') + + # Enable healthcheck middleware. + self.override_config( + "enabled", + True, + group='healthcheck' + ) + + # Create the application. + super(TestHealthcheckMiddleware, self).setUp() diff --git a/releasenotes/notes/healthcheck-b2757de5e49a868b.yaml b/releasenotes/notes/healthcheck-b2757de5e49a868b.yaml new file mode 100644 index 000000000..cada81000 --- /dev/null +++ b/releasenotes/notes/healthcheck-b2757de5e49a868b.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + The new ``[healthcheck] enabled`` option has been added. This option can + be used to enable the ``helthcheck`` middleware in mistral-api service. diff --git a/tools/config/config-generator.mistral.conf b/tools/config/config-generator.mistral.conf index 6ed91ae30..70197289e 100644 --- a/tools/config/config-generator.mistral.conf +++ b/tools/config/config-generator.mistral.conf @@ -3,6 +3,7 @@ namespace = mistral.config namespace = oslo.db namespace = oslo.messaging namespace = oslo.middleware.cors +namespace = oslo.middleware.healthcheck namespace = oslo.middleware.http_proxy_to_wsgi namespace = keystonemiddleware.auth_token namespace = oslo.log