add support for reporting multiple k8s envs with one reporter command#688
add support for reporting multiple k8s envs with one reporter command#688sami-alajrami merged 1 commit intomainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds multi-environment reporting support to kosli snapshot k8s via a new --config-file YAML input, enabling one command invocation to report multiple Kosli environments with different namespace selectors.
Changes:
- Added YAML config parsing + validation for multi-environment K8s snapshot reporting.
- Refactored snapshot reporting logic to share a
reportEnvironmentpath between single-env and multi-env execution. - Added CLI and unit tests plus YAML fixtures covering valid and invalid config scenarios.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| cmd/kosli/snapshotK8S.go | Implements --config-file, config parsing/validation, multi-env execution, and shared reporting logic. |
| cmd/kosli/snapshotK8S_test.go | Adds CLI validation tests and unit tests for config parsing/validation. |
| cmd/kosli/testdata/k8s-config/valid-single.yaml | Fixture: one environment with explicit namespaces. |
| cmd/kosli/testdata/k8s-config/valid-multi.yaml | Fixture: multiple environments with include/exclude/regex examples. |
| cmd/kosli/testdata/k8s-config/missing-name.yaml | Fixture: invalid env entry missing required name. |
| cmd/kosli/testdata/k8s-config/invalid-yaml.yaml | Fixture: invalid YAML parsing case. |
| cmd/kosli/testdata/k8s-config/invalid-regex.yaml | Fixture: invalid regex validation case. |
| cmd/kosli/testdata/k8s-config/empty-environments.yaml | Fixture: invalid empty environments list. |
| cmd/kosli/testdata/k8s-config/duplicate-names.yaml | Fixture: duplicate environment names validation case. |
| cmd/kosli/testdata/k8s-config/conflicting-filters.yaml | Fixture: include/exclude conflict validation case. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if o.configFilePath != "" { | ||
| return o.runMultiEnv(clientset) | ||
| } | ||
| return o.run(clientset, args) |
There was a problem hiding this comment.
--config-file mode detection is inconsistent: Args/PreRunE treat the flag as enabled when it is changed, but RunE switches to multi-env mode only when o.configFilePath != "". If a user passes --config-file="" (or otherwise sets an empty value), Args allows zero positional args but RunE will fall back to o.run(clientset, args) and args[0] will panic. Consider using a single predicate everywhere (e.g., o.configFilePath != "") and explicitly erroring when --config-file is provided with an empty value.
| return nil | ||
| } | ||
|
|
||
| return MuXRequiredFlags(cmd, []string{"namespaces", "exclude-namespaces"}, false) |
There was a problem hiding this comment.
The namespace include/exclude mutual-exclusion validation only checks --namespaces vs --exclude-namespaces. Users can still combine include flags (--namespaces-regex) with exclude flags (--exclude-namespaces / --exclude-namespaces-regex), but filters.ResourceFilterOptions.ShouldInclude is documented to be used for only one operation and will silently prioritize the exclude branch. Consider validating mutual exclusion across all include vs exclude namespace flags (similar to how snapshot ECS/Lambda validate include/exclude + regex combinations).
| if !hasConfigFile && len(args) > 1 { | ||
| return fmt.Errorf("accepts at most 1 arg(s), received %d", len(args)) | ||
| } |
There was a problem hiding this comment.
For the "too many args" case, the returned error message ("accepts at most 1 arg(s), received %d") differs from the Cobra ExactArgs(1) wording used across the CLI (e.g., "accepts 1 arg(s), received %d"). Keeping the phrasing consistent makes CLI errors easier to grep and keeps tests/messages uniform.
mbevc1
left a comment
There was a problem hiding this comment.
LGTM. Do we need to amend Helm chart to support new config as well?
|
yes, but will be done separately after releasing the CLI (since we need to update the CLI version in the chart) |
Summary
related to kosli-dev/server#4720
Adds a
--config-fileflag tokosli snapshot k8sthat allows reporting multiple Kosli environments in a single command invocation. Previously, users had to run the command once per environment; now they can define all environment-to-namespace mappings in a single YAML file.How it works
When
--config-fileis provided, the command reads a YAML file with anenvironmentslist, where each entry specifies:name— the Kosli environment namenamespaces/namespacesRegex— namespaces to includeexcludeNamespaces/excludeNamespacesRegex— namespaces to excludeExample config file:
The
--config-fileflag is mutually exclusive with the positional environment name argument and with any namespace flags (--namespaces,--exclude-namespaces, etc.).Changes
k8sSnapshotConfig/k8sEnvironmentConfigstructs and YAML parsing logicrunMultiEnvmethod that reports each environment, collecting and surfacing all errors at the endrunto sharereportEnvironmentlogic with the multi-env path