Skip to content

Commit

Permalink
Try to read namespace from kube config when missing k8s_namesapce par…
Browse files Browse the repository at this point in the history
…am (#95)
  • Loading branch information
lidongze0629 authored Jan 20, 2021
1 parent aeca19d commit 2695b65
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
5 changes: 3 additions & 2 deletions python/graphscope/client/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
54 changes: 30 additions & 24 deletions python/graphscope/deploy/kubernetes/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
8 changes: 8 additions & 0 deletions python/graphscope/deploy/kubernetes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2695b65

Please sign in to comment.