From 2958cf0195581cbdf4c1a142610666a2e1cabd33 Mon Sep 17 00:00:00 2001 From: Scott Lee Date: Fri, 13 Sep 2019 16:16:54 -0700 Subject: [PATCH] Address PR comments --- examples/README.md | 10 +++++----- examples/api_discovery.py | 4 ++-- examples/in_cluster_config.py | 4 ++-- examples/ingress_create.py | 2 +- examples/job_crud.py | 2 +- examples/multiple_clusters.py | 32 ++++++++++++++++--------------- examples/out_of_cluster_config.py | 2 +- examples/pod_exec.py | 2 +- examples/pod_namespace_watch.py | 2 +- 9 files changed, 31 insertions(+), 29 deletions(-) diff --git a/examples/README.md b/examples/README.md index 3fac49785..0c4d513e8 100644 --- a/examples/README.md +++ b/examples/README.md @@ -1,14 +1,14 @@ # Python Client Examples -This directory contains various examples how to use the Python client. Please -read the description at the top of each script for more information about what -it does and any prequisite steps. Most scripts also include comments throughout -the code. +This directory contains various examples of how to use the Python client. +Please read the description at the top of each example for more information +about what the script does and any prequisites. Most scripts also include +comments throughout the code. ## Setup These scripts require Python 2.7 or 3.5+ and the Kubernetes client which can be -installed via the directions +installed following the directions [here](https://github.com/kubernetes-client/python#installation). ## Contributions diff --git a/examples/api_discovery.py b/examples/api_discovery.py index 8c040ecc0..9c91fe429 100644 --- a/examples/api_discovery.py +++ b/examples/api_discovery.py @@ -13,8 +13,8 @@ # limitations under the License. """ -Reads the list of the available API versions and prints them. -Similar to running `kubectl api-versions`. +Reads the list of available API versions and prints them. Similar to running +`kubectl api-versions`. """ from kubernetes import client, config diff --git a/examples/in_cluster_config.py b/examples/in_cluster_config.py index 9aafbb27b..55f9eb792 100644 --- a/examples/in_cluster_config.py +++ b/examples/in_cluster_config.py @@ -13,13 +13,13 @@ # limitations under the License. """ -Showcases loading the Kubernetes config from within the cluster. This script +Shows how to load a Kubernetes config from within a cluster. This script must be run within a pod. You can start a pod with a Python image (for example, `python:latest`), exec into the pod, install the library, then run this example. If you get 403 errors from the API server you will have to configure RBAC to -add the permission to list pods by applying the following manifest: +add permission to list pods by applying the following manifest: --- kind: ClusterRole diff --git a/examples/ingress_create.py b/examples/ingress_create.py index cf7ca732e..fa5739c8f 100644 --- a/examples/ingress_create.py +++ b/examples/ingress_create.py @@ -14,7 +14,7 @@ """ Creates deployment, service, and ingress objects. The ingress allows external -network access within the cluster. +network access to the cluster. """ from kubernetes import client, config diff --git a/examples/job_crud.py b/examples/job_crud.py index 521050ee2..b18b152d4 100644 --- a/examples/job_crud.py +++ b/examples/job_crud.py @@ -13,7 +13,7 @@ # limitations under the License. """ -Creates, updates, and deletes a Job object. +Creates, updates, and deletes a job object. """ from os import path diff --git a/examples/multiple_clusters.py b/examples/multiple_clusters.py index 962639669..94b0458cd 100644 --- a/examples/multiple_clusters.py +++ b/examples/multiple_clusters.py @@ -30,23 +30,25 @@ def main(): return contexts = [context['name'] for context in contexts] active_index = contexts.index(active_context['name']) - option, _ = pick(contexts, title="Pick the context to load", - default_index=active_index) - # Configs can be set in Configuration class directly or using helper - # utility - config.load_kube_config(context=option) + cluster1, first_index = pick(contexts, title="Pick the first context", + default_index=active_index) + cluster2, _ = pick(contexts, title="Pick the second context", + default_index=first_index) - print("Active host is %s" % configuration.Configuration().host) + client1 = client.CoreV1Api( + api_client=config.new_client_from_config(context=cluster1)) + client2 = client.CoreV1Api( + api_client=config.new_client_from_config(context=cluster2)) - v1 = client.CoreV1Api() - print("Listing pods with their IPs:") - ret = v1.list_pod_for_all_namespaces(watch=False) - for item in ret.items: - print( - "%s\t%s\t%s" % - (item.status.pod_ip, - item.metadata.namespace, - item.metadata.name)) + print("\nList of pods on %s:" % cluster1) + for i in client1.list_pod_for_all_namespaces().items: + print("%s\t%s\t%s" % + (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) + + print("\n\nList of pods on %s:" % cluster2) + for i in client2.list_pod_for_all_namespaces().items: + print("%s\t%s\t%s" % + (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) if __name__ == '__main__': diff --git a/examples/out_of_cluster_config.py b/examples/out_of_cluster_config.py index be7d271ab..f391be236 100644 --- a/examples/out_of_cluster_config.py +++ b/examples/out_of_cluster_config.py @@ -13,7 +13,7 @@ # limitations under the License. """ -Showcases loading the Kubernetes config from outside of the cluster. +Shows how to load a Kubernetes config from outside of the cluster. """ from kubernetes import client, config diff --git a/examples/pod_exec.py b/examples/pod_exec.py index 4e6d85ded..98b717f4a 100644 --- a/examples/pod_exec.py +++ b/examples/pod_exec.py @@ -13,7 +13,7 @@ # limitations under the License. """ -Showcases the functionality of exec using a Busybox container. +Shows the functionality of exec using a Busybox container. """ import time diff --git a/examples/pod_namespace_watch.py b/examples/pod_namespace_watch.py index 5a87792ea..f09768cf7 100644 --- a/examples/pod_namespace_watch.py +++ b/examples/pod_namespace_watch.py @@ -15,7 +15,7 @@ """ Uses watch to print the stream of events from list namespaces and list pods. The script will wait for 10 events related to namespaces to occur within -the `timeout_seconds` threshold and then move on to waiting for 10 events +the `timeout_seconds` threshold and then move on to wait for another 10 events related to pods to occur within the `timeout_seconds` threshold. """