Repository: sql-machine-learning/elasticdl

Issue:
Expose namespace as an arg to users
Currently namespace is hard-coded to use "default" namespace in `master.main`. However, this won't be sufficient for many potential users. We need to expose this to our users.


Discussion:

Resolving patch:
diff --git a/elasticdl/python/elasticdl/client/client.py b/elasticdl/python/elasticdl/client/client.py
--- a/elasticdl/python/elasticdl/client/client.py
+++ b/elasticdl/python/elasticdl/client/client.py
@@ -95,6 +95,13 @@ def _add_train_params(parser):
     parser.add_argument(
         "--extra_pypi_index", help="The extra python package repository"
     )
+    parser.add_argument(
+        "--namespace",
+        default="default",
+        type=str,
+        help="The name of the Kubernetes namespace where ElasticDL "
+             "pods will be created",
+    )
 
 
 def _add_evaluate_params(parser):
@@ -200,6 +207,8 @@ def _submit(image_name, model_file, job_name, args, argv):
         args.worker_resource_request,
         "--worker_resource_limit",
         args.worker_resource_request,
+        "--namespace",
+        args.namespace,
     ]
     container_args.extend(["--image_pull_policy", args.image_pull_policy])
     container_args.extend(["--restart_policy", args.restart_policy])
@@ -223,7 +232,7 @@ def _submit(image_name, model_file, job_name, args, argv):
 
     k8s.Client(
         image_name=image_name,
-        namespace="default",
+        namespace=args.namespace,
         job_name=job_name,
         event_callback=None,
     ).create_master(
diff --git a/elasticdl/python/elasticdl/common/k8s_client.py b/elasticdl/python/elasticdl/common/k8s_client.py
--- a/elasticdl/python/elasticdl/common/k8s_client.py
+++ b/elasticdl/python/elasticdl/common/k8s_client.py
@@ -22,7 +22,8 @@ def __init__(self, *, image_name, namespace, job_name, event_callback):
 
         Args:
             image_name: Docker image path for ElasticDL pod.
-            namespace: k8s namespace for ElasticDL pods.
+            namespace: The name of the Kubernetes namespace where ElasticDL
+                pods will be created.
             job_name: ElasticDL job name, should be unique in the namespace.
                 Used as pod name prefix and value for "elasticdl" label.
             event_callback: If not None, an event watcher will be created and
@@ -65,8 +66,8 @@ def get_master_pod_name(self):
     def get_worker_pod_name(self, worker_id):
         return "elasticdl-%s-worker-%s" % (self._job_name, str(worker_id))
 
-    @staticmethod
     def _create_pod(
+        self,
         pod_name,
         job_name,
         image_name,
@@ -145,6 +146,7 @@ def _create_pod(
                 # TODO: Add tests for this once we've done refactoring on
                 # k8s client code and the constant strings
                 owner_references=owner_ref,
+                namespace=self._ns,
             ),
         )
         return pod
diff --git a/elasticdl/python/elasticdl/master/main.py b/elasticdl/python/elasticdl/master/main.py
--- a/elasticdl/python/elasticdl/master/main.py
+++ b/elasticdl/python/elasticdl/master/main.py
@@ -180,6 +180,13 @@ def _parse_args():
         default="Never",
         help="The pod restart policy when pod crashed",
     )
+    parser.add_argument(
+        "--namespace",
+        default="default",
+        type=str,
+        help="The name of the Kubernetes namespace where ElasticDL "
+             "pods will be created",
+    )
     return parser.parse_args()
 
 
@@ -270,7 +277,7 @@ def main():
             image_name=args.worker_image,
             command=worker_command,
             args=worker_args,
-            namespace="default",
+            namespace=args.namespace,
             num_workers=args.num_workers,
             worker_resource_request=args.worker_resource_request,
             worker_resource_limit=args.worker_resource_limit,


Test patch: