diff --git a/README.md b/README.md index ec254a4211..e69f5c352c 100644 --- a/README.md +++ b/README.md @@ -27,12 +27,10 @@ pip install kubernetes list all pods: ```python -import os - from kubernetes import client, config # Configs can be set in Configuration class directly or using helper utility -config.load_kube_config(os.path.join(os.path.expanduser('~'), '.kube', 'config')) +config.load_kube_config() v1=client.CoreV1Api() print("Listing pods with their IPs:") @@ -44,12 +42,10 @@ for i in ret.items: watch on namespace object: ```python -import os - from kubernetes import client, config, watch # Configs can be set in Configuration class directly or using helper utility -config.load_kube_config(os.path.join(os.path.expanduser('~'), '.kube', 'config')) +config.load_kube_config() v1 = client.CoreV1Api() count = 10 diff --git a/examples/example1.py b/examples/example1.py index 192c237dc4..214fd190f7 100644 --- a/examples/example1.py +++ b/examples/example1.py @@ -12,16 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os - from kubernetes import client, config def main(): # Configs can be set in Configuration class directly or using helper - # utility - config.load_kube_config( - os.path.join(os.path.expanduser('~'), '.kube', 'config')) + # utility. If no argument provided, the config will be loaded from + # default location. + config.load_kube_config() v1 = client.CoreV1Api() print("Listing pods with their IPs:") diff --git a/examples/example2.py b/examples/example2.py index 07e8dfd688..003d5c9601 100644 --- a/examples/example2.py +++ b/examples/example2.py @@ -12,16 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os - from kubernetes import client, config, watch def main(): # Configs can be set in Configuration class directly or using helper - # utility - config.load_kube_config( - os.path.join(os.path.expanduser('~'), '.kube', 'config')) + # utility. If no argument provided, the config will be loaded from + # default location. + config.load_kube_config() v1 = client.CoreV1Api() count = 10 diff --git a/examples/example3.py b/examples/example3.py index 84904e44ce..25c0d9a807 100644 --- a/examples/example3.py +++ b/examples/example3.py @@ -12,16 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os - from kubernetes import client, config def main(): # Configs can be set in Configuration class directly or using helper - # utility - config.load_kube_config( - os.path.join(os.path.expanduser('~'), '.kube', 'config')) + # utility. If no argument provided, the config will be loaded from + # default location. + config.load_kube_config() print("Supported APIs (* is preferred version):") print("%-20s %s" % diff --git a/examples/example4.py b/examples/example4.py index af309b191e..2c6ca67558 100644 --- a/examples/example4.py +++ b/examples/example4.py @@ -12,15 +12,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import os - from kubernetes import client, config from kubernetes.client import configuration def main(): - config_file = os.path.join(os.path.expanduser('~'), '.kube', 'config') - contexts, active_context = config.list_kube_config_contexts(config_file) + contexts, active_context = config.list_kube_config_contexts() if not contexts: print("Cannot find any context in kube-config file.") return @@ -43,7 +40,7 @@ def main(): # Configs can be set in Configuration class directly or using helper # utility - config.load_kube_config(config_file, context_name) + config.load_kube_config(context=context_name) print("Active host is %s" % configuration.host) diff --git a/kubernetes/config/kube_config.py b/kubernetes/config/kube_config.py index 9a52011502..dfde2851ca 100644 --- a/kubernetes/config/kube_config.py +++ b/kubernetes/config/kube_config.py @@ -24,6 +24,7 @@ from .config_exception import ConfigException +KUBE_CONFIG_DEFAULT_LOCATION = '~/.kube/config' _temp_files = {} @@ -269,12 +270,16 @@ def _get_kube_config_loader_for_yaml_file(filename, **kwargs): **kwargs) -def list_kube_config_contexts(config_file): +def list_kube_config_contexts(config_file=None): + + if config_file is None: + config_file = os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION) + loader = _get_kube_config_loader_for_yaml_file(config_file) return loader.list_contexts(), loader.current_context -def load_kube_config(config_file, context=None): +def load_kube_config(config_file=None, context=None): """Loads authentication and cluster information from kube-config file and stores them in kubernetes.client.configuration. @@ -283,5 +288,8 @@ def load_kube_config(config_file, context=None): from config file will be used. """ + if config_file is None: + config_file = os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION) + _get_kube_config_loader_for_yaml_file( config_file, active_context=context).load_and_set() diff --git a/kubernetes/config/kube_config_test.py b/kubernetes/config/kube_config_test.py index 14ff14d9b7..00f3ddb57a 100644 --- a/kubernetes/config/kube_config_test.py +++ b/kubernetes/config/kube_config_test.py @@ -20,7 +20,7 @@ from .config_exception import ConfigException from .kube_config import (ConfigNode, FileOrData, KubeConfigLoader, - _create_temp_file_with_content, _cleanup_temp_files) + _cleanup_temp_files, _create_temp_file_with_content) NON_EXISTING_FILE = "zz_non_existing_file_472398324" diff --git a/scripts/__init__.py b/scripts/__init__.py index aa331f6c7b..3fd54b0c56 100644 --- a/scripts/__init__.py +++ b/scripts/__init__.py @@ -9,8 +9,7 @@ # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and +# See the License for the specific language governing permissions and # limitations under the License. # Intentional empty init file to make this folder a python package -