get-k8s-resources/get-k8s-resources.py

148 lines
4.2 KiB
Python
Raw Normal View History

2019-03-07 23:41:45 +00:00
#!/usr/bin/env python
# Note: This license has also been called the "Simplified BSD License"
# and the "FreeBSD License". See also the 3-clause BSD License.
#
# Copyright 2019 Elijah Lazkani
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import sys
import argparse
from kubernetes import client, config
from kubernetes.config.config_exception import ConfigException
def get_deployments(api, namespace):
if namespace:
deployments = api.list_namespaced_deployment(namespace=namespace)
else:
deployments = api.list_deployment_for_all_namespaces()
print_list(deployments)
def get_pods(api, namespace):
if namespace:
pods = api.list_namespaced_pod(namespace=namespace)
else:
pods = api.list_pod_for_all_namespaces()
print_list(pods)
def get_services(api, namespace):
if namespace:
services = api.list_namespaced_service(namespace=namespace)
else:
services = api.list_service_for_all_namespaces()
print_list(services)
def get_ingresses(api, namespace):
if namespace:
ingresses = api.list_namespaced_ingress(namespace=namespace)
else:
ingresses = api.list_ingress_for_all_namespaces()
print_list(ingresses)
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)
def get_client_extensions(api):
return client.ExtensionsV1beta1Api(api)
def get_core_api_v1(api):
return client.CoreV1Api(api)
def get_client(kubeconfig, context):
try:
return config.new_client_from_config(
config_file=kubeconfig, context=context
)
except ConfigException as e:
print(f"Error: {e}")
sys.exit(1)
k8s_types = {
"deployments": get_deployments,
"services": get_services,
"pods": get_pods,
"ingresses": get_ingresses,
}
def main():
parser = argumentparser()
_client = get_client(parser.kubeconfig, parser.context)
api = None
if parser.type in k8s_types.keys():
if parser.type in ["pods", "services"]:
api = get_core_api_v1(_client)
if parser.type in ["deployments", "ingresses"]:
api = get_client_extensions(_client)
k8s_types[parser.type](api, parser.namespace)
else:
print("No type provided, failing...")
sys.exit(1)
def argumentparser():
parser = argparse.ArgumentParser(
description="Returns a json list of kubernetes deployments."
)
parser.add_argument(
"-n", "--namespace", default=None, help="filter by namespace"
)
parser.add_argument("-c", "--context", default=None, help="context to use")
parser.add_argument(
"-t",
"--type",
required=True,
choices=k8s_types.keys(),
help="type of kubernetes resource to get",
)
parser.add_argument(
"--kubeconfig", default=None, help="path to the kubeconfig"
)
return parser.parse_args()
if __name__ == "__main__":
main()