From d7400b5dc4b71bea8b786aa0f1f64a30d5d701b1 Mon Sep 17 00:00:00 2001
From: Takashi Kajinami <tkajinam@redhat.com>
Date: Sun, 12 Sep 2021 22:44:02 +0900
Subject: [PATCH] Add optional healthcheck middleware

This change allows uses to enable the healthcheck middleware from
oslo.middleware in API pipeline, by setting the [healthcheck]/enabled
option. This middleware provides an API endpoint at /healthcheck path
which allows load balancers or monitoring applications to determine
a service is up using HTTP requests.

This change basically follows the same change merged in ironic[1] repo.

[1] 6f439414bdcef9fc02f844f475ec798d48d42558

Change-Id: Ic2ee2bca74ea2a5a0723ef54b10c531f77ea7b8d
---
 ironic_inspector/conf/__init__.py             |  2 ++
 ironic_inspector/conf/healthcheck.py          | 33 +++++++++++++++++++
 ironic_inspector/conf/opts.py                 |  1 +
 ironic_inspector/main.py                      |  2 ++
 ironic_inspector/utils.py                     |  9 +++++
 ...althcheck-middleware-5994e8a8b54dbdb4.yaml |  7 ++++
 tools/config-generator.conf                   |  1 +
 7 files changed, 55 insertions(+)
 create mode 100644 ironic_inspector/conf/healthcheck.py
 create mode 100644 releasenotes/notes/healthcheck-middleware-5994e8a8b54dbdb4.yaml

diff --git a/ironic_inspector/conf/__init__.py b/ironic_inspector/conf/__init__.py
index 4592fa011..466b6e92e 100644
--- a/ironic_inspector/conf/__init__.py
+++ b/ironic_inspector/conf/__init__.py
@@ -19,6 +19,7 @@ from ironic_inspector.conf import default
 from ironic_inspector.conf import discovery
 from ironic_inspector.conf import dnsmasq_pxe_filter
 from ironic_inspector.conf import extra_hardware
+from ironic_inspector.conf import healthcheck
 from ironic_inspector.conf import iptables
 from ironic_inspector.conf import ironic
 from ironic_inspector.conf import pci_devices
@@ -39,6 +40,7 @@ discovery.register_opts(CONF)
 default.register_opts(CONF)
 dnsmasq_pxe_filter.register_opts(CONF)
 extra_hardware.register_opts(CONF)
+healthcheck.register_opts(CONF)
 iptables.register_opts(CONF)
 ironic.register_opts(CONF)
 pci_devices.register_opts(CONF)
diff --git a/ironic_inspector/conf/healthcheck.py b/ironic_inspector/conf/healthcheck.py
new file mode 100644
index 000000000..c9fcbe217
--- /dev/null
+++ b/ironic_inspector/conf/healthcheck.py
@@ -0,0 +1,33 @@
+#    Licensed under the Apache License, Version 2.0 (the "License"); you may
+#    not use this file except in compliance with the License. You may obtain
+#    a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+#    Unless required by applicable law or agreed to in writing, software
+#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+#    License for the specific language governing permissions and limitations
+#    under the License.
+
+from oslo_config import cfg
+
+from ironic_inspector.common.i18n import _
+
+_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.')),
+]
+
+
+def register_opts(conf):
+    conf.register_opts(_OPTS, group='healthcheck')
+
+
+def list_opts():
+    return _OPTS
diff --git a/ironic_inspector/conf/opts.py b/ironic_inspector/conf/opts.py
index dcf7f31ea..3a74610b3 100644
--- a/ironic_inspector/conf/opts.py
+++ b/ironic_inspector/conf/opts.py
@@ -69,6 +69,7 @@ def list_opts():
         ('dnsmasq_pxe_filter',
          ironic_inspector.conf.dnsmasq_pxe_filter.list_opts()),
         ('extra_hardware', ironic_inspector.conf.extra_hardware.list_opts()),
+        ('healthcheck', ironic_inspector.conf.healthcheck.list_opts()),
         ('ironic', ironic_inspector.conf.ironic.list_opts()),
         ('iptables', ironic_inspector.conf.iptables.list_opts()),
         ('port_physnet', ironic_inspector.conf.port_physnet.list_opts()),
diff --git a/ironic_inspector/main.py b/ironic_inspector/main.py
index fa137d00c..b541422b5 100644
--- a/ironic_inspector/main.py
+++ b/ironic_inspector/main.py
@@ -70,6 +70,8 @@ def _init_middleware():
     else:
         LOG.warning('Starting unauthenticated, please check'
                     ' configuration')
+    if CONF.healthcheck.enabled:
+        utils.add_healthcheck_middleware(_app)
     utils.add_cors_middleware(_app)
 
 
diff --git a/ironic_inspector/utils.py b/ironic_inspector/utils.py
index c03940290..764506945 100644
--- a/ironic_inspector/utils.py
+++ b/ironic_inspector/utils.py
@@ -22,6 +22,7 @@ from openstack.baremetal.v1 import node
 from oslo_config import cfg
 from oslo_log import log
 from oslo_middleware import cors as cors_middleware
+from oslo_middleware import healthcheck as healthcheck_middleware
 import pytz
 import webob
 
@@ -246,6 +247,14 @@ def add_cors_middleware(app):
     app.wsgi_app = cors_middleware.CORS(app.wsgi_app, CONF)
 
 
+def add_healthcheck_middleware(app):
+    """Add healthcheck middleware
+
+    :param app: application
+    """
+    app.wsgi_app = healthcheck_middleware.Healthcheck(app.wsgi_app, CONF)
+
+
 def check_auth(request, rule=None, target=None):
     """Check authentication on request.
 
diff --git a/releasenotes/notes/healthcheck-middleware-5994e8a8b54dbdb4.yaml b/releasenotes/notes/healthcheck-middleware-5994e8a8b54dbdb4.yaml
new file mode 100644
index 000000000..c62730d6a
--- /dev/null
+++ b/releasenotes/notes/healthcheck-middleware-5994e8a8b54dbdb4.yaml
@@ -0,0 +1,7 @@
+---
+features:
+  - |
+    The new ``[healthcheck] enabled`` option has been added. When this option
+    is set to ``True``, the healthcheck middleware is enabled in API pipeline
+    and the additional API endpoint to monitor service availability becomes
+    available at ``/healthcheck`` path.
diff --git a/tools/config-generator.conf b/tools/config-generator.conf
index 2e7adfce2..8a0f18aac 100644
--- a/tools/config-generator.conf
+++ b/tools/config-generator.conf
@@ -7,6 +7,7 @@ namespace = oslo.db
 namespace = oslo.log
 namespace = oslo.messaging
 namespace = oslo.middleware.cors
+namespace = oslo.middleware.healthcheck
 namespace = oslo.policy
 namespace = oslo.service.service
 namespace = oslo.service.sslutils