Reformatting code and formatting output to JSON

This commit is contained in:
Elia El Lazkani 2019-03-09 20:59:19 -05:00
parent 99506725ab
commit ec2cb57d96
2 changed files with 13 additions and 7 deletions

View file

@ -1,7 +1,7 @@
Get K8s Resources 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 Usage
----- -----

View file

@ -31,6 +31,7 @@
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
import sys import sys
import json
import argparse import argparse
from kubernetes import client, config from kubernetes import client, config
@ -43,7 +44,7 @@ def get_deployments(api, namespace):
else: else:
deployments = api.list_deployment_for_all_namespaces() deployments = api.list_deployment_for_all_namespaces()
print_list(deployments) return deployments
def get_pods(api, namespace): def get_pods(api, namespace):
@ -52,7 +53,7 @@ def get_pods(api, namespace):
else: else:
pods = api.list_pod_for_all_namespaces() pods = api.list_pod_for_all_namespaces()
print_list(pods) return pods
def get_services(api, namespace): def get_services(api, namespace):
@ -61,7 +62,7 @@ def get_services(api, namespace):
else: else:
services = api.list_service_for_all_namespaces() services = api.list_service_for_all_namespaces()
print_list(services) return services
def get_ingresses(api, namespace): def get_ingresses(api, namespace):
@ -70,15 +71,19 @@ def get_ingresses(api, namespace):
else: else:
ingresses = api.list_ingress_for_all_namespaces() 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): def print_list(k8s_object_list):
_k8s_object_list = [] _k8s_object_list = []
for k8s_object in k8s_object_list.items: for k8s_object in k8s_object_list.items:
_k8s_object_list.append(k8s_object.metadata.name) _k8s_object_list.append(k8s_object.metadata.name)
print(_k8s_object_list) print(json_dump(_k8s_object_list))
def get_extensions(api): def get_extensions(api):
@ -125,7 +130,8 @@ def main():
api = None api = None
if parser.type in k8s_types.keys(): if parser.type in k8s_types.keys():
api = k8s_types[parser.type]["api"](_client) 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(): def argumentparser():