blob: ca1df8441e8f7f6d61832fcef642a2f393987c51 [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
17//Expect ONAP and DCAE namespaces and Helm "release" name to be passed via environment variables
18//
19const ONAP_NS = process.env.ONAP_NAMESPACE || 'default';
Jack Lucase6d18572018-05-09 22:44:31 +000020const DCAE_NS = process.env.DCAE_NAMESPACE || process.env.ONAP_NAMESPACE || 'default';
Jack Lucas2832ba22018-04-20 13:22:05 +000021const HELM_REL = process.env.HELM_RELEASE || '';
22
23const HEALTHY = 200;
24const UNHEALTHY = 500;
25const UNKNOWN = 503;
26
Jack Lucase6d18572018-05-09 22:44:31 +000027// List of deployments expected to be created via Helm
28const helmDeps =
29 [
30 'dcae-cloudify-manager'
31 ];
32
33// List of deployments expected to be created via Cloudify Manager
34const dcaeDeps =
35 [
36 'dep-config-binding-service',
37 'dep-deployment-handler',
38 'dep-inventory',
39 'dep-service-change-handler',
40 'dep-policy-handler',
41 'dep-dcae-ves-collector',
42 'dep-dcae-tca-analytics'
43 ];
44
Jack Lucas2832ba22018-04-20 13:22:05 +000045const status = require('./get-status');
46const http = require('http');
47
Jack Lucase6d18572018-05-09 22:44:31 +000048// Helm deployments are always in the ONAP namespace and prefixed by Helm release name
49const helmList = helmDeps.map(function(name) {
50 return {namespace: ONAP_NS, deployment: HELM_REL.length > 0 ? HELM_REL + '-' + name : name};
51});
52
53// DCAE deployments via CM don't have a release prefix and are in the DCAE namespace,
54// which can be the same as the ONAP namespace
55const dcaeList = dcaeDeps.map(function(name) {
56 return {namespace: DCAE_NS, deployment: name};
57});
58
Jack Lucas2832ba22018-04-20 13:22:05 +000059const isHealthy = function(summary) {
60 // Current healthiness criterion is simple--all deployments are ready
61 return summary.count && summary.ready && summary.count === summary.ready;
62};
63
64const checkHealth = function (callback) {
65 // Makes queries to Kubernetes and checks results
66 // If we encounter some kind of error contacting k8s (or other), health status is UNKNOWN (500)
67 // If we get responses from k8s but don't find all deployments ready, health status is UNHEALTHY (503)
68 // If we get responses from k8s and all deployments are ready, health status is HEALTHY (200)
69 // This could be a lot more nuanced, but what's here should be sufficient for R2 OOM healthchecking
Jack Lucase6d18572018-05-09 22:44:31 +000070
71 status.getStatusListPromise(helmList.concat(dcaeList))
72 .then(function(body) {
73 callback({status: isHealthy(body) ? HEALTHY : UNHEALTHY, body: body});
74 })
75 .catch(function(error){
76 callback({status: UNKNOWN, body: [error]})
Jack Lucas2832ba22018-04-20 13:22:05 +000077 });
78};
79
80// Simple HTTP server--any incoming request triggers a health check
81const server = http.createServer(function(req, res) {
82 checkHealth(function(ret) {
83 console.log ((new Date()).toISOString() + ": " + JSON.stringify(ret));
84 res.statusCode = ret.status;
85 res.setHeader('Content-Type', 'application/json');
86 res.end(JSON.stringify(ret.body || {}), 'utf8');
87 });
88});
89server.listen(80);