Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright(c) 2018 AT&T Intellectual Property. All rights reserved. |
| 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 |
| 27 | const helmDeps = |
| 28 | [ |
| 29 | 'dcae-cloudify-manager' |
| 30 | ]; |
| 31 | |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 32 | const status = require('./get-status'); |
| 33 | const http = require('http'); |
| 34 | |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 35 | // Helm deployments are always in the ONAP namespace and prefixed by Helm release name |
| 36 | const helmList = helmDeps.map(function(name) { |
| 37 | return {namespace: ONAP_NS, deployment: HELM_REL.length > 0 ? HELM_REL + '-' + name : name}; |
| 38 | }); |
| 39 | |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 40 | const 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 | |
| 45 | const 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 Lucas | e60d88b | 2018-12-12 16:48:41 -0500 | [diff] [blame] | 51 | |
| 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 Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 58 | .then(function(body) { |
| 59 | callback({status: isHealthy(body) ? HEALTHY : UNHEALTHY, body: body}); |
| 60 | }) |
| 61 | .catch(function(error){ |
| 62 | callback({status: UNKNOWN, body: [error]}) |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 63 | }); |
| 64 | }; |
| 65 | |
| 66 | // Simple HTTP server--any incoming request triggers a health check |
| 67 | const 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 | }); |
| 75 | server.listen(80); |