summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-notification-lib/openecomp-sdc-notification-core/pom.xml
blob: 6c06e016abe5dd39e6659558eb0311e3379de82d (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
27
28
29
30
31
32
33
34
35
36
37
38
39
<!--
  ~ Copyright © 2016-2018 European Support Limited
  ~
  ~ 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.
  -->

<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>

  <name>openecomp-sdc-notification-core</name>
  <artifactId>openecomp-sdc-notification-core</artifactId>


  <parent>
    <groupId>org.openecomp.sdc</groupId>
    <artifactId>openecomp-sdc-notification-lib</artifactId>
    <version>1.10.1-SNAPSHOT</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.openecomp.sdc</groupId>
      <artifactId>openecomp-sdc-notification-api</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>

</project>
{ color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #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 */ }
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#   COPYRIGHT NOTICE STARTS HERE

#   Copyright 2019 © Samsung Electronics Co., Ltd.
#
#   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.

#   COPYRIGHT NOTICE ENDS HERE


from __future__ import print_function
import sys
import argparse
import yaml
import requests
from subprocess import Popen,STDOUT,PIPE,check_output
import datetime
from time import sleep
from os.path import expanduser
from itertools import chain
import csv
from requests.packages.urllib3.exceptions import InsecureRequestWarning
from base64 import b64decode
from tempfile import NamedTemporaryFile

def add_resource_kind(resources, kind):
    for item in resources:
        item['kind'] = kind
    return resources

def pods_by_parent(pods, parent):
    for pod in pods:
        if pod['metadata']['labels']['app'] == parent:
            yield pod

def k8s_controller_ready(k8s_controller):
    if k8s_controller['kind'] == 'Job':
        return k8s_controller['status'].get('succeeded', 0) == k8s_controller['spec']['completions']
    return k8s_controller['status'].get('readyReplicas', 0) == k8s_controller['spec']['replicas']

def get_not_ready(data):
    return [x for x in data if not k8s_controller_ready(x)]

def get_apps(data):
    return [x['metadata']['labels']['app'] for x in data]

def get_names(data):
    return [x['metadata']['name'] for x in data]

def pod_ready(pod):
    try:
        return [x['status'] for x in pod['status']['conditions']
                    if x['type'] == 'Ready'][0] == 'True'
    except (KeyError, IndexError):
        return False

def not_ready_pods(pods):
    for pod in pods:
        if not pod_ready(pod):
            yield pod

def analyze_k8s_controllers(resources_data):
    resources = {'total_count': len(resources_data)}
    resources['not_ready_list'] = get_apps(get_not_ready(resources_data))
    resources['ready_count'] = resources['total_count'] - len(resources['not_ready_list'])

    return resources

def get_k8s_controllers(k8s):
    k8s_controllers = {}

    k8s_controllers['deployments'] = {'data': k8s.get_resources(
        'apis/apps/v1', 'deployments')}
    k8s_controllers['deployments'].update(analyze_k8s_controllers(
        k8s_controllers['deployments']['data']))

    k8s_controllers['statefulsets'] = {'data': k8s.get_resources(
        'apis/apps/v1', 'statefulsets')}
    k8s_controllers['statefulsets'].update(analyze_k8s_controllers(
        k8s_controllers['statefulsets']['data']))

    k8s_controllers['jobs'] = {'data': k8s.get_resources(
        'apis/batch/v1', 'jobs')}
    k8s_controllers['jobs'].update(analyze_k8s_controllers(
        k8s_controllers['jobs']['data']))

    not_ready_controllers = chain.from_iterable(
            k8s_controllers[x]['not_ready_list'] for x in k8s_controllers)

    return k8s_controllers, list(not_ready_controllers)

def exec_healthcheck(hp_script, namespace, hp_mode):
    # spawn healthcheck script and redirect it's stderr to stdout
    hc = Popen(['sh',hp_script,namespace,hp_mode],stdout=PIPE,stderr=STDOUT)
    # Trace the output of subprocess until it has finished
    for line in iter(hc.stdout.readline, ''):
        print(line.strip())
    hc.poll() # set returncode in Popen object
    return hc.returncode

def check_readiness(k8s, verbosity):
        k8s_controllers, not_ready_controllers = get_k8s_controllers(k8s)

        # check pods only when it is explicitly wanted (judging readiness by deployment status)
        if verbosity > 1:
            pods = k8s.get_resources('api/v1', 'pods')
            unready_pods = chain.from_iterable(
                   get_names(not_ready_pods(
                       pods_by_parent(pods, x)))
                   for x in not_ready_controllers)
        else:
            unready_pods = []

        print_status(verbosity, k8s_controllers, unready_pods)
        return not not_ready_controllers

def check_in_loop(k8s, max_time, sleep_time, verbosity):
    max_end_time = datetime.datetime.now() + datetime.timedelta(minutes=max_time)
    ready = False
    while datetime.datetime.now() < max_end_time:
        ready = check_readiness(k8s, verbosity)
        if ready:
            return ready
        sleep(sleep_time)
    return ready

def check_helm_releases():
    helm = check_output(['helm', 'ls'])
    if helm == '':
        sys.exit('No Helm releases detected.')
    helm_releases = csv.DictReader(
            map(lambda x: x.replace(' ', ''), helm.split('\n')),
            delimiter='\t')
    failed_releases = [release['NAME'] for release in helm_releases
            if release['STATUS'] == 'FAILED']
    return helm, failed_releases


def create_ready_string(ready, total, prefix):
    return '{:12} {}/{}'.format(prefix, ready, total)

def print_status(verbosity, resources, not_ready_pods):
    ready_strings = []
    ready = {k: v['ready_count'] for k,v in resources.items()}
    count = {k: v['total_count'] for k,v in resources.items()}
    if verbosity > 0:
        ready_strings += [
                create_ready_string(ready[k], count[k], k.capitalize()) for k in ready
                ]
    total_ready = sum(ready.values())
    total_count = sum(count.values())
    ready_strings.append(create_ready_string(total_ready, total_count, 'Ready'))
    status_strings = ['\n'.join(ready_strings)]
    if verbosity > 1:
        if not_ready_pods:
            status_strings.append('\nWaiting for pods:\n{}'.format('\n'.join(not_ready_pods)))
        else:
            status_strings.append('\nAll pods are ready!')
    print('\n'.join(status_strings), '\n')

def parse_args():
    parser = argparse.ArgumentParser(description='Monitor ONAP deployment progress',
            formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--namespace', '-n', default='onap',
            help='Kubernetes namespace of ONAP')
    parser.add_argument('--server', '-s', help='address of Kubernetes cluster')
    parser.add_argument('--kubeconfig', '-c',
            default=expanduser('~') + '/.kube/config',
            help='path to .kube/config file')
    parser.add_argument('--health-path', '-hp', help='path to ONAP robot ete-k8s.sh')
    parser.add_argument('--health-mode', '-hm', default='health', help='healthcheck mode')
    parser.add_argument('--no-helm', action='store_true', help='Do not check Helm')
    parser.add_argument('--check-frequency', '-w', default=300, type=int,
            help='time between readiness checks in seconds')
    parser.add_argument('--max-time', '-t', default=120, type=int,
            help='max time to run readiness checks in minutes')
    parser.add_argument('--single-run', '-1', action='store_true',
            help='run check loop only once')
    parser.add_argument('-v', dest='verbosity', action='count', default=0,
            help='increase output verbosity, e.g. -vv is more verbose than -v')
    parser.add_argument('--no-ssl-auth', action='store_true',
            help='Disable SSL certificate based authentication while connecting to server')

    return parser.parse_args()

class Kubernetes:
    '''Class exposing get_resources() routine for connecting to kube API.
       It keeps all attributes required by that call as an internal
       object state.'''

    requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

    def __init__(self,args):

        self.config = args.kubeconfig
        self.url = args.server if args.server is not None else \
                   self._get_k8s_url()
        self.no_ssl_auth = args.no_ssl_auth
        self.certs = self._get_k8s_certs() if not self.no_ssl_auth else {}
        self.namespace = args.namespace

        # Setup tmp file with ca chain only if certs were gathered successfully
        # and --no-ssl-auth wasn't set
        if self.certs and not self.no_ssl_auth:
            self._setup_cert_files()

    def get_resources(self, api, kind):
        '''Performs actual API call'''
        url = '/'.join([self.url, api, 'namespaces', self.namespace, kind])
        try:
            if self.no_ssl_auth:
                req = requests.get(url, verify=False)
            else:
                req = requests.get(url, verify=self.crt_tmp_file.name, cert=self.crt_tmp_file.name)
        except requests.exceptions.ConnectionError as err:
            sys.exit('Error: Could not connect to {}'.format(self.url))
        if req.status_code == 200:
            json = req.json()
            # kind is <resource>List in response so [:-4] removes 'List' from value
            return add_resource_kind(json['items'], json['kind'][:-4])
        elif (req.status_code == 401):
            sys.exit('Error: Server replied with "401 Unauthorized" while making connection')
        else:
            sys.exit("Error: There's been an unspecified issue while making a request to the API")

    def _setup_cert_files(self):
        '''Helper funtion to setup named file for requests.get() call
           in self.get_resources() which is able read certificate only
           from file'''
        ca_chain = NamedTemporaryFile()
        for crt in self.certs.values():
            ca_chain.write(crt)
        ca_chain.read() # flush the file buffer
        self.crt_tmp_file = ca_chain

    def _get_k8s_url(self):
        # TODO: Get login info
        with open(self.config) as f:
            config = yaml.load(f)
        # TODO: Support cluster by name
        return config['clusters'][0]['cluster']['server']

    def _get_k8s_certs(self):
        '''Helper function to read and decode certificates from kube config'''
        with open(self.config) as f:
            config = yaml.load(f)
        certs = {}
        try:
            certs.update(dict(ca_cert=b64decode(
              config['clusters'][0]['cluster']['certificate-authority-data'])))
            certs.update(dict(client_cert=b64decode(
              config['users'][0]['user']['client-certificate-data'])))
            certs.update(dict(client_key=b64decode(
              config['users'][0]['user']['client-key-data'])))
        except KeyError as err:
            print('Warning: could not get Kubernetes config for certificates. ' \
                      'Turning off SSL authentication.')
            self.no_ssl_auth = True
        return certs

def main():
    args = parse_args()

    if not args.no_helm:
        try:
            helm_output, failed_releases = check_helm_releases()
            if failed_releases:
                print('Deployment of {} failed.'.format(','.join(failed_releases)))
                sys.exit(1)
            elif args.verbosity > 1:
                print(helm_output)
        except IOError as err:
            sys.exit(err.strerror)

    k8s = Kubernetes(args)

    ready = False
    if args.single_run:
        ready = check_readiness(k8s, args.verbosity)
    else:
        if not check_in_loop(k8s, args.max_time, args.check_frequency, args.verbosity):
            # Double-check last 5 minutes and write verbosely in case it is not ready
            ready = check_readiness(k8s, 2)

    if args.health_path is not None:
        hc_rc = exec_healthcheck(args.health_path, args.namespace, args.health_mode)
        if hc_rc:
            sys.exit(hc_rc)

    if not ready:
        sys.exit('Deployment is not ready')

if __name__ == '__main__':
    main()