Add default kube config location

This commit is contained in:
mbohlool 2016-12-13 16:52:22 -08:00
parent 8d536e6642
commit 4d14edb757
8 changed files with 25 additions and 31 deletions

View File

@ -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

View File

@ -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:")

View File

@ -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

View File

@ -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" %

View File

@ -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)

View File

@ -24,6 +24,7 @@ from oauth2client.client import GoogleCredentials
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()

View File

@ -20,7 +20,7 @@ import unittest
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"

View File

@ -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