X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FKubernetesUtils.py;fp=resources%2Flibraries%2Fpython%2FKubernetesUtils.py;h=5faa056ddc946d9f46d7c9be269946293ea6b5be;hp=0000000000000000000000000000000000000000;hb=9a261ea61549fc6a5c23369d2e236b002dc35038;hpb=f2573eccd38609fbc3d44f1fb9c706d08e50d49c diff --git a/resources/libraries/python/KubernetesUtils.py b/resources/libraries/python/KubernetesUtils.py new file mode 100644 index 0000000000..5faa056ddc --- /dev/null +++ b/resources/libraries/python/KubernetesUtils.py @@ -0,0 +1,372 @@ +# Copyright (c) 2017 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: +# +# 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. + +"""Library to control Kubernetes kubectl.""" + +import time +import yaml + +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.CpuUtils import CpuUtils +from resources.libraries.python.VppConfigGenerator import VppConfigGenerator + +__all__ = ["KubernetesUtils"] + + +class KubernetesUtils(object): + """Kubernetes utilities class.""" + + def __init__(self): + """Initialize KubernetesUtils class.""" + pass + + @staticmethod + def setup_kubernetes_on_node(node): + """Set up Kubernetes on node. + + :param node: DUT node. + :type node: dict + :raises RuntimeError: If Kubernetes setup failed on node. + """ + 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) + if int(ret_code) != 0: + raise RuntimeError('Failed to setup Kubernetes on {node}.' + .format(node=node['host'])) + + @staticmethod + def setup_kubernetes_on_all_duts(nodes): + """Set up Kubernetes on all DUTs. + + :param nodes: Topology nodes. + :type nodes: dict + """ + for node in nodes.values(): + if node['type'] == NodeType.DUT: + KubernetesUtils.setup_kubernetes_on_node(node) + + @staticmethod + def apply_kubernetes_resource_on_node(node, yaml_file, **kwargs): + """Apply Kubernetes resource on node. + + :param node: DUT node. + :param yaml_file: YAML configuration file. + :param kwargs: Key-value pairs to replace in YAML template. + :type node: dict + :type yaml_file: str + :type kwargs: dict + :raises RuntimeError: If applying Kubernetes template failed. + """ + 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): + 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') + cmd = 'cat <