blob: 1156ff570b61d4f500fb0b014d1d0e873ad9a1a2 [file] [log] [blame]
Jack Lucas2832ba22018-04-20 13:22:05 +00001/*
Vijay Venkatesh Kumard2c82122019-04-05 00:38:59 +00002Copyright(c) 2018-2019 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
22const HEALTHY = 200;
23const UNHEALTHY = 500;
24const UNKNOWN = 503;
25
Jack Lucase6d18572018-05-09 22:44:31 +000026// List of deployments expected to be created via Helm
Jack Lucas1d746682018-12-13 17:24:29 -050027const helmDeps =
28 [
Jack Lucas0025fda2019-01-17 13:53:56 -050029 'dcae-cloudify-manager',
Jack Lucascddf9bf2019-01-28 10:37:02 -050030 'dcae-config-binding-service',
31 'dcae-inventory-api',
Jack Lucas5caf1652019-02-08 14:21:58 -050032 'dcae-servicechange-handler',
33 'dcae-deployment-handler',
Jack Lucasef4ae302019-07-25 14:30:12 -040034 'dcae-policy-handler',
35 'dcae-dashboard'
Jack Lucas1d746682018-12-13 17:24:29 -050036 ];
37
38// List of deployments expected to be created by CM at boot time
39const bootDeps =
40 [
Jack Lucas1d746682018-12-13 17:24:29 -050041 'dep-dcae-ves-collector',
42 'dep-dcae-tca-analytics',
43 'dep-dcae-prh',
Jack Lucasef4ae302019-07-25 14:30:12 -040044 'dep-dcae-hv-ves-collector'
Jack Lucas1d746682018-12-13 17:24:29 -050045 ];
Jack Lucase6d18572018-05-09 22:44:31 +000046
Jack Lucas2832ba22018-04-20 13:22:05 +000047const status = require('./get-status');
48const http = require('http');
49
Jack Lucase6d18572018-05-09 22:44:31 +000050// Helm deployments are always in the ONAP namespace and prefixed by Helm release name
51const helmList = helmDeps.map(function(name) {
Jack Lucas1d746682018-12-13 17:24:29 -050052 return {namespace: ONAP_NS, deployment: HELM_REL.length > 0 ? HELM_REL + '-' + name : name};
Jack Lucase6d18572018-05-09 22:44:31 +000053});
54
Jack Lucas2832ba22018-04-20 13:22:05 +000055const isHealthy = function(summary) {
Jack Lucas1d746682018-12-13 17:24:29 -050056 // Current healthiness criterion is simple--all deployments are ready
57 return summary.count && summary.ready && summary.count === summary.ready;
Jack Lucas2832ba22018-04-20 13:22:05 +000058};
59
60const checkHealth = function (callback) {
Jack Lucas1d746682018-12-13 17:24:29 -050061 // Makes queries to Kubernetes and checks results
62 // If we encounter some kind of error contacting k8s (or other), health status is UNKNOWN (500)
63 // If we get responses from k8s but don't find all deployments ready, health status is UNHEALTHY (503)
64 // If we get responses from k8s and all deployments are ready, health status is HEALTHY (200)
65 // 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 -050066
Jack Lucas1d746682018-12-13 17:24:29 -050067 // Query k8s to find all the deployments launched by CM (they all have a 'cfydeployment' label)
68 status.getDCAEDeploymentsPromise(DCAE_NS)
69 .then(function(fullDCAEList) {
70 // Remove any expected boot-time CM deployments from the list to avoid duplicates
71 dynamicDCAEDeps = fullDCAEList.filter(function(i) {return !(bootDeps.includes(i.deployment));})
72 // Create full list of CM deployments to check: boot deployments and anything else created by CM
73 dcaeList = (bootDeps.map(function(name){return {namespace: DCAE_NS, deployment: name}})).concat(dynamicDCAEDeps);
74 // Now get status for Helm deployments and CM deployments
75 return status.getStatusListPromise(helmList.concat(dcaeList));
76 })
77 .then(function(body) {
78 callback({status: isHealthy(body) ? HEALTHY : UNHEALTHY, body: body});
79 })
80 .catch(function(error){
81 callback({status: UNKNOWN, body: [error]})
82 });
Jack Lucas2832ba22018-04-20 13:22:05 +000083};
84
85// Simple HTTP server--any incoming request triggers a health check
86const server = http.createServer(function(req, res) {
Jack Lucas1d746682018-12-13 17:24:29 -050087 checkHealth(function(ret) {
88 console.log ((new Date()).toISOString() + ": " + JSON.stringify(ret));
89 res.statusCode = ret.status;
90 res.setHeader('Content-Type', 'application/json');
91 res.end(JSON.stringify(ret.body || {}), 'utf8');
92 });
Jack Lucas2832ba22018-04-20 13:22:05 +000093});
94server.listen(80);