blob: 574859f20e00fa1993b3e5fedfc76afe27a26894 [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 Lucasef3183f2020-06-12 11:44:49 -040034const EXPECTED_COMPONENTS='/opt/app/expected-components.json'
Jack Lucas1d746682018-12-13 17:24:29 -050035
Jack Lucasef3183f2020-06-12 11:44:49 -040036const fs = require('fs');
37
38// List of deployments expected to be created via Helm
39let helmDeps = [];
40try {
41 helmDeps = JSON.parse(fs.readFileSync(EXPECTED_COMPONENTS, {encoding: 'utf8'}));
42}
43catch (error) {
44 console.log(`Could not access ${EXPECTED_COMPONENTS}: ${error}`);
45 console.log ('Using empty list of expected components');
46}
Jack Lucase6d18572018-05-09 22:44:31 +000047
Jack Lucas2832ba22018-04-20 13:22:05 +000048const status = require('./get-status');
49const http = require('http');
50
Jack Lucase6d18572018-05-09 22:44:31 +000051// Helm deployments are always in the ONAP namespace and prefixed by Helm release name
52const helmList = helmDeps.map(function(name) {
Jack Lucas1d746682018-12-13 17:24:29 -050053 return {namespace: ONAP_NS, deployment: HELM_REL.length > 0 ? HELM_REL + '-' + name : name};
Jack Lucase6d18572018-05-09 22:44:31 +000054});
55
Jack Lucas2832ba22018-04-20 13:22:05 +000056const isHealthy = function(summary) {
Jack Lucas1d746682018-12-13 17:24:29 -050057 // Current healthiness criterion is simple--all deployments are ready
Jack Lucasef3183f2020-06-12 11:44:49 -040058 return summary.hasOwnProperty('count') && summary.hasOwnProperty('ready') && summary.count === summary.ready;
Jack Lucas2832ba22018-04-20 13:22:05 +000059};
60
61const checkHealth = function (callback) {
Jack Lucas1d746682018-12-13 17:24:29 -050062 // Makes queries to Kubernetes and checks results
63 // If we encounter some kind of error contacting k8s (or other), health status is UNKNOWN (500)
64 // If we get responses from k8s but don't find all deployments ready, health status is UNHEALTHY (503)
65 // If we get responses from k8s and all deployments are ready, health status is HEALTHY (200)
66 // 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 -050067
Jack Lucasef3183f2020-06-12 11:44:49 -040068 // Query k8s to find all the deployments with specified DEPLOY_LABEL
69 status.getLabeledDeploymentsPromise(DCAE_NS, DEPLOY_LABEL)
Jack Lucas1d746682018-12-13 17:24:29 -050070 .then(function(fullDCAEList) {
Jack Lucas1d746682018-12-13 17:24:29 -050071 // Now get status for Helm deployments and CM deployments
Jack Lucasef3183f2020-06-12 11:44:49 -040072 return status.getStatusListPromise(helmList.concat(fullDCAEList));
Jack Lucas1d746682018-12-13 17:24:29 -050073 })
74 .then(function(body) {
75 callback({status: isHealthy(body) ? HEALTHY : UNHEALTHY, body: body});
76 })
77 .catch(function(error){
78 callback({status: UNKNOWN, body: [error]})
79 });
Jack Lucas2832ba22018-04-20 13:22:05 +000080};
81
82// Simple HTTP server--any incoming request triggers a health check
83const server = http.createServer(function(req, res) {
Jack Lucas1d746682018-12-13 17:24:29 -050084 checkHealth(function(ret) {
85 console.log ((new Date()).toISOString() + ": " + JSON.stringify(ret));
86 res.statusCode = ret.status;
87 res.setHeader('Content-Type', 'application/json');
88 res.end(JSON.stringify(ret.body || {}), 'utf8');
89 });
Jack Lucas2832ba22018-04-20 13:22:05 +000090});
Jack Lucas40ee89a2020-02-11 11:50:07 -050091server.listen(8080);