From e60d88b5821a75754b60fd3b004da08f221a3fc6 Mon Sep 17 00:00:00 2001 From: Jack Lucas Date: Wed, 12 Dec 2018 16:48:41 -0500 Subject: Add healthcheck for after-boot deployments Update node to v 10.14.1 Remove dependency on 'request' package Issue-ID: DCAEGEN2-988 Change-Id: Icc68f8271d22b5ffbdac124e109c0273c05682c2 Signed-off-by: Jack Lucas --- healthcheck-container/Dockerfile | 20 +++++++++++- healthcheck-container/get-status.js | 62 +++++++++++++++++++++++++++--------- healthcheck-container/healthcheck.js | 33 +++++-------------- healthcheck-container/package.json | 5 +-- healthcheck-container/pom.xml | 2 +- 5 files changed, 76 insertions(+), 46 deletions(-) (limited to 'healthcheck-container') diff --git a/healthcheck-container/Dockerfile b/healthcheck-container/Dockerfile index d1b4231..10a6488 100644 --- a/healthcheck-container/Dockerfile +++ b/healthcheck-container/Dockerfile @@ -1,4 +1,22 @@ -FROM node:8.11.1 +# ============LICENSE_START======================================================= +# org.onap.dcae +# ================================================================================ +# Copyright (c) 2018 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. +# ============LICENSE_END========================================================= +# +FROM node:10.14.1 RUN mkdir -p /opt/app COPY *.js /opt/app/ COPY package.json /opt/app/ diff --git a/healthcheck-container/get-status.js b/healthcheck-container/get-status.js index 034ff9d..9c4a723 100644 --- a/healthcheck-container/get-status.js +++ b/healthcheck-container/get-status.js @@ -19,12 +19,15 @@ See the License for the specific language governing permissions and limitations */ const fs = require('fs'); -const request = require('request'); +const https = require('https'); const K8S_CREDS = '/var/run/secrets/kubernetes.io/serviceaccount'; -const K8S_API = 'https://kubernetes.default.svc.cluster.local/'; // Full name to match cert for TLS +const K8S_HOST = 'kubernetes.default.svc.cluster.local'; // Full name to match cert for TLS const K8S_PATH = 'apis/apps/v1beta2/namespaces/'; +const CFY_LABEL = 'cfydeployment'; // All k8s deployments created by Cloudify--and only k8s deployments created by Cloudify--have this label +const MAX_DEPS = 1000; // Maximum number of k8s deployments to return from a query to k8s + //Get token and CA cert const ca = fs.readFileSync(K8S_CREDS + '/ca.crt'); const token = fs.readFileSync(K8S_CREDS + '/token'); @@ -65,24 +68,31 @@ const summarizeDeployment = function(deployment) { }; const queryKubernetes = function(path, callback) { - // Make request to Kubernetes - + // Make GET request to Kubernetes API const options = { - url: K8S_API + path, + host: K8S_HOST, + path: "/" + path, ca : ca, headers: { Authorization: 'bearer ' + token - }, - json: true - }; - console.log ("request url: " + options.url); - request(options, function(error, res, body) { - console.log ("status: " + (res && res.statusCode) ? res.statusCode : "NONE"); - if (error) { - console.log("error: " + error); } - callback(error, res, body); + }; + console.log ("request url: " + options.host + options.path); + const req = https.get(options, function(resp) { + let rawBody = ""; + resp.on("data", function(data) { + rawBody += data; + }); + resp.on("error", function (error) { + console.error("error: " + error); + callback(error, null, null) + }); + resp.on("end", function() { + console.log ("status: " + resp.statusCode ? resp.statusCode: "NONE") + callback(null, resp, JSON.parse(rawBody)); + }); }); + req.end(); }; const getStatus = function(path, extract, callback) { @@ -138,4 +148,26 @@ exports.getStatusListPromise = function (list) { return p.then(function(results) { return summarizeDeploymentList({items: results}); }); -} \ No newline at end of file +} + +exports.getDCAEDeploymentsPromise = function (namespace) { + // Return list of the form [{namespace: "namespace"}, deployment: "deployment_name"]. + // List contains all k8s deployments in the specified namespace that were deployed + // by Cloudify, based on Cloudify's use of a "marker" label on each k8s deployment that + // the k8s plugin created. + + return new Promise(function(resolve, reject) { + const path = K8S_PATH + namespace + '/deployments?labelSelector=' + CFY_LABEL + '&limit=' + MAX_DEPS + queryKubernetes(path, function(error, res, body){ + if (error) { + reject(error); + } + else if (res.statusCode !== 200) { + reject(body); + } + else { + resolve(body.items.map(function(i) {return {namespace : namespace, deployment: i.metadata.name};})); + } + }); + }); +}; diff --git a/healthcheck-container/healthcheck.js b/healthcheck-container/healthcheck.js index 159de1f..a1c45e9 100644 --- a/healthcheck-container/healthcheck.js +++ b/healthcheck-container/healthcheck.js @@ -14,8 +14,7 @@ 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 -// +// 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 || ''; @@ -30,21 +29,6 @@ const helmDeps = 'dcae-cloudify-manager' ]; -// List of deployments expected to be created via Cloudify Manager -const dcaeDeps = - [ - 'dep-config-binding-service', - 'dep-deployment-handler', - 'dep-inventory', - 'dep-service-change-handler', - 'dep-policy-handler', - 'dep-dcae-ves-collector', - 'dep-dcae-tca-analytics', - 'dep-dcae-prh', - 'dep-dcae-hv-ves-collector', - 'dep-dcae-datafile-collector' - ]; - const status = require('./get-status'); const http = require('http'); @@ -53,12 +37,6 @@ const helmList = helmDeps.map(function(name) { return {namespace: ONAP_NS, deployment: HELM_REL.length > 0 ? HELM_REL + '-' + name : name}; }); -// DCAE deployments via CM don't have a release prefix and are in the DCAE namespace, -// which can be the same as the ONAP namespace -const dcaeList = dcaeDeps.map(function(name) { - return {namespace: DCAE_NS, deployment: name}; -}); - const isHealthy = function(summary) { // Current healthiness criterion is simple--all deployments are ready return summary.count && summary.ready && summary.count === summary.ready; @@ -70,8 +48,13 @@ const checkHealth = function (callback) { // 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 - - status.getStatusListPromise(helmList.concat(dcaeList)) + + // Query k8s to find all the deployments launched by CM (they all have a 'cfydeployment' label) + status.getDCAEDeploymentsPromise(DCAE_NS) + .then(function(dcaeList) { + // Now get status for Helm deployments and CM deployments + return status.getStatusListPromise(helmList.concat(dcaeList)); + }) .then(function(body) { callback({status: isHealthy(body) ? HEALTHY : UNHEALTHY, body: body}); }) diff --git a/healthcheck-container/package.json b/healthcheck-container/package.json index cce714b..125bc24 100644 --- a/healthcheck-container/package.json +++ b/healthcheck-container/package.json @@ -1,11 +1,8 @@ { "name": "k8s-healthcheck", "description": "DCAE healthcheck server", - "version": "1.1.2", + "version": "1.2.0", "main": "healthcheck.js", - "dependencies": { - "request": "2.85.0" - }, "author": "author", "license": "(Apache-2.0)" } diff --git a/healthcheck-container/pom.xml b/healthcheck-container/pom.xml index 43a2535..a0141a0 100644 --- a/healthcheck-container/pom.xml +++ b/healthcheck-container/pom.xml @@ -27,7 +27,7 @@ limitations under the License. org.onap.dcaegen2.deployments healthcheck-container dcaegen2-deployments-healthcheck-container - 1.1.2 + 1.2.0 http://maven.apache.org UTF-8 -- cgit 1.2.3-korg