Skip to content

add support for reporting multiple k8s envs with one reporter command#688

Merged
sami-alajrami merged 1 commit intomainfrom
feature/k8s-multi-env-config
Mar 9, 2026
Merged

add support for reporting multiple k8s envs with one reporter command#688
sami-alajrami merged 1 commit intomainfrom
feature/k8s-multi-env-config

Conversation

@sami-alajrami
Copy link
Contributor

@sami-alajrami sami-alajrami commented Mar 9, 2026

Summary

related to kosli-dev/server#4720
Adds a --config-file flag to kosli snapshot k8s that 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-file is provided, the command reads a YAML file with an environments list, where each entry specifies:

  • name — the Kosli environment name
  • namespaces / namespacesRegex — namespaces to include
  • excludeNamespaces / excludeNamespacesRegex — namespaces to exclude

Example config file:

environments:
  - name: prod-env
    namespaces: [prod-ns1, prod-ns2]
  - name: staging-env
    namespacesRegex: ["^staging-.*"]
  - name: infra-env
    excludeNamespaces: [prod-ns1, prod-ns2, default]

The --config-file flag is mutually exclusive with the positional environment name argument and with any namespace flags (--namespaces, --exclude-namespaces, etc.).

Note: --config-file shadows the global Kosli --config-file persistent flag for this subcommand. The global Kosli config can still be set via the KOSLI_CONFIG_FILE environment variable.

Changes

  • New k8sSnapshotConfig / k8sEnvironmentConfig structs and YAML parsing logic
  • Config validation: at least one environment required, no duplicate names, no mixing of include and exclude filters, valid regex patterns
  • New runMultiEnv method that reports each environment, collecting and surfacing all errors at the end
  • Refactored run to share reportEnvironment logic with the multi-env path
  • Comprehensive unit and CLI tests, with testdata fixture files covering valid and invalid config scenarios

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 reportEnvironment path 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.

Comment on lines +201 to +204
if o.configFilePath != "" {
return o.runMultiEnv(clientset)
}
return o.run(clientset, args)
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--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.

Copilot uses AI. Check for mistakes.
return nil
}

return MuXRequiredFlags(cmd, []string{"namespaces", "exclude-namespaces"}, false)
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Comment on lines +171 to +173
if !hasConfigFile && len(args) > 1 {
return fmt.Errorf("accepts at most 1 arg(s), received %d", len(args))
}
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

@mbevc1 mbevc1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Do we need to amend Helm chart to support new config as well?

@sami-alajrami sami-alajrami merged commit 7fb3f77 into main Mar 9, 2026
14 checks passed
@sami-alajrami sami-alajrami deleted the feature/k8s-multi-env-config branch March 9, 2026 09:22
@sami-alajrami
Copy link
Contributor Author

yes, but will be done separately after releasing the CLI (since we need to update the CLI version in the chart)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants