8000 - Add context switch to kube config loader · mbohlool/client-python@b888000 · GitHub
[go: up one dir, main page]

Skip to content

Commit b888000

Browse files
committed
- Add context switch to kube config loader
- Refactor kube config loader to be able to test it - Add test for kube config loader
1 parent acc07d7 commit b888000

File tree

4 files changed

+741
-117
lines changed

4 files changed

+741
-117
lines changed

examples/example4.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2016 The Kubernetes Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
from kubernetes import client, config
18+
from kubernetes.client import configuration
19+
20+
21+
def main():
22+
config_file = os.path.join(os.path.expanduser('~'), '.kube', 'config')
23+
contexts, active_context = config.list_kube_config_contexts(config_file)
24+
if not contexts:
25+
print("Cannot find any context in kube-config file.")
26+
return
27+
contexts = [context['name'] for context in contexts]
28+
active_context = active_context['name']
29+
for i, context in enumerate(contexts):
30+
format_str = "%d. %s"
31+
if context == active_context:
32+
format_str = "* " + format_str
33+
print(format_str % (i, context))
34+
context = input("Enter context number: ")
35+
context = int(context)
36+
if context not in range(len(contexts)):
37+
print(
38+
"Number out of range. Using default context %s." %
39+
active_context)
40+
context_name = active_context
41+
else:
42+
context_name = contexts[context]
43+
44+
# Configs can be set in Configuration class directly or using helper
45+
# utility
46+
config.load_kube_config(config_file, context_name)
47+
48+
print("Active host is %s" % configuration.host)
49+
50+
v1 = client.CoreV1Api()
51+
print("Listing pods with their IPs:")
52+
ret = v1.list_pod_for_all_namespaces(watch=False)
53+
for item in ret.items:
54+
print(
55+
"%s\t%s\t%s" %
56+
(item.status.pod_ip,
57+
item.metadata.namespace,
58+
item.metadata.name))
59+
60+
61+
if __name__ == '__main__':
62+
main()

kubernetes/config/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414

1515
from .config_exception import ConfigException
1616
from .incluster_config import load_incluster_config
17+
from .kube_config import list_kube_config_contexts
1718
from .kube_config import load_kube_config

0 commit comments

Comments
 (0)
0