Skip to content

Commit

Permalink
[plugins/k8s][feat] Add argument to collect all contexts in config fi…
Browse files Browse the repository at this point in the history
…le (#727)

This avoids the need to specify every context in order to have it imported.
  • Loading branch information
tdickers authored Mar 24, 2022
1 parent cb325c9 commit 7556ac2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions plugins/k8s/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
```
6 changes: 6 additions & 0 deletions plugins/k8s/resoto_plugin_k8s/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
13 changes: 10 additions & 3 deletions plugins/k8s/resoto_plugin_k8s/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,31 @@ 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
):
active_context = active_context["name"]
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(
Expand Down
1 change: 1 addition & 0 deletions plugins/k8s/test/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 7556ac2

Please sign in to comment.