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 | c441166 | 2021-10-28 14:55:54 -0400 | [diff] [blame] | 34 | const EXPECTED_COMPONENTS = '/opt/app/expected-components.json' |
| 35 | const LISTEN_PORT = 8080; |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 36 | |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 37 | const fs = require('fs'); |
Jack Lucas | c441166 | 2021-10-28 14:55:54 -0400 | [diff] [blame] | 38 | const log = require('./log') |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 39 | |
| 40 | // List of deployments expected to be created via Helm |
| 41 | let helmDeps = []; |
| 42 | try { |
| 43 | helmDeps = JSON.parse(fs.readFileSync(EXPECTED_COMPONENTS, {encoding: 'utf8'})); |
| 44 | } |
| 45 | catch (error) { |
Jack Lucas | c441166 | 2021-10-28 14:55:54 -0400 | [diff] [blame] | 46 | log.error(`Could not access ${EXPECTED_COMPONENTS}: ${error}`); |
| 47 | log.error ('Using empty list of expected components'); |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 48 | } |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 49 | |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 50 | const status = require('./get-status'); |
| 51 | const http = require('http'); |
| 52 | |
Jack Lucas | e6d1857 | 2018-05-09 22:44:31 +0000 | [diff] [blame] | 53 | // Helm deployments are always in the ONAP namespace and prefixed by Helm release name |
| 54 | const helmList = helmDeps.map(function(name) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 55 | 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] | 56 | }); |
| 57 | |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 58 | const isHealthy = function(summary) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 59 | // Current healthiness criterion is simple--all deployments are ready |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 60 | return summary.hasOwnProperty('count') && summary.hasOwnProperty('ready') && summary.count === summary.ready; |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | const checkHealth = function (callback) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 64 | // Makes queries to Kubernetes and checks results |
| 65 | // If we encounter some kind of error contacting k8s (or other), health status is UNKNOWN (500) |
| 66 | // If we get responses from k8s but don't find all deployments ready, health status is UNHEALTHY (503) |
| 67 | // If we get responses from k8s and all deployments are ready, health status is HEALTHY (200) |
| 68 | // 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] | 69 | |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 70 | // Query k8s to find all the deployments with specified DEPLOY_LABEL |
| 71 | status.getLabeledDeploymentsPromise(DCAE_NS, DEPLOY_LABEL) |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 72 | .then(function(fullDCAEList) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 73 | // Now get status for Helm deployments and CM deployments |
Jack Lucas | ef3183f | 2020-06-12 11:44:49 -0400 | [diff] [blame] | 74 | return status.getStatusListPromise(helmList.concat(fullDCAEList)); |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 75 | }) |
| 76 | .then(function(body) { |
| 77 | callback({status: isHealthy(body) ? HEALTHY : UNHEALTHY, body: body}); |
| 78 | }) |
| 79 | .catch(function(error){ |
| 80 | callback({status: UNKNOWN, body: [error]}) |
| 81 | }); |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | // Simple HTTP server--any incoming request triggers a health check |
| 85 | const server = http.createServer(function(req, res) { |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 86 | checkHealth(function(ret) { |
Jack Lucas | c441166 | 2021-10-28 14:55:54 -0400 | [diff] [blame] | 87 | log.info(`Incoming request: ${req.url} -- response: ${JSON.stringify(ret)}`); |
Jack Lucas | 1d74668 | 2018-12-13 17:24:29 -0500 | [diff] [blame] | 88 | res.statusCode = ret.status; |
| 89 | res.setHeader('Content-Type', 'application/json'); |
| 90 | res.end(JSON.stringify(ret.body || {}), 'utf8'); |
| 91 | }); |
Jack Lucas | 2832ba2 | 2018-04-20 13:22:05 +0000 | [diff] [blame] | 92 | }); |
Jack Lucas | c441166 | 2021-10-28 14:55:54 -0400 | [diff] [blame] | 93 | server.listen(LISTEN_PORT); |
| 94 | log.info(`Listening on port ${LISTEN_PORT} -- expected components: ${JSON.stringify(helmDeps)}`); |