コード例 #1
0
ファイル: kubectl.py プロジェクト: yuanbw/nauta
def get_top_for_pod(name: str, namespace: str) -> Tuple[str, str]:
    """
    Returns cpu and memory usage for a pod with a given name located in a given namespace
    :param name: name of a pod
    :param namespace:  namespace where the pod resided. Optional - if not given, function searches the pod in
                        current namespace
    :return: tuple containing two values - cpu and memory usage expressed in k8s format
    """
    top_command = ["kubectl", "top", "pod", name]

    if namespace:
        top_command.extend(["-n", namespace])
    output, err_code, log_output = system.execute_system_command(top_command)
    if err_code:
        raise KubectlConnectionError(
            Texts.K8S_CLUSTER_NO_CONNECTION_ERROR_MSG.format(
                output=log_output))

    if output:
        lines = output.split("\n")

        if lines and len(lines) > 1:
            second_line = lines[1]
            if second_line:
                split_second_line = second_line.split()
                if split_second_line and len(split_second_line) > 2:
                    return (split_second_line[1], split_second_line[2])

    logger.error(Texts.TOP_COMMAND_ERROR_LOG.format(output=log_output))
    raise KubernetesError(Texts.TOP_COMMAND_ERROR)
コード例 #2
0
def check_port_forwarding():
    config = Config()
    with K8sProxy(NAUTAAppNames.DOCKER_REGISTRY,
                  port=config.local_registry_port) as proxy:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        address = "127.0.0.1", proxy.tunnel_port
        if sock.connect_ex(address) != 0:
            raise KubectlConnectionError(Texts.K8S_PORT_FORWARDING_ERROR_MSG)
コード例 #3
0
ファイル: kubectl.py プロジェクト: yuanbw/nauta
def check_connection_to_cluster():
    check_connection_cmd = ['kubectl', 'get', 'pods']
    logger.debug(check_connection_cmd)
    output, err_code, log_output = system.execute_system_command(
        check_connection_cmd)
    logger.debug(
        f"check_connection_to_cluster - output : {err_code} - {log_output}")
    if err_code:
        raise KubectlConnectionError(
            Texts.K8S_CLUSTER_NO_CONNECTION_ERROR_MSG.format(
                output=log_output))
コード例 #4
0
def test_verify_with_kubectl_connection_error(mocker):
    check_connection_mock = mocker.patch.object(verify, "check_connection_to_cluster")
    check_connection_mock.side_effect = KubectlConnectionError("Cannot connect to K8S cluster")
    check_dependency_mock = mocker.patch.object(verify, "check_dependency", return_value=(True, LooseVersion('1.0')))
    mocker.patch.object(verify, "save_dependency_versions")
    mocker.patch.object(verify, "check_os")

    runner = CliRunner()
    result = runner.invoke(verify.verify, [])

    assert check_dependency_mock.call_count == 1
    assert check_connection_mock.call_count == 1, "connection wasn't checked"
    # noinspection PyUnresolvedReferences
    assert verify.save_dependency_versions.call_count == 0

    assert "Cannot connect to K8S cluster" in result.output, \
        "Bad output. Connection error should be indicated in console output."
コード例 #5
0
def check_port_forwarding():
    with K8sProxy(NAUTAAppNames.WEB_GUI) as proxy:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        address = "127.0.0.1", proxy.tunnel_port
        if sock.connect_ex(address) != 0:
            raise KubectlConnectionError(Texts.K8S_PORT_FORWARDING_ERROR_MSG)