From 2695b653b0d6105ac7a9ea0b56bff166dd3b5b35 Mon Sep 17 00:00:00 2001 From: DongZe Li <9546726@qq.com> Date: Wed, 20 Jan 2021 13:58:04 +0800 Subject: [PATCH] Try to read namespace from kube config when missing k8s_namesapce param (#95) --- python/graphscope/client/session.py | 5 +- .../graphscope/deploy/kubernetes/cluster.py | 54 ++++++++++--------- python/graphscope/deploy/kubernetes/utils.py | 8 +++ 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/python/graphscope/client/session.py b/python/graphscope/client/session.py index f3abd672199e..06a44063d0e3 100755 --- a/python/graphscope/client/session.py +++ b/python/graphscope/client/session.py @@ -171,8 +171,9 @@ def __init__( num_workers (int, optional): The number of workers to launch GraphScope engine. Defaults to 2. k8s_namespace (str, optional): Contains the namespace to create all resource inside. - If param missing or the namespace not exist, a random namespace will be created and deleted - when service stopping. Defaults to None. + If param missing, it will try to read namespace from kubernetes context, or + a random namespace will be created and deleted if namespace not exist. + Defaults to None. k8s_service_type (str, optional): Type determines how the GraphScope service is exposed. Valid options are NodePort, and LoadBalancer. Defaults to NodePort. diff --git a/python/graphscope/deploy/kubernetes/cluster.py b/python/graphscope/deploy/kubernetes/cluster.py index fe87651caf8b..51f4b7e14a5f 100644 --- a/python/graphscope/deploy/kubernetes/cluster.py +++ b/python/graphscope/deploy/kubernetes/cluster.py @@ -45,6 +45,7 @@ from graphscope.deploy.kubernetes.utils import delete_kubernetes_object from graphscope.deploy.kubernetes.utils import get_service_endpoints from graphscope.deploy.kubernetes.utils import is_minikube_cluster +from graphscope.deploy.kubernetes.utils import try_to_read_namespace_from_context from graphscope.deploy.kubernetes.utils import wait_for_deployment_complete from graphscope.framework.errors import K8sError from graphscope.framework.utils import random_string @@ -285,7 +286,10 @@ def _cluster_role_binding_exist(self, cluster_role_binding): def _create_namespace(self): if self._namespace is None: - self._namespace = self._get_free_namespace() + self._namespace = try_to_read_namespace_from_context() + # Doesn't have any namespace info in kube context. + if self._namespace is None: + self._namespace = self._get_free_namespace() if not self._namespace_exist(self._namespace): self._core_api.create_namespace(NamespaceBuilder(self._namespace).build()) self._delete_namespace = True @@ -326,31 +330,33 @@ def _create_role_and_binding(self): ) ) - if not self._cluster_role_exist(cluster_role=self._cluster_role_name): - cluster_role_builder = ClusterRoleBuilder( - name=self._cluster_role_name, - api_groups="apps,", - resources="namespaces", - verbs="create,delete,get,update,watch,list", - ) - targets.append( - self._rbac_api.create_cluster_role(cluster_role_builder.build()) - ) + if self._delete_namespace: + # Create clusterRole to delete namespace. + if not self._cluster_role_exist(cluster_role=self._cluster_role_name): + cluster_role_builder = ClusterRoleBuilder( + name=self._cluster_role_name, + api_groups="apps,", + resources="namespaces", + verbs="create,delete,get,update,watch,list", + ) + targets.append( + self._rbac_api.create_cluster_role(cluster_role_builder.build()) + ) - if not self._cluster_role_binding_exist( - cluster_role_binding=self._cluster_role_binding_name - ): - cluster_role_binding_builder = ClusterRoleBindingBuilder( - name=self._cluster_role_binding_name, - namespace=self._namespace, - cluster_role_name=self._cluster_role_name, - service_account_name="default", - ) - targets.append( - self._rbac_api.create_cluster_role_binding( - cluster_role_binding_builder.build() + if not self._cluster_role_binding_exist( + cluster_role_binding=self._cluster_role_binding_name + ): + cluster_role_binding_builder = ClusterRoleBindingBuilder( + name=self._cluster_role_binding_name, + namespace=self._namespace, + cluster_role_name=self._cluster_role_name, + service_account_name="default", + ) + targets.append( + self._rbac_api.create_cluster_role_binding( + cluster_role_binding_builder.build() + ) ) - ) self._resource_object.extend(targets) diff --git a/python/graphscope/deploy/kubernetes/utils.py b/python/graphscope/deploy/kubernetes/utils.py index 6eb6d8c8ab5f..7aa4451a854d 100644 --- a/python/graphscope/deploy/kubernetes/utils.py +++ b/python/graphscope/deploy/kubernetes/utils.py @@ -54,6 +54,14 @@ def is_minikube_cluster(): return active_context["context"]["cluster"] == "minikube" +def try_to_read_namespace_from_context(): + contexts, active_context = kube_config.list_kube_config_contexts() + if contexts and "namespace" in active_context["context"]: + return active_context["context"]["namespace"] + else: + return None + + def wait_for_deployment_complete(api_client, namespace, name, timeout_seconds=60): core_api = kube_client.CoreV1Api(api_client) app_api = kube_client.AppsV1Api(api_client)