Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 1 | /* |
Jack Lucas | 40ee89a | 2020-02-11 11:50:07 -0500 | [diff] [blame] | 2 | Copyright(c) 2018-2020 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 | |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 22 | // If the healthcheck should include k8s deployments that are marked with a specific label, |
| 23 | // the DEPLOY_LABEL environment variable will be set to the name of the label. |
| 24 | // Note that the only the name of label is important--the value isn't used by the |
| 25 | // the healthcheck. If a k8s deployment has the label, it is included in the check. |
| 26 | // For DCAE (dcaegen2), this capability is used to check for k8s deployments that are |
| 27 | // created by Cloudify using the k8s plugin. |
| 28 | const DEPLOY_LABEL = process.env.DEPLOY_LABEL || ''; |
| 29 | |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 30 | const HEALTHY = 200; |
| 31 | const UNHEALTHY = 500; |
| 32 | const UNKNOWN = 503; |
| 33 | |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 34 | const EXPECTED_COMPONENTS='/opt/app/expected-components.json' |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 35 | |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 36 | const fs = require('fs'); |
| 37 | |
| 38 | // List of deployments expected to be created via Helm |
| 39 | let helmDeps = []; |
| 40 | try { |
| 41 | helmDeps = JSON.parse(fs.readFileSync(EXPECTED_COMPONENTS, {encoding: 'utf8'})); |
| 42 | } |
| 43 | catch (error) { |
| 44 | console.log(`Could not access ${EXPECTED_COMPONENTS}: ${error}`); |
| 45 | console.log ('Using empty list of expected components'); |
| 46 | } |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 47 | |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 48 | const status = require('./get-status'); |
| 49 | const http = require('http'); |
| 50 | |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 51 | // Helm deployments are always in the ONAP namespace and prefixed by Helm release name |
| 52 | const helmList = helmDeps.map(function(name) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 53 | 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] | 54 | }); |
| 55 | |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 56 | const isHealthy = function(summary) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 57 | // Current healthiness criterion is simple--all deployments are ready |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 58 | return summary.hasOwnProperty('count') && summary.hasOwnProperty('ready') && summary.count === summary.ready; |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | const checkHealth = function (callback) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 62 | // Makes queries to Kubernetes and checks results |
| 63 | // If we encounter some kind of error contacting k8s (or other), health status is UNKNOWN (500) |
| 64 | // If we get responses from k8s but don't find all deployments ready, health status is UNHEALTHY (503) |
| 65 | // If we get responses from k8s and all deployments are ready, health status is HEALTHY (200) |
| 66 | // 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] | 67 | |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 68 | // Query k8s to find all the deployments with specified DEPLOY_LABEL |
| 69 | status.getLabeledDeploymentsPromise(DCAE_NS, DEPLOY_LABEL) |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 70 | .then(function(fullDCAEList) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 71 | // Now get status for Helm deployments and CM deployments |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 72 | return status.getStatusListPromise(helmList.concat(fullDCAEList)); |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 73 | }) |
| 74 | .then(function(body) { |
| 75 | callback({status: isHealthy(body) ? HEALTHY : UNHEALTHY, body: body}); |
| 76 | }) |
| 77 | .catch(function(error){ |
| 78 | callback({status: UNKNOWN, body: [error]}) |
| 79 | }); |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | // Simple HTTP server--any incoming request triggers a health check |
| 83 | const server = http.createServer(function(req, res) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 84 | checkHealth(function(ret) { |
| 85 | console.log ((new Date()).toISOString() + ": " + JSON.stringify(ret)); |
| 86 | res.statusCode = ret.status; |
| 87 | res.setHeader('Content-Type', 'application/json'); |
| 88 | res.end(JSON.stringify(ret.body || {}), 'utf8'); |
| 89 | }); |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 90 | }); |
Jack Lucas | 40ee89a | 2020-02-11 11:50:07 -0500 | [diff] [blame] | 91 | server.listen(8080); |