X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FKubernetesUtils.py;h=d0d72a39a176eb0e2d7b0da9555d03b1dd1a6491;hp=029d635c72e477e3fb300ca5c0e6309331d63633;hb=d68951ac245150eeefa6e0f4156e4c1b5c9e9325;hpb=ed0258a440cfad7023d643f717ab78ac568dc59b diff --git a/resources/libraries/python/KubernetesUtils.py b/resources/libraries/python/KubernetesUtils.py index 029d635c72..d0d72a39a1 100644 --- a/resources/libraries/python/KubernetesUtils.py +++ b/resources/libraries/python/KubernetesUtils.py @@ -1,4 +1,4 @@ -# Copyright (c) 2018 Cisco and/or its affiliates. +# Copyright (c) 2019 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,25 +13,27 @@ """Library to control Kubernetes kubectl.""" +from functools import reduce +from io import open 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, exec_cmd_no_error from resources.libraries.python.CpuUtils import CpuUtils +from resources.libraries.python.ssh import SSH, exec_cmd_no_error +from resources.libraries.python.topology import NodeType from resources.libraries.python.VppConfigGenerator import VppConfigGenerator -__all__ = ["KubernetesUtils"] +__all__ = [u"KubernetesUtils"] # Maximum number of retries to check if PODs are running or deleted. MAX_RETRY = 48 -class KubernetesUtils(object): + +class KubernetesUtils: """Kubernetes utilities class.""" def __init__(self): """Initialize KubernetesUtils class.""" - pass @staticmethod def load_docker_image_on_node(node, image_path): @@ -43,20 +45,18 @@ class KubernetesUtils(object): :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']) + command = f"docker load -i {image_path}" + message = f"Failed to load Docker image on {node[u'host']}." + exec_cmd_no_error( + node, command, timeout=240, sudo=True, message=message + ) + + command = u"docker rmi $(sudo docker images -f 'dangling=true' -q)" + message = f"Failed to clean Docker images on {node[u'host']}." try: - exec_cmd_no_error(node, command, timeout=240, sudo=True, - message=message) + exec_cmd_no_error( + node, command, timeout=240, sudo=True, message=message + ) except RuntimeError: pass @@ -70,7 +70,7 @@ class KubernetesUtils(object): :type image_path: str """ for node in nodes.values(): - if node['type'] == NodeType.DUT: + if node[u"type"] == NodeType.DUT: KubernetesUtils.load_docker_image_on_node(node, image_path) @staticmethod @@ -84,16 +84,17 @@ class KubernetesUtils(object): ssh = SSH() ssh.connect(node) - 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) + cmd = f"{Constants.REMOTE_FW_DIR}/{Constants.RESOURCES_LIB_SH}/" \ + f"k8s_setup.sh deploy_calico" + 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'])) + raise RuntimeError( + "Failed to setup Kubernetes on {node[u'host']}." + ) - KubernetesUtils.wait_for_kubernetes_pods_on_node(node, - nspace='kube-system') + KubernetesUtils.wait_for_kubernetes_pods_on_node( + node, nspace=u"kube-system" + ) @staticmethod def setup_kubernetes_on_all_duts(nodes): @@ -103,7 +104,7 @@ class KubernetesUtils(object): :type nodes: dict """ for node in nodes.values(): - if node['type'] == NodeType.DUT: + if node[u"type"] == NodeType.DUT: KubernetesUtils.setup_kubernetes_on_node(node) @staticmethod @@ -117,13 +118,14 @@ class KubernetesUtils(object): 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) + cmd = f"{Constants.REMOTE_FW_DIR}/{Constants.RESOURCES_LIB_SH}/" \ + f"k8s_setup.sh destroy" + + 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'])) + raise RuntimeError( + f"Failed to destroy Kubernetes on {node[u'host']}." + ) @staticmethod def destroy_kubernetes_on_all_duts(nodes): @@ -133,7 +135,7 @@ class KubernetesUtils(object): :type nodes: dict """ for node in nodes.values(): - if node['type'] == NodeType.DUT: + if node[u"type"] == NodeType.DUT: KubernetesUtils.destroy_kubernetes_on_node(node) @staticmethod @@ -151,19 +153,20 @@ class KubernetesUtils(object): ssh = SSH() ssh.connect(node) - fqn_file = '{tpl}/{yaml}'.format(tpl=Constants.RESOURCES_TPL_K8S, - yaml=yaml_file) + fqn_file = f"{Constants.RESOURCES_TPL_K8S}/{yaml_file}" with open(fqn_file, 'r') as src_file: stream = src_file.read() - data = reduce(lambda a, kv: a.replace(*kv), kwargs.iteritems(), - stream) - cmd = 'cat <