blob: a1c45e9c11aa05b66ece58f60d675415ef6f1078 [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
27const helmDeps =
28 [
29 'dcae-cloudify-manager'
30 ];
31
Jack Lucas2832ba22018-04-20 13:22:05 +000032const status = require('./get-status');
33const http = require('http');
34
Jack Lucase6d18572018-05-09 22:44:31 +000035// Helm deployments are always in the ONAP namespace and prefixed by Helm release name
36const helmList = helmDeps.map(function(name) {
37 return {namespace: ONAP_NS, deployment: HELM_REL.length > 0 ? HELM_REL + '-' + name : name};
38});
39
Jack Lucas2832ba22018-04-20 13:22:05 +000040const isHealthy = function(summary) {
41 // Current healthiness criterion is simple--all deployments are ready
42 return summary.count && summary.ready && summary.count === summary.ready;
43};
44
45const checkHealth = function (callback) {
46 // Makes queries to Kubernetes and checks results
47 // If we encounter some kind of error contacting k8s (or other), health status is UNKNOWN (500)
48 // If we get responses from k8s but don't find all deployments ready, health status is UNHEALTHY (503)
49 // If we get responses from k8s and all deployments are ready, health status is HEALTHY (200)
50 // 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 -050051
52 // Query k8s to find all the deployments launched by CM (they all have a 'cfydeployment' label)
53 status.getDCAEDeploymentsPromise(DCAE_NS)
54 .then(function(dcaeList) {
55 // Now get status for Helm deployments and CM deployments
56 return status.getStatusListPromise(helmList.concat(dcaeList));
57 })
Jack Lucase6d18572018-05-09 22:44:31 +000058 .then(function(body) {
59 callback({status: isHealthy(body) ? HEALTHY : UNHEALTHY, body: body});
60 })
61 .catch(function(error){
62 callback({status: UNKNOWN, body: [error]})
Jack Lucas2832ba22018-04-20 13:22:05 +000063 });
64};
65
66// Simple HTTP server--any incoming request triggers a health check
67const server = http.createServer(function(req, res) {
68 checkHealth(function(ret) {
69 console.log ((new Date()).toISOString() + ": " + JSON.stringify(ret));
70 res.statusCode = ret.status;
71 res.setHeader('Content-Type', 'application/json');
72 res.end(JSON.stringify(ret.body || {}), 'utf8');
73 });
74});
75server.listen(80);