diff --git a/README.rst b/README.rst index 75d8b89..8396158 100644 --- a/README.rst +++ b/README.rst @@ -1,7 +1,7 @@ Get K8s Resources ================= -The `get_k8s_resources` script is a small script that will return a list of all instances of a certain kubernetes type. +The `get_k8s_resources` script is a small script that will return a list of all instances of a certain kubernetes type `JSON` formatted. Usage ----- diff --git a/get-k8s-resources.py b/get-k8s-resources.py index 20b0d23..e082ded 100644 --- a/get-k8s-resources.py +++ b/get-k8s-resources.py @@ -31,6 +31,7 @@ # POSSIBILITY OF SUCH DAMAGE. import sys +import json import argparse from kubernetes import client, config @@ -43,7 +44,7 @@ def get_deployments(api, namespace): else: deployments = api.list_deployment_for_all_namespaces() - print_list(deployments) + return deployments def get_pods(api, namespace): @@ -52,7 +53,7 @@ def get_pods(api, namespace): else: pods = api.list_pod_for_all_namespaces() - print_list(pods) + return pods def get_services(api, namespace): @@ -61,7 +62,7 @@ def get_services(api, namespace): else: services = api.list_service_for_all_namespaces() - print_list(services) + return services def get_ingresses(api, namespace): @@ -70,15 +71,19 @@ def get_ingresses(api, namespace): else: ingresses = api.list_ingress_for_all_namespaces() - print_list(ingresses) + return ingresses +def json_dump(object_list): + return json.dumps(object_list) + + def print_list(k8s_object_list): _k8s_object_list = [] for k8s_object in k8s_object_list.items: _k8s_object_list.append(k8s_object.metadata.name) - print(_k8s_object_list) + print(json_dump(_k8s_object_list)) def get_extensions(api): @@ -125,7 +130,8 @@ def main(): api = None if parser.type in k8s_types.keys(): api = k8s_types[parser.type]["api"](_client) - k8s_types[parser.type]["function"](api, parser.namespace) + k8s_object_list = k8s_types[parser.type]["function"](api, parser.namespace) + print_list(k8s_object_list) def argumentparser():