feat: 'kportal generate' subcommand to bulk-add forwards from a cluster

New subcommand that connects to a chosen kube context and walks the
user through three picker steps before writing forwards to the config:

  1. Namespace multi-select (no system-namespace exclusion)
  2. Service multi-select grouped by namespace, with already-configured
     rows greyed out and locked off
  3. Starting-port input with a live preview of consecutive local-port
     assignments, skipping any port already taken by the existing config
     or another row in the batch

Multi-port services emit one forward per port. Non-TCP ports are
skipped with a one-line warning at the end (kportal forward layer is
TCP-only). --dry-run prints the planned forwards without writing.

Subcommand dispatch lives in cmd/kportal/main.go: when os.Args[1] ==
'generate', runGenerate(os.Args[2:]) is invoked before the main
flag.Parse() so the flag.NewFlagSet 'generate' parses its own
'--context', '--config', '--dry-run' flags. Invalid contexts list
the available ones for quick correction.

Tests in internal/ui/generate_test.go cover:
  - namespace toggle / toggle-all / filter
  - service multi-select with locked already-configured rows and
    non-TCP filtering
  - port-collision-aware consecutive assignment using a real Mutator
    against a tempdir config
  - reject + recover for starting-port < 1024
  - dry-run does not invoke the mutator
  - end-to-end Update() walk through the three steps
  - parse-starting-port boundary table
  - port-step view rendering
  - ServiceCandidate.Key() determinism

README updated with a 'Generate Forwards from a Cluster' section
describing the flow and the three flags.
This commit is contained in:
2026-05-06 13:09:12 +01:00
parent e02edb68ef
commit f4adeedb8f
5 changed files with 1705 additions and 0 deletions
+26
View File
@@ -273,6 +273,32 @@ kportal --check
kportal -c /path/to/config.yaml
```
### Generate Forwards from a Cluster
The `generate` subcommand discovers services in a Kubernetes context and lets you
interactively pick which ones to forward. Selected entries are appended to the
config file with consecutive local ports starting from a value you choose.
```bash
kportal generate --context=my-cluster
kportal generate --context=my-cluster --config=/path/to/.kportal.yaml
kportal generate --context=my-cluster --dry-run
```
| Flag | Description |
|------|-------------|
| `--context` | (required) Kubernetes context to scan |
| `--config` | Path to kportal config file (default: `.kportal.yaml`) |
| `--dry-run` | Print the planned forwards but do not modify the config |
The interactive flow has three steps:
1. **Namespaces** — multi-select with `space`, toggle-all with `a`, filter with `/`.
2. **Services** — same controls; rows already present in the config are locked off, and non-TCP ports are skipped (UDP is not supported by kportal's forward layer).
3. **Port assignment** — choose a starting local port (default `10000`, must be ≥ `1024`). Local ports are assigned consecutively in stable order, skipping any already in use.
Press `enter` on the final step to save (or to print and exit when `--dry-run` is set), `b` to go back, or `esc` to cancel.
## Status Indicators
| Indicator | Description |