Esempio n. 1
0
def make_kubectl_client(access_token=None, project_id=None, cluster_id=None):
    """make a kubectl client for connection to k8s, it return a tuple of kubectl client and exception
    """
    options = dict()
    host = get_bcs_host(access_token, project_id, cluster_id)
    if host:
        bcs_client = get_bcs_client(project_id=project_id,
                                    cluster_id=cluster_id,
                                    access_token=access_token)
        try:
            with bcs_client.make_kubectl_client() as kubectl_client:
                yield kubectl_client, None
        except Exception as e:
            logger.exception("make kubectl client failed, %s", e)
            yield None, e
    else:
        # default
        kubectl_client = KubectlClusterClient(kubectl_bin=settings.KUBECTL_BIN,
                                              kubeconfig=settings.KUBECFG,
                                              **options)
        yield kubectl_client, None
Esempio n. 2
0
def make_kubectl_client_from_kubeconfig(kubeconfig_content, **options):
    with tempfile.NamedTemporaryFile() as fp:
        fp.write(kubeconfig_content.encode())
        fp.flush()
        kubectl_client = KubectlClusterClient(kubectl_bin=settings.KUBECTL_BIN, kubeconfig=fp.name, **options)
        yield kubectl_client