| /* |
| Copyright(c) 2018-2020 AT&T Intellectual Property. All rights reserved. |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and limitations under the License. |
| */ |
| |
| // Expect ONAP and DCAE namespaces and Helm "release" name to be passed via environment variables |
| const ONAP_NS = process.env.ONAP_NAMESPACE || 'default'; |
| const DCAE_NS = process.env.DCAE_NAMESPACE || process.env.ONAP_NAMESPACE || 'default'; |
| const HELM_REL = process.env.HELM_RELEASE || ''; |
| |
| // If the healthcheck should include k8s deployments that are marked with a specific label, |
| // the DEPLOY_LABEL environment variable will be set to the name of the label. |
| // Note that the only the name of label is important--the value isn't used by the |
| // the healthcheck. If a k8s deployment has the label, it is included in the check. |
| // For DCAE (dcaegen2), this capability is used to check for k8s deployments that are |
| // created by Cloudify using the k8s plugin. |
| const DEPLOY_LABEL = process.env.DEPLOY_LABEL || ''; |
| |
| const HEALTHY = 200; |
| const UNHEALTHY = 500; |
| const UNKNOWN = 503; |
| |
| const EXPECTED_COMPONENTS = '/opt/app/expected-components.json' |
| const LISTEN_PORT = 8080; |
| |
| const fs = require('fs'); |
| const log = require('./log') |
| |
| // List of deployments expected to be created via Helm |
| let helmDeps = []; |
| try { |
| helmDeps = JSON.parse(fs.readFileSync(EXPECTED_COMPONENTS, {encoding: 'utf8'})); |
| } |
| catch (error) { |
| log.error(`Could not access ${EXPECTED_COMPONENTS}: ${error}`); |
| log.error ('Using empty list of expected components'); |
| } |
| |
| const status = require('./get-status'); |
| const http = require('http'); |
| |
| // Helm deployments are always in the ONAP namespace and prefixed by Helm release name |
| const helmList = helmDeps.map(function(name) { |
| return {namespace: ONAP_NS, deployment: HELM_REL.length > 0 ? HELM_REL + '-' + name : name}; |
| }); |
| |
| const isHealthy = function(summary) { |
| // Current healthiness criterion is simple--all deployments are ready |
| return summary.hasOwnProperty('count') && summary.hasOwnProperty('ready') && summary.count === summary.ready; |
| }; |
| |
| const checkHealth = function (callback) { |
| // Makes queries to Kubernetes and checks results |
| // If we encounter some kind of error contacting k8s (or other), health status is UNKNOWN (500) |
| // If we get responses from k8s but don't find all deployments ready, health status is UNHEALTHY (503) |
| // If we get responses from k8s and all deployments are ready, health status is HEALTHY (200) |
| // This could be a lot more nuanced, but what's here should be sufficient for R2 OOM healthchecking |
| |
| // Query k8s to find all the deployments with specified DEPLOY_LABEL |
| status.getLabeledDeploymentsPromise(DCAE_NS, DEPLOY_LABEL) |
| .then(function(fullDCAEList) { |
| // Now get status for Helm deployments and CM deployments |
| return status.getStatusListPromise(helmList.concat(fullDCAEList)); |
| }) |
| .then(function(body) { |
| callback({status: isHealthy(body) ? HEALTHY : UNHEALTHY, body: body}); |
| }) |
| .catch(function(error){ |
| callback({status: UNKNOWN, body: [error]}) |
| }); |
| }; |
| |
| // Simple HTTP server--any incoming request triggers a health check |
| const server = http.createServer(function(req, res) { |
| checkHealth(function(ret) { |
| log.info(`Incoming request: ${req.url} -- response: ${JSON.stringify(ret)}`); |
| res.statusCode = ret.status; |
| res.setHeader('Content-Type', 'application/json'); |
| res.end(JSON.stringify(ret.body || {}), 'utf8'); |
| }); |
| }); |
| server.listen(LISTEN_PORT); |
| log.info(`Listening on port ${LISTEN_PORT} -- expected components: ${JSON.stringify(helmDeps)}`); |