Browse Source

Second commit

master
Elia El Lazkani 4 years ago
commit
14b5af34c9
  1. 37
      README.rst
  2. 147
      get-k8s-resources.py
  3. 1
      requirements.txt

37
README.rst

@ -0,0 +1,37 @@
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.
Usage
-----
::
$ get-k8s-resources -h
usage: get-k8s-resources.py [-h] [-n NAMESPACE] [-c CONTEXT] -t
{deployments,services,pods,ingresses}
Returns a json list of kubernetes deployments.
optional arguments:
-h, --help show this help message and exit
-n NAMESPACE, --namespace NAMESPACE
filter by namespace
-c CONTEXT, --context CONTEXT
context to use
-t {deployments,services,pods,ingresses}, --type {deployments,services,pods,ingresses}
type of kubernetes resource to get
--kubeconfig KUBECONFIG
path to the kubeconfig
Requirements
------------
The requirements can be found in the `requirements.txt` file in this repository.
Configuration
-------------
By default, this tool will try to find your kubeconfig in the default paths where `kubectl` looks for. It will also try to take assumptions on which `context` it should be using if there are multiple contexts in the same configuration. Generally, it should work as shown in the help menu.

147
get-k8s-resources.py

@ -0,0 +1,147 @@
#!/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()

1
requirements.txt

@ -0,0 +1 @@
kubernetes
Loading…
Cancel
Save