X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FKubernetesUtils.py;h=7a4784d42f3e86a06bb99933b68912f82c18d4d1;hp=69e7e832b4d7d0fc4bbcdb6409d22f3b73fe6240;hb=585bba1465e4fe2e83f6ff702c7db5af4f05d162;hpb=b015ecf08aba9659d0261dfa6eb0da9ab4950b92 diff --git a/resources/libraries/python/KubernetesUtils.py b/resources/libraries/python/KubernetesUtils.py index 69e7e832b4..7a4784d42f 100644 --- a/resources/libraries/python/KubernetesUtils.py +++ b/resources/libraries/python/KubernetesUtils.py @@ -1,4 +1,4 @@ -# Copyright (c) 2017 Cisco and/or its affiliates. +# Copyright (c) 2018 Cisco and/or its affiliates. # 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: @@ -13,17 +13,18 @@ """Library to control Kubernetes kubectl.""" -import time -import yaml +from time import sleep from resources.libraries.python.constants import Constants from resources.libraries.python.topology import NodeType -from resources.libraries.python.ssh import SSH +from resources.libraries.python.ssh import SSH, exec_cmd_no_error from resources.libraries.python.CpuUtils import CpuUtils from resources.libraries.python.VppConfigGenerator import VppConfigGenerator __all__ = ["KubernetesUtils"] +# Maximum number of retries to check if PODs are running or deleted. +MAX_RETRY = 48 class KubernetesUtils(object): """Kubernetes utilities class.""" @@ -32,6 +33,46 @@ class KubernetesUtils(object): """Initialize KubernetesUtils class.""" pass + @staticmethod + def load_docker_image_on_node(node, image_path): + """Load Docker container image from file on node. + + :param node: DUT node. + :param image_path: Container image path. + :type node: dict + :type image_path: str + :raises RuntimeError: If loading image failed on node. + """ + command = 'docker load -i {image_path}'.\ + format(image_path=image_path) + message = 'Failed to load Docker image on {node}.'.\ + format(node=node['host']) + exec_cmd_no_error(node, command, timeout=240, sudo=True, + message=message) + + command = "docker rmi $(sudo docker images -f 'dangling=true' -q)".\ + format(image_path=image_path) + message = 'Failed to clean Docker images on {node}.'.\ + format(node=node['host']) + try: + exec_cmd_no_error(node, command, timeout=240, sudo=True, + message=message) + except RuntimeError: + pass + + @staticmethod + def load_docker_image_on_all_duts(nodes, image_path): + """Load Docker container image from file on all DUTs. + + :param nodes: Topology nodes. + :param image_path: Container image path. + :type nodes: dict + :type image_path: str + """ + for node in nodes.values(): + if node['type'] == NodeType.DUT: + KubernetesUtils.load_docker_image_on_node(node, image_path) + @staticmethod def setup_kubernetes_on_node(node): """Set up Kubernetes on node. @@ -43,9 +84,10 @@ class KubernetesUtils(object): ssh = SSH() ssh.connect(node) - cmd = '{dir}/{lib}/k8s_setup.sh '.format(dir=Constants.REMOTE_FW_DIR, - lib=Constants.RESOURCES_LIB_SH) - (ret_code, _, _) = ssh.exec_command(cmd, timeout=120) + cmd = '{dir}/{lib}/k8s_setup.sh deploy_calico'\ + .format(dir=Constants.REMOTE_FW_DIR, + lib=Constants.RESOURCES_LIB_SH) + (ret_code, _, _) = ssh.exec_command(cmd, timeout=240) if int(ret_code) != 0: raise RuntimeError('Failed to setup Kubernetes on {node}.' .format(node=node['host'])) @@ -64,6 +106,36 @@ class KubernetesUtils(object): if node['type'] == NodeType.DUT: KubernetesUtils.setup_kubernetes_on_node(node) + @staticmethod + def destroy_kubernetes_on_node(node): + """Destroy Kubernetes on node. + + :param node: DUT node. + :type node: dict + :raises RuntimeError: If destroying Kubernetes failed. + """ + ssh = SSH() + ssh.connect(node) + + cmd = '{dir}/{lib}/k8s_setup.sh destroy'\ + .format(dir=Constants.REMOTE_FW_DIR, + lib=Constants.RESOURCES_LIB_SH) + (ret_code, _, _) = ssh.exec_command(cmd, timeout=120) + if int(ret_code) != 0: + raise RuntimeError('Failed to destroy Kubernetes on {node}.' + .format(node=node['host'])) + + @staticmethod + def destroy_kubernetes_on_all_duts(nodes): + """Destroy Kubernetes on all DUTs. + + :param nodes: Topology nodes. + :type nodes: dict + """ + for node in nodes.values(): + if node['type'] == NodeType.DUT: + KubernetesUtils.destroy_kubernetes_on_node(node) + @staticmethod def apply_kubernetes_resource_on_node(node, yaml_file, **kwargs): """Apply Kubernetes resource on node. @@ -79,18 +151,15 @@ class KubernetesUtils(object): ssh = SSH() ssh.connect(node) - stream = file('{tpl}/{yaml}'.format(tpl=Constants.RESOURCES_TPL_K8S, - yaml=yaml_file), 'r') - - for data in yaml.load_all(stream): + fqn_file = '{tpl}/{yaml}'.format(tpl=Constants.RESOURCES_TPL_K8S, + yaml=yaml_file) + with open(fqn_file, 'r') as src_file: + stream = src_file.read() data = reduce(lambda a, kv: a.replace(*kv), kwargs.iteritems(), - yaml.dump(data, default_flow_style=False)) - # Workaround to avoid using RAW string anotated with | in YAML as - # library + bash is misinterpreting spaces. - data = data.replace('.conf:\n', '.conf: |\n') + stream) cmd = 'cat <