aboutsummaryrefslogtreecommitdiffstats
path: root/generic-resource-api/pom.xml
blob: a336eafbc35643d46e262e2e5095a2d21e298f1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.onap.ccsdk.parent</groupId>
        <artifactId>odlparent-lite</artifactId>
        <version>2.2.4</version>
        <relativePath/>
    </parent>

    <groupId>org.onap.sdnc.northbound</groupId>
    <artifactId>generic-resource-api</artifactId>
    <version>2.2.3</version>
    <packaging>pom</packaging>

    <name>sdnc-northbound :: generic-resource-api</name>

    <modules>
        <module>model</module>
        <module>provider</module>
        <module>features</module>
        <module>client</module>
        <module>installer</module>
    </modules>
</project>
lor: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
/*
Copyright(c) 2018 AT&T Intellectual Property. All rights reserved.
Copyright(c) 2020 Nokia 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.
*/

/*
 * Query Kubernetes for status of deployments and extract readiness information
 */

const fs = require('fs');
const https = require('https');

const K8S_CREDS = '/var/run/secrets/kubernetes.io/serviceaccount';
const K8S_HOST = 'kubernetes.default.svc.cluster.local';	// Full name to match cert for TLS
const K8S_PATH = 'apis/apps/v1/namespaces/';

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');

const summarizeDeploymentList = function(list) {
    // list is a DeploymentList object returned by k8s
    // Individual deployments are in the array 'items'

    let ret =
    {
        type: "summary",
        count: 0,
        ready: 0,
        items: []
    };

    // Extract readiness information
    for (let deployment of list.items) {
        ret.items.push(
            {
                name: deployment.metadata.name,
                ready: deployment.status.readyReplicas || 0,
                unavailable: deployment.status.unavailableReplicas || 0
            }
        );
        ret.count ++;
        ret.ready = ret.ready + (deployment.status.readyReplicas || 0);
    }

    return ret;
};

const queryKubernetes = function(path, callback) {
    // Make GET request to Kubernetes API
    const options = {
        host: K8S_HOST,
        path: "/" + path,
        ca : ca,
        headers: {
            Authorization: 'bearer ' + token
        }
    };
    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 getStatusSinglePromise = function (item) {
    // Expect item to be of the form {namespace: "namespace", deployment: "deployment_name"}
    return new Promise(function(resolve, reject){
        const path = K8S_PATH + item.namespace + '/deployments/' + item.deployment;
        queryKubernetes(path, function(error, res, body){
            if (error) {
                reject(error);
            }
            else if (res.statusCode === 404) {
                // Treat absent deployment as if it's an unhealthy deployment
                resolve ({
                    metadata: {name: item.deployment},
                    status: {unavailableReplicas: 1}
                });
            }
            else if (res.statusCode != 200) {
                reject(body);
            }
            else {
                resolve(body);
            }
        });
    });
}

exports.getStatusListPromise = function (list) {
    // List is of the form [{namespace: "namespace", deployment: "deployment_name"}, ... ]
    const p = Promise.all(list.map(getStatusSinglePromise))
    return p.then(function(results) {
        return summarizeDeploymentList({items: results});
    });
}

exports.getLabeledDeploymentsPromise = function (namespace, label) {
    // Return list of the form [{namespace: "namespace", deployment: "deployment_name"}].
    // List contains all k8s deployments in the specified namespace that were deployed
    // with the specified 'label'.  (The check is for the presence of the label--its
    // values is not important.)  In DCAE, this is used to find deployments created
    // by Cloudify, based on Cloudify's use of a "marker" label on each k8s deployment that
    // the k8s plugin created.
    // If 'label' is unspecified or has zero length, returns an empty list.

    return new Promise(function(resolve, reject) {
        if (!label || label.length < 1) {
          resolve([]);
        }
        else {
            const path = K8S_PATH + namespace + '/deployments?labelSelector=' + 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};}));
                }
            });
        }
    });
};