From 7556ac270dbf28be5418462f1c6125ee91b777c5 Mon Sep 17 00:00:00 2001 From: "Tai D (TaiD)" Date: Thu, 24 Mar 2022 08:22:14 -0400 Subject: [PATCH] [plugins/k8s][feat] Add argument to collect all contexts in config file (#727) This avoids the need to specify every context in order to have it imported. --- plugins/k8s/README.md | 2 ++ plugins/k8s/resoto_plugin_k8s/__init__.py | 6 ++++++ plugins/k8s/resoto_plugin_k8s/utils.py | 13 ++++++++++--- plugins/k8s/test/test_args.py | 1 + 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/plugins/k8s/README.md b/plugins/k8s/README.md index 8c1de0024e..d6e78509a1 100644 --- a/plugins/k8s/README.md +++ b/plugins/k8s/README.md @@ -6,6 +6,7 @@ It is meant as a starting point for K8S work but not intended for production use ## Usage When the collector is enabled (`--collector k8s`) it will automatically collect the current active context if any exists. Optionally a list of contexts to collect can be supplied using `--k8s-context contextA contextB contextC ...`. +To collect all contexts in the config file without having to specify each, use `--k8s-all-contexts` If a config file (`--k8s-config`) is supplied it will be used instead of the default `~/.kube/config`. Alternatively or in addition Kubernetes Clusters can be specified entirely on the commandline using e.g. @@ -41,4 +42,5 @@ For example if `clusterC` is provided in third place using `--k8s-cluster firstc --k8s-pool-size K8S_POOL_SIZE Kubernetes Thread Pool Size (default: 5) --k8s-fork Kubernetes use forked process instead of threads (default: False) + --k8s-all-contexts Kubernetes collect all contexts in kubeconfig file without needed to specify --k8s-context (default: False) ``` diff --git a/plugins/k8s/resoto_plugin_k8s/__init__.py b/plugins/k8s/resoto_plugin_k8s/__init__.py index c8221534ae..a6cce66c55 100644 --- a/plugins/k8s/resoto_plugin_k8s/__init__.py +++ b/plugins/k8s/resoto_plugin_k8s/__init__.py @@ -174,3 +174,9 @@ def add_args(arg_parser: ArgumentParser) -> None: dest="k8s_fork", action="store_true", ) + arg_parser.add_argument( + "--k8s-all-contexts", + help="Kubernetes collect all contexts in kubeconfig file without needed to specify --k8s-context", + dest="k8s_all_contexts", + action="store_true", + ) diff --git a/plugins/k8s/resoto_plugin_k8s/utils.py b/plugins/k8s/resoto_plugin_k8s/utils.py index e582cd7a5c..80bcabac47 100644 --- a/plugins/k8s/resoto_plugin_k8s/utils.py +++ b/plugins/k8s/resoto_plugin_k8s/utils.py @@ -46,7 +46,11 @@ def k8s_config() -> Dict: log.error(e) else: if contexts: - if ( + if ArgumentParser.args.k8s_all_contexts: + log.debug( + "importing all contexts in configuration file since --k8s-all-contexts was specified" + ) + elif ( len(ArgumentParser.args.k8s_context) == 0 and len(ArgumentParser.args.k8s_cluster) == 0 ): @@ -54,16 +58,19 @@ def k8s_config() -> Dict: log.debug( ( "no --k8s-context or --k8s-cluster specified, defaulting to" - f" active context {active_context}" + f" active context {active_context}. To import all contexts" + " in configuration file, use --k8s-all-contexts" ) ) else: active_context = None contexts = [context["name"] for context in contexts] + for context in contexts: if ( - context not in ArgumentParser.args.k8s_context + not ArgumentParser.args.k8s_all_contexts + and context not in ArgumentParser.args.k8s_context and context != active_context ): log.debug( diff --git a/plugins/k8s/test/test_args.py b/plugins/k8s/test/test_args.py index 0c9a726401..d644e48be4 100644 --- a/plugins/k8s/test/test_args.py +++ b/plugins/k8s/test/test_args.py @@ -16,3 +16,4 @@ def test_args(): assert len(ArgumentParser.args.k8s_no_collect) == 0 assert ArgumentParser.args.k8s_pool_size == 5 assert ArgumentParser.args.k8s_fork is False + assert ArgumentParser.args.k8s_all_contexts is False