blob: 9197e4d91a759faf63c77388f1a64f9f61f8699f [file] [log] [blame]
Jack Lucas2832ba22018-04-20 13:22:05 +00001/*
Jack Lucas40ee89a2020-02-11 11:50:07 -05002Copyright(c) 2018-2020 AT&T Intellectual Property. All rights reserved.
Jack Lucas2832ba22018-04-20 13:22:05 +00003
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing,
12software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
13CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and limitations under the License.
15*/
16
Jack Lucase60d88b2018-12-12 16:48:41 -050017// Expect ONAP and DCAE namespaces and Helm "release" name to be passed via environment variables
Jack Lucas2832ba22018-04-20 13:22:05 +000018const ONAP_NS = process.env.ONAP_NAMESPACE || 'default';
Jack Lucase6d18572018-05-09 22:44:31 +000019const DCAE_NS = process.env.DCAE_NAMESPACE || process.env.ONAP_NAMESPACE || 'default';
Jack Lucas2832ba22018-04-20 13:22:05 +000020const HELM_REL = process.env.HELM_RELEASE || '';
21
Jack Lucasef3183f2020-06-12 11:44:49 -040022// If the healthcheck should include k8s deployments that are marked with a specific label,
23// the DEPLOY_LABEL environment variable will be set to the name of the label.
24// Note that the only the name of label is important--the value isn't used by the
25// the healthcheck. If a k8s deployment has the label, it is included in the check.
26// For DCAE (dcaegen2), this capability is used to check for k8s deployments that are
27// created by Cloudify using the k8s plugin.
28const DEPLOY_LABEL = process.env.DEPLOY_LABEL || '';
29
Jack Lucas2832ba22018-04-20 13:22:05 +000030const HEALTHY = 200;
31const UNHEALTHY = 500;
32const UNKNOWN = 503;
33
Jack Lucasc4411662021-10-28 14:55:54 -040034const EXPECTED_COMPONENTS = '/opt/app/expected-components.json'
35const LISTEN_PORT = 8080;
Jack Lucas1d746682018-12-13 17:24:29 -050036
Jack Lucasef3183f2020-06-12 11:44:49 -040037const fs = require('fs');
Jack Lucasc4411662021-10-28 14:55:54 -040038const log = require('./log')
Jack Lucasef3183f2020-06-12 11:44:49 -040039
40// List of deployments expected to be created via Helm
41let helmDeps = [];
42try {
43 helmDeps = JSON.parse(fs.readFileSync(EXPECTED_COMPONENTS, {encoding: 'utf8'}));
44}
45catch (error) {
Jack Lucasc4411662021-10-28 14:55:54 -040046 log.error(`Could not access ${EXPECTED_COMPONENTS}: ${error}`);
47 log.error ('Using empty list of expected components');
Jack Lucasef3183f2020-06-12 11:44:49 -040048}
Jack Lucase6d18572018-05-09 22:44:31 +000049
Jack Lucas2832ba22018-04-20 13:22:05 +000050const status = require('./get-status');
51const http = require('http');
52
Jack Lucase6d18572018-05-09 22:44:31 +000053// Helm deployments are always in the ONAP namespace and prefixed by Helm release name
54const helmList = helmDeps.map(function(name) {
Jack Lucas1d746682018-12-13 17:24:29 -050055 return {namespace: ONAP_NS, deployment: HELM_REL.length > 0 ? HELM_REL + '-' + name : name};
Jack Lucase6d18572018-05-09 22:44:31 +000056});
57
Jack Lucas2832ba22018-04-20 13:22:05 +000058const isHealthy = function(summary) {
Jack Lucas1d746682018-12-13 17:24:29 -050059 // Current healthiness criterion is simple--all deployments are ready
Jack Lucasef3183f2020-06-12 11:44:49 -040060 return summary.hasOwnProperty('count') && summary.hasOwnProperty('ready') && summary.count === summary.ready;
Jack Lucas2832ba22018-04-20 13:22:05 +000061};
62
63const checkHealth = function (callback) {
Jack Lucas1d746682018-12-13 17:24:29 -050064 // Makes queries to Kubernetes and checks results
65 // If we encounter some kind of error contacting k8s (or other), health status is UNKNOWN (500)
66 // If we get responses from k8s but don't find all deployments ready, health status is UNHEALTHY (503)
67 // If we get responses from k8s and all deployments are ready, health status is HEALTHY (200)
68 // This could be a lot more nuanced, but what's here should be sufficient for R2 OOM healthchecking
Jack Lucase60d88b2018-12-12 16:48:41 -050069
Jack Lucasef3183f2020-06-12 11:44:49 -040070 // Query k8s to find all the deployments with specified DEPLOY_LABEL
71 status.getLabeledDeploymentsPromise(DCAE_NS, DEPLOY_LABEL)
Jack Lucas1d746682018-12-13 17:24:29 -050072 .then(function(fullDCAEList) {
Jack Lucas1d746682018-12-13 17:24:29 -050073 // Now get status for Helm deployments and CM deployments
Jack Lucasef3183f2020-06-12 11:44:49 -040074 return status.getStatusListPromise(helmList.concat(fullDCAEList));
Jack Lucas1d746682018-12-13 17:24:29 -050075 })
76 .then(function(body) {
77 callback({status: isHealthy(body) ? HEALTHY : UNHEALTHY, body: body});
78 })
79 .catch(function(error){
80 callback({status: UNKNOWN, body: [error]})
81 });
Jack Lucas2832ba22018-04-20 13:22:05 +000082};
83
84// Simple HTTP server--any incoming request triggers a health check
85const server = http.createServer(function(req, res) {
Jack Lucas1d746682018-12-13 17:24:29 -050086 checkHealth(function(ret) {
Jack Lucasc4411662021-10-28 14:55:54 -040087 log.info(`Incoming request: ${req.url} -- response: ${JSON.stringify(ret)}`);
Jack Lucas1d746682018-12-13 17:24:29 -050088 res.statusCode = ret.status;
89 res.setHeader('Content-Type', 'application/json');
90 res.end(JSON.stringify(ret.body || {}), 'utf8');
91 });
Jack Lucas2832ba22018-04-20 13:22:05 +000092});
Jack Lucasc4411662021-10-28 14:55:54 -040093server.listen(LISTEN_PORT);
94log.info(`Listening on port ${LISTEN_PORT} -- expected components: ${JSON.stringify(helmDeps)}`);