Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 1 | /* |
Vijay Venkatesh Kumar | d2c8212 | 2019-04-05 00:38:59 +0000 | [diff] [blame] | 2 | Copyright(c) 2018-2019 AT&T Intellectual Property. All rights reserved. |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, |
| 12 | software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 13 | CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and limitations under the License. |
| 15 | */ |
| 16 | |
Jack Lucas | e60d88b | 2018-12-12 16:48:41 -0500 | [diff] [blame] | 17 | // Expect ONAP and DCAE namespaces and Helm "release" name to be passed via environment variables |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 18 | const ONAP_NS = process.env.ONAP_NAMESPACE || 'default'; |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 19 | const DCAE_NS = process.env.DCAE_NAMESPACE || process.env.ONAP_NAMESPACE || 'default'; |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 20 | const HELM_REL = process.env.HELM_RELEASE || ''; |
| 21 | |
| 22 | const HEALTHY = 200; |
| 23 | const UNHEALTHY = 500; |
| 24 | const UNKNOWN = 503; |
| 25 | |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 26 | // List of deployments expected to be created via Helm |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 27 | const helmDeps = |
| 28 | [ |
Jack Lucas | 0025fda | 2019-01-17 13:53:56 -0500 | [diff] [blame] | 29 | 'dcae-cloudify-manager', |
Jack Lucas | cddf9bf | 2019-01-28 10:37:02 -0500 | [diff] [blame] | 30 | 'dcae-config-binding-service', |
| 31 | 'dcae-inventory-api', |
Jack Lucas | 5caf165 | 2019-02-08 14:21:58 -0500 | [diff] [blame] | 32 | 'dcae-servicechange-handler', |
| 33 | 'dcae-deployment-handler', |
Jack Lucas | ef4ae30 | 2019-07-25 14:30:12 -0400 | [diff] [blame] | 34 | 'dcae-policy-handler', |
| 35 | 'dcae-dashboard' |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 36 | ]; |
| 37 | |
| 38 | // List of deployments expected to be created by CM at boot time |
| 39 | const bootDeps = |
| 40 | [ |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 41 | 'dep-dcae-ves-collector', |
| 42 | 'dep-dcae-tca-analytics', |
| 43 | 'dep-dcae-prh', |
Jack Lucas | ef4ae30 | 2019-07-25 14:30:12 -0400 | [diff] [blame] | 44 | 'dep-dcae-hv-ves-collector' |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 45 | ]; |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 46 | |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 47 | const status = require('./get-status'); |
| 48 | const http = require('http'); |
| 49 | |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 50 | // Helm deployments are always in the ONAP namespace and prefixed by Helm release name |
| 51 | const helmList = helmDeps.map(function(name) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 52 | return {namespace: ONAP_NS, deployment: HELM_REL.length > 0 ? HELM_REL + '-' + name : name}; |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 53 | }); |
| 54 | |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 55 | const isHealthy = function(summary) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 56 | // Current healthiness criterion is simple--all deployments are ready |
| 57 | return summary.count && summary.ready && summary.count === summary.ready; |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | const checkHealth = function (callback) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 61 | // 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 Lucas | e60d88b | 2018-12-12 16:48:41 -0500 | [diff] [blame] | 66 | |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 67 | // 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 Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | // Simple HTTP server--any incoming request triggers a health check |
| 86 | const server = http.createServer(function(req, res) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 87 | 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 Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 93 | }); |
| 94 | server.listen(80); |