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 | |
| 17 | //Expect ONAP and DCAE namespaces and Helm "release" name to be passed via environment variables |
| 18 | // |
| 19 | const ONAP_NS = process.env.ONAP_NAMESPACE || 'default'; |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 20 | const DCAE_NS = process.env.DCAE_NAMESPACE || process.env.ONAP_NAMESPACE || 'default'; |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 21 | const HELM_REL = process.env.HELM_RELEASE || ''; |
| 22 | |
| 23 | const HEALTHY = 200; |
| 24 | const UNHEALTHY = 500; |
| 25 | const UNKNOWN = 503; |
| 26 | |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 27 | // List of deployments expected to be created via Helm |
| 28 | const helmDeps = |
| 29 | [ |
| 30 | 'dcae-cloudify-manager' |
| 31 | ]; |
| 32 | |
| 33 | // List of deployments expected to be created via Cloudify Manager |
| 34 | const 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 Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 45 | const status = require('./get-status'); |
| 46 | const http = require('http'); |
| 47 | |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 48 | // Helm deployments are always in the ONAP namespace and prefixed by Helm release name |
| 49 | const 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 |
| 55 | const dcaeList = dcaeDeps.map(function(name) { |
| 56 | return {namespace: DCAE_NS, deployment: name}; |
| 57 | }); |
| 58 | |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 59 | const 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 | |
| 64 | const 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 Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 70 | |
| 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 Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 77 | }); |
| 78 | }; |
| 79 | |
| 80 | // Simple HTTP server--any incoming request triggers a health check |
| 81 | const 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 | }); |
| 89 | server.listen(80); |