Repository: sql-machine-learning/elasticdl

Issue:
Need more informative error message when failing to load kubeconfig 
Currently if kubeconfig is not properly setup, we'll receive an error like the following which is not very informative:
```
Traceback (most recent call last):
  File "/miniconda/envs/sqlflow-dev/bin/elasticdl", line 11, in <module>
    load_entry_point('elasticdl==0.0.1', 'console_scripts', 'elasticdl')()
  File "/miniconda/envs/sqlflow-dev/lib/python3.6/site-packages/elasticdl-0.0.1-py3.6.egg/elasticdl/python/elasticdl/client.py", line 46, in main
    args.func(args)
  File "/miniconda/envs/sqlflow-dev/lib/python3.6/site-packages/elasticdl-0.0.1-py3.6.egg/elasticdl/python/elasticdl/api.py", line 43, in train
    _submit_job(image_name, args, container_args)
  File "/miniconda/envs/sqlflow-dev/lib/python3.6/site-packages/elasticdl-0.0.1-py3.6.egg/elasticdl/python/elasticdl/api.py", line 132, in _submit_job
    cluster_spec=client_args.cluster_spec,
  File "/miniconda/envs/sqlflow-dev/lib/python3.6/site-packages/elasticdl-0.0.1-py3.6.egg/elasticdl/python/common/k8s_client.py", line 51, in __init__
    config.load_kube_config()
  File "/miniconda/envs/sqlflow-dev/lib/python3.6/site-packages/kubernetes/config/kube_config.py", line 645, in load_kube_config
    persist_config=persist_config)
  File "/miniconda/envs/sqlflow-dev/lib/python3.6/site-packages/kubernetes/config/kube_config.py", line 613, in _get_kube_config_loader_for_yaml_file
    **kwargs)
  File "/miniconda/envs/sqlflow-dev/lib/python3.6/site-packages/kubernetes/config/kube_config.py", line 153, in __init__
    self.set_active_context(active_context)
  File "/miniconda/envs/sqlflow-dev/lib/python3.6/site-packages/kubernetes/config/kube_config.py", line 173, in set_active_context
    context_name = self._config['current-context']
  File "/miniconda/envs/sqlflow-dev/lib/python3.6/site-packages/kubernetes/config/kube_config.py", line 495, in __getitem__
    v = self.safe_get(key)
  File "/miniconda/envs/sqlflow-dev/lib/python3.6/site-packages/kubernetes/config/kube_config.py", line 491, in safe_get
    key in self.value):
TypeError: argument of type 'NoneType' is not iterable
```


Discussion:

Resolving patch:
diff --git a/elasticdl/python/common/k8s_client.py b/elasticdl/python/common/k8s_client.py
--- a/elasticdl/python/common/k8s_client.py
+++ b/elasticdl/python/common/k8s_client.py
@@ -43,12 +43,18 @@ def __init__(
             event_callback: If not None, an event watcher will be created and
                 events passed to the callback.
         """
-        if os.getenv("KUBERNETES_SERVICE_HOST"):
-            # We are running inside k8s
-            config.load_incluster_config()
-        else:
-            # Use user's kube config
-            config.load_kube_config()
+        try:
+            if os.getenv("KUBERNETES_SERVICE_HOST"):
+                # We are running inside a k8s cluster
+                config.load_incluster_config()
+            else:
+                # Use user's kube config
+                config.load_kube_config()
+        except Exception as ex:
+            traceback.print_exc()
+            raise Exception(
+                "Failed to load configuration for Kubernetes:\n%s" % str(ex)
+            )
 
         self.client = client.CoreV1Api()
         self.namespace = namespace


Test patch: