Ejemplo n.º 1
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
Ejemplo n.º 2
0
 def test_valid_yaml_invalid_resource(self, context):
     config = KubeConfig(contexts=[context])
     with config.as_tempfile() as fname:
         client = KubectlClusterClient(KUBECTL_BIN, kubeconfig=fname)
         tmpl = textwrap.dedent('''
         - invalid
         - valid
         - resource
         ''')
         with pytest.raises(KubectlExecutionError):
             client.apply(tmpl)
         with pytest.raises(KubectlExecutionError):
             client.delete(tmpl)
Ejemplo n.º 3
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
Ejemplo n.º 4
0
 def test_normal(self, context):
     config = KubeConfig(contexts=[context])
     with config.as_tempfile() as fname:
         client = KubectlClusterClient(KUBECTL_BIN, kubeconfig=fname)
         tmpl = textwrap.dedent('''
         apiVersion: v1
         data:
             hello: world
         kind: ConfigMap
         metadata:
             creationTimestamp: null
             name: bcs-unitest-c1
         ''')
         client.apply(tmpl)
         client.delete(tmpl)
Ejemplo n.º 5
0
 def test_invalid_format(self, context):
     config = KubeConfig(contexts=[context])
     with config.as_tempfile() as fname:
         client = KubectlClusterClient(KUBECTL_BIN, kubeconfig=fname)
         with pytest.raises(KubectlExecutionError):
             client.apply("invalid_template")