blob: 7a03f1ede4a13cd76d1db2717b767e60d4c815a5 [file] [log] [blame]
Jack Lucas2832ba22018-04-20 13:22:05 +00001/*
2Copyright(c) 2018 AT&T Intellectual Property. All rights reserved.
3
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 [
29 'dcae-cloudify-manager'
30 ];
31
32// List of deployments expected to be created by CM at boot time
33const bootDeps =
34 [
35 'dep-config-binding-service',
36 'dep-deployment-handler',
37 'dep-inventory',
38 'dep-service-change-handler',
39 'dep-policy-handler',
40 'dep-dcae-ves-collector',
41 'dep-dcae-tca-analytics',
42 'dep-dcae-prh',
43 'dep-dcae-hv-ves-collector',
44 'dep-dcae-datafile-collector'
45 ];
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);