Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 729d543

Browse files
authored
Merge pull request kubernetes-client#64 from mbohlool/c4
Add default kube config location
2 parents 8d536e6 + 4d14edb commit 729d543

File tree

8 files changed

+25
-31
lines changed

8 files changed

+25
-31
lines changed

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@ pip install kubernetes
2727
list all pods:
2828

2929
```python
30-
import os
31-
3230
from kubernetes import client, config
3331

3432
# Configs can be set in Configuration class directly or using helper utility
35-
config.load_kube_config(os.path.join(os.path.expanduser('~'), '.kube', 'config'))
33+
config.load_kube_config()
3634

3735
v1=client.CoreV1Api()
3836
print("Listing pods with their IPs:")
@@ -44,12 +42,10 @@ for i in ret.items:
4442
watch on namespace object:
4543

4644
```python
47-
import os
48-
4945
from kubernetes import client, config, watch
5046

5147
# Configs can be set in Configuration class directly or using helper utility
52-
config.load_kube_config(os.path.join(os.path.expanduser('~'), '.kube', 'config'))
48+
config.load_kube_config()
5349

5450
v1 = client.CoreV1Api()
5551
count = 10

examples/example1.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
16-
1715
from kubernetes import client, config
1816

1917

2018
def main():
2119
# Configs can be set in Configuration class directly or using helper
22-
# utility
23-
config.load_kube_config(
24-
os.path.join(os.path.expanduser('~'), '.kube', 'config'))
20+
# utility. If no argument provided, the config will be loaded from
21+
# default location.
22+
config.load_kube_config()
2523

2624
v1 = client.CoreV1Api()
2725
print("Listing pods with their IPs:")

examples/example2.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
16-
1715
from kubernetes import client, config, watch
1816

1917

2018
def main():
2119
# Configs can be set in Configuration class directly or using helper
22-
# utility
23-
config.load_kube_config(
24-
os.path.join(os.path.expanduser('~'), '.kube', 'config'))
20+
# utility. If no argument provided, the config will be loaded from
21+
# default location.
22+
config.load_kube_config()
2523

2624
v1 = client.CoreV1Api()
2725
count = 10

examples/example3.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
16-
1715
from kubernetes import client, config
1816

1917

2018
def main():
2119
# Configs can be set in Configuration class directly or using helper
22-
# utility
23-
config.load_kube_config(
24-
os.path.join(os.path.expanduser('~'), '.kube', 'config'))
20+
# utility. If no argument provided, the config will be loaded from
21+
# default location.
22+
config.load_kube_config()
2523

2624
print("Supported APIs (* is preferred version):")
2725
print("%-20s %s" %

examples/example4.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
16-
1715
from kubernetes import client, config
1816
from kubernetes.client import configuration
1917

2018

2119
def main():
22-
config_file = os.path.join(os.path.expanduser('~'), '.kube', 'config')
23-
contexts, active_context = config.list_kube_config_contexts(config_file)
20+
contexts, active_context = config.list_kube_config_contexts()
2421
if not contexts:
2522
print("Cannot find any context in kube-config file.")
2623
return
@@ -43,7 +40,7 @@ def main():
4340

4441
# Configs can be set in Configuration class directly or using helper
4542
# utility
46-
config.load_kube_config(config_file, context_name)
43+
config.load_kube_config(context=context_name)
4744

4845
print("Active host is %s" % configuration.host)
4946

kubernetes/config/kube_config.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from .config_exception import ConfigException
2626

27+
KUBE_CONFIG_DEFAULT_LOCATION = '~/.kube/config'
2728
_temp_files = {}
2829

2930

@@ -269,12 +270,16 @@ def _get_kube_config_loader_for_yaml_file(filename, **kwargs):
269270
**kwargs)
270271

271272

272-
def list_kube_config_contexts(config_file):
273+
def list_kube_config_contexts(config_file=None):
274+
275+
if config_file is None:
276+
config_file = os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION)
277+
273278
loader = _get_kube_config_loader_for_yaml_file(config_file)
274279
return loader.list_contexts(), loader.current_context
275280

276281

277-
def load_kube_config(config_file, context=None):
282+
def load_kube_config(config_file=None, context=None):
278283
"""Loads authentication and cluster information from kube-config file
279284
and stores them in kubernetes.client.configuration.
280285
@@ -283,5 +288,8 @@ def load_kube_config(config_file, context=None):
283288
from config file will be used.
284289
"""
285290

291+
if config_file is None:
292+
config_file = os.path.expanduser(KUBE_CONFIG_DEFAULT_LOCATION)
293+
286294
_get_kube_config_loader_for_yaml_file(
287295
config_file, active_context=context).load_and_set()

kubernetes/config/kube_config_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from .config_exception import ConfigException
2222
from .kube_config import (ConfigNode, FileOrData, KubeConfigLoader,
23-
_create_temp_file_with_content, _cleanup_temp_files)
23+
_cleanup_temp_files, _create_temp_file_with_content)
2424

2525
NON_EXISTING_FILE = "zz_non_existing_file_472398324"
2626

scripts/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
# Unless required by applicable law or agreed to in writing, software
1010
# distributed under the License is distributed on an "AS IS" BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
12+
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

1515
# Intentional empty init file to make this folder a python package
16-

0 commit comments

Comments
 (0)