feat: add --context to forward only selected contexts (#85)

* feat: add --context to forward only selected contexts

kportal forwards every context in the config file. When the file describes
several clusters that host the same services, that means holding connections
open to clusters nobody is using, and - because all contexts are active at once
- every localPort has to be globally unique. The same service therefore lands on
a different local port per cluster, so switching clusters means reconfiguring
the app being developed.

--context limits a run to the contexts you name. It is repeatable and accepts
comma-separated values, and composes with -headless:

    kportal --context development-team-a
    kportal --context team-a,team-b -headless
    kportal --context team-a --context team-b

An unknown name is an error listing the contexts the file defines, so a typo
fails loudly instead of silently forwarding nothing.

Local-port and mDNS-hostname conflicts are now scoped to forwards that could run
at the same time: same context, or both contexts in the active selection. With
no selection every context is active, so existing behaviour is unchanged.
Contexts that are never selected together may reuse the same localPort, which is
what lets a service keep one fixed local port in every cluster.

Validation still covers the whole file, so a problem in a context outside the
selection is still reported and --check does not get weaker. Loading, validating
and filtering are shared by every path that re-reads the config (startup, file
watcher, SIGHUP in both headless and verbose modes), so no path can forget the
selection.

Also fixes a latent identity bug this makes reachable: Forward.ID() is only
"alias:localPort" when an alias is set, which was unique across contexts only
because duplicate local ports were globally rejected. With ports now shareable,
deleting or editing a forward from the TUI could have matched same-alias
forwards in other contexts and silently removed them. Delete and edit are now
scoped to the context the row came from.

* fix(config): keep the running config when no selected context remains

A reload dropped selected contexts that no longer exist in the file.
When all of them vanished, the reload returned an empty config and the
manager stopped every forward after a single rename.

LoadForRuntime now returns an error in that case. The watcher keeps the
previous config, as it does for any other invalid reload.

Refs #80

* fix(ui): apply new table columns after a successful SIGHUP reload

The context selection moved the SIGHUP reload into reloadFromSignal,
which had no way to update the verbose table columns. The function now
takes a callback that runs only after the manager reloads without error.
The verbose table passes SetColumns.

Refs #80

* style: gofmt run_test.go

---------

Co-authored-by: André Cruz <[email protected]>
This commit is contained in:
2026-10-02 02:33:29 +01:00
committed by GitHub
co-authored by André Cruz
parent 4748bc4be5
commit 4d263319ab
24 changed files with 1761 additions and 143 deletions
+121
View File
@@ -504,3 +504,124 @@ func TestWatcher_StopWithoutStart(t *testing.T) {
t.Fatal("Stop without start timed out")
}
}
// TestWatcher_WithContextSelection verifies that hot-reload honours the
// --context selection: a config whose non-selected contexts reuse local ports
// must reload cleanly, and the callback must only receive the selection.
func TestWatcher_WithContextSelection(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, ".kportal.yaml")
initial := `contexts:
- name: team-a
namespaces:
- name: default
forwards:
- resource: pod/app
port: 8080
localPort: 3004
- name: team-b
namespaces:
- name: default
forwards:
- resource: pod/app
port: 8080
localPort: 3004
`
require.NoError(t, os.WriteFile(configPath, []byte(initial), 0600))
var mu sync.Mutex
var receivedConfig *Config
callback := func(cfg *Config) error {
mu.Lock()
defer mu.Unlock()
receivedConfig = cfg
return nil
}
watcher, err := NewWatcher(configPath, callback, false, WithContextSelection([]string{"team-a"}))
require.NoError(t, err)
defer watcher.Stop()
watcher.Start()
time.Sleep(100 * time.Millisecond)
updated := `contexts:
- name: team-a
namespaces:
- name: default
forwards:
- resource: pod/app
port: 8080
localPort: 3004
- resource: pod/extra
port: 9090
localPort: 3005
- name: team-b
namespaces:
- name: default
forwards:
- resource: pod/app
port: 8080
localPort: 3004
`
require.NoError(t, os.WriteFile(configPath, []byte(updated), 0600))
require.Eventually(t, func() bool {
mu.Lock()
defer mu.Unlock()
return receivedConfig != nil
}, 3*time.Second, 50*time.Millisecond, "watcher should reload a config with ports shared across non-selected contexts")
mu.Lock()
defer mu.Unlock()
require.Len(t, receivedConfig.Contexts, 1, "callback should only see the selected context")
assert.Equal(t, "team-a", receivedConfig.Contexts[0].Name)
assert.Len(t, receivedConfig.GetAllForwards(), 2)
}
// TestWatcher_WithContextSelection_UnknownNameDoesNotFreezeReload covers a
// context renamed in the file: reload must keep working for the rest.
func TestWatcher_WithContextSelection_UnknownNameDoesNotFreezeReload(t *testing.T) {
tmpDir := t.TempDir()
configPath := filepath.Join(tmpDir, ".kportal.yaml")
initial := `contexts:
- name: team-a
namespaces:
- name: default
forwards:
- resource: pod/app
port: 8080
localPort: 3004
`
require.NoError(t, os.WriteFile(configPath, []byte(initial), 0600))
var mu sync.Mutex
var called bool
watcher, err := NewWatcher(configPath, func(cfg *Config) error {
mu.Lock()
defer mu.Unlock()
called = true
return nil
}, false, WithContextSelection([]string{"team-a", "since-renamed"}))
require.NoError(t, err)
defer watcher.Stop()
watcher.Start()
time.Sleep(100 * time.Millisecond)
updated := initial + ` - resource: pod/extra
port: 9090
localPort: 3005
`
require.NoError(t, os.WriteFile(configPath, []byte(updated), 0600))
require.Eventually(t, func() bool {
mu.Lock()
defer mu.Unlock()
return called
}, 3*time.Second, 50*time.Millisecond, "an unknown selected context must not freeze hot-reload")
}