Add healthcheck for after-boot deployments 95/74595/2
authorJack Lucas <jflucas@research.att.com>
Wed, 12 Dec 2018 21:48:41 +0000 (16:48 -0500)
committerJack Lucas <jflucas@research.att.com>
Wed, 12 Dec 2018 22:21:29 +0000 (17:21 -0500)
Update node to v 10.14.1
Remove dependency on 'request' package

Issue-ID: DCAEGEN2-988
Change-Id: Icc68f8271d22b5ffbdac124e109c0273c05682c2
Signed-off-by: Jack Lucas <jflucas@research.att.com>
healthcheck-container/Dockerfile
healthcheck-container/get-status.js
healthcheck-container/healthcheck.js
healthcheck-container/package.json
healthcheck-container/pom.xml

index d1b4231..10a6488 100644 (file)
@@ -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/
index 034ff9d..9c4a723 100644 (file)
@@ -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};}));
+                       }
+               });
+       });
+};
index 159de1f..a1c45e9 100644 (file)
@@ -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});
        })
index cce714b..125bc24 100644 (file)
@@ -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)"
 }
index 43a2535..a0141a0 100644 (file)
@@ -27,7 +27,7 @@ limitations under the License.
   <groupId>org.onap.dcaegen2.deployments</groupId>
   <artifactId>healthcheck-container</artifactId>
   <name>dcaegen2-deployments-healthcheck-container</name>
-  <version>1.1.2</version>
+  <version>1.2.0</version>
   <url>http://maven.apache.org</url>
   <properties>
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>