mirror of
https://github.com/lukaszraczylo/kportal.git
synced 2026-10-20 05:32:50 +00:00
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:
@@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -662,3 +663,237 @@ func TestMutator_Concurrent(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, cfg)
|
||||
}
|
||||
|
||||
// mutatorWithSharedPorts writes the issue #80 layout - the same alias and local
|
||||
// port in two contexts - and returns a mutator scoped to team-a.
|
||||
func mutatorWithSharedPorts(t *testing.T, active []string) (*Mutator, string) {
|
||||
t.Helper()
|
||||
|
||||
path := filepath.Join(t.TempDir(), ".kportal.yaml")
|
||||
body := `contexts:
|
||||
- name: team-a
|
||||
namespaces:
|
||||
- name: rate-service
|
||||
forwards:
|
||||
- resource: pod/rpc-server
|
||||
protocol: tcp
|
||||
port: 50051
|
||||
localPort: 3004
|
||||
alias: rate-service
|
||||
- resource: pod/keeper
|
||||
protocol: tcp
|
||||
port: 9000
|
||||
localPort: 3009
|
||||
alias: keeper-a
|
||||
- name: team-b
|
||||
namespaces:
|
||||
- name: rate-service
|
||||
forwards:
|
||||
- resource: pod/rpc-server
|
||||
protocol: tcp
|
||||
port: 50051
|
||||
localPort: 3004
|
||||
alias: rate-service
|
||||
- resource: pod/keeper
|
||||
protocol: tcp
|
||||
port: 9000
|
||||
localPort: 3009
|
||||
alias: keeper-b
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(body), 0600); err != nil {
|
||||
t.Fatalf("failed to write config: %v", err)
|
||||
}
|
||||
|
||||
m := NewMutator(path)
|
||||
m.SetActiveContexts(active)
|
||||
return m, path
|
||||
}
|
||||
|
||||
func forwardsForContext(t *testing.T, path, contextName string) []Forward {
|
||||
t.Helper()
|
||||
|
||||
cfg, err := LoadConfig(path)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to reload config: %v", err)
|
||||
}
|
||||
var out []Forward
|
||||
for _, fwd := range cfg.GetAllForwards() {
|
||||
if fwd.GetContext() == contextName {
|
||||
out = append(out, fwd)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// TestMutator_RemoveForwardByIDInContext_OnlyTouchesOneContext is the
|
||||
// regression test for the data-loss case that --context makes reachable:
|
||||
// Forward.ID() is just "alias:localPort" when an alias is set, so an unscoped
|
||||
// delete would strip the forward from every context at once.
|
||||
func TestMutator_RemoveForwardByIDInContext_OnlyTouchesOneContext(t *testing.T) {
|
||||
m, path := mutatorWithSharedPorts(t, []string{"team-a"})
|
||||
|
||||
if err := m.RemoveForwardByIDInContext("team-a", "rate-service:3004"); err != nil {
|
||||
t.Fatalf("RemoveForwardByIDInContext() failed: %v", err)
|
||||
}
|
||||
|
||||
// Only the shared-alias forward goes; team-a keeps its other one and
|
||||
// team-b is untouched entirely.
|
||||
teamA := forwardsForContext(t, path, "team-a")
|
||||
if len(teamA) != 1 || teamA[0].Alias != "keeper-a" {
|
||||
t.Errorf("team-a should keep only keeper-a, got %+v", teamA)
|
||||
}
|
||||
if got := forwardsForContext(t, path, "team-b"); len(got) != 2 {
|
||||
t.Errorf("team-b must be untouched, got %d forwards", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
// TestMutator_UpdateForwardInContext_OnlyTouchesOneContext guards the same
|
||||
// identity problem on the edit path.
|
||||
func TestMutator_UpdateForwardInContext_OnlyTouchesOneContext(t *testing.T) {
|
||||
m, path := mutatorWithSharedPorts(t, []string{"team-a"})
|
||||
|
||||
updated := Forward{
|
||||
Resource: "pod/rpc-server",
|
||||
Protocol: "tcp",
|
||||
Port: 50052,
|
||||
LocalPort: 3004,
|
||||
Alias: "rate-service",
|
||||
}
|
||||
if err := m.UpdateForwardInContext("team-a", "rate-service:3004", "team-a", "rate-service", updated); err != nil {
|
||||
t.Fatalf("UpdateForwardInContext() failed: %v", err)
|
||||
}
|
||||
|
||||
teamA := forwardsForContext(t, path, "team-a")
|
||||
if len(teamA) != 2 {
|
||||
t.Fatalf("team-a should still have two forwards, got %d", len(teamA))
|
||||
}
|
||||
|
||||
teamB := forwardsForContext(t, path, "team-b")
|
||||
if len(teamB) != 2 {
|
||||
t.Fatalf("team-b must keep both forwards, got %d", len(teamB))
|
||||
}
|
||||
for _, fwd := range teamB {
|
||||
if fwd.Alias == "rate-service" && fwd.Port != 50051 {
|
||||
t.Errorf("team-b remote port = %d, want 50051 (unchanged)", fwd.Port)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestMutator_AddForward_PortFreeInNonActiveContext covers the TUI add path for
|
||||
// a config that reuses ports across contexts.
|
||||
func TestMutator_AddForward_PortFreeInNonActiveContext(t *testing.T) {
|
||||
m, path := mutatorWithSharedPorts(t, []string{"team-a"})
|
||||
|
||||
// 3005 is unused anywhere; adding it to the active context must succeed
|
||||
// even though team-b exists with a colliding 3004 entry.
|
||||
fwd := Forward{Resource: "pod/other", Protocol: "tcp", Port: 8080, LocalPort: 3005, Alias: "other"}
|
||||
if err := m.AddForward("team-a", "rate-service", fwd); err != nil {
|
||||
t.Fatalf("AddForward() failed: %v", err)
|
||||
}
|
||||
|
||||
// The file must still hold both contexts and reload cleanly.
|
||||
cfg, err := LoadConfig(path)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to reload written config: %v", err)
|
||||
}
|
||||
if len(cfg.Contexts) != 2 {
|
||||
t.Errorf("written config lost a context: got %d, want 2", len(cfg.Contexts))
|
||||
}
|
||||
if got := len(forwardsForContext(t, path, "team-a")); got != 3 {
|
||||
t.Errorf("team-a should now have three forwards, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMutator_AddForward_PortTakenInActiveContext keeps the guard rail: a real
|
||||
// clash inside the running selection is still refused.
|
||||
func TestMutator_AddForward_PortTakenInActiveContext(t *testing.T) {
|
||||
m, _ := mutatorWithSharedPorts(t, []string{"team-a"})
|
||||
|
||||
fwd := Forward{Resource: "pod/other", Protocol: "tcp", Port: 8080, LocalPort: 3004, Alias: "other"}
|
||||
err := m.AddForward("team-a", "rate-service", fwd)
|
||||
if err == nil {
|
||||
t.Fatal("AddForward() should reject a port already used in the active context")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "3004") {
|
||||
t.Errorf("error should name the conflicting port, got %q", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMutator_AddForward_ValidatesNonActiveContexts closes the hole where
|
||||
// validating only the active selection would let an invalid forward reach disk.
|
||||
func TestMutator_AddForward_ValidatesNonActiveContexts(t *testing.T) {
|
||||
m, path := mutatorWithSharedPorts(t, []string{"team-a"})
|
||||
|
||||
// The wizard lets the user pick any kubeconfig context, including one that
|
||||
// is not currently being forwarded. It must still be validated.
|
||||
bad := Forward{Resource: "not-a-valid-resource-format", Protocol: "tcp", Port: 8080, LocalPort: 4000}
|
||||
if err := m.AddForward("team-c", "default", bad); err == nil {
|
||||
t.Fatal("AddForward() must validate forwards added to non-active contexts")
|
||||
}
|
||||
|
||||
// Nothing should have been written.
|
||||
cfg, err := LoadConfig(path)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to reload config: %v", err)
|
||||
}
|
||||
if len(cfg.Contexts) != 2 {
|
||||
t.Errorf("rejected add must not modify the file, got %d contexts", len(cfg.Contexts))
|
||||
}
|
||||
}
|
||||
|
||||
// TestMutator_RemoveForwardByID_UnscopedStillMatchesEveryContext documents that
|
||||
// the original entry point keeps its behaviour for callers that have no context.
|
||||
// The fixture uses distinct ports so it is valid with no selection at all.
|
||||
func TestMutator_RemoveForwardByID_UnscopedStillMatchesEveryContext(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), ".kportal.yaml")
|
||||
body := `contexts:
|
||||
- name: team-a
|
||||
namespaces:
|
||||
- name: default
|
||||
forwards:
|
||||
- resource: pod/rpc-server
|
||||
protocol: tcp
|
||||
port: 50051
|
||||
localPort: 3004
|
||||
alias: shared
|
||||
- resource: pod/keeper
|
||||
protocol: tcp
|
||||
port: 9000
|
||||
localPort: 3009
|
||||
alias: keeper-a
|
||||
- name: team-b
|
||||
namespaces:
|
||||
- name: default
|
||||
forwards:
|
||||
- resource: pod/rpc-server
|
||||
protocol: tcp
|
||||
port: 50051
|
||||
localPort: 3004
|
||||
alias: shared
|
||||
- resource: pod/keeper
|
||||
protocol: tcp
|
||||
port: 9000
|
||||
localPort: 3010
|
||||
alias: keeper-b
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(body), 0600); err != nil {
|
||||
t.Fatalf("failed to write config: %v", err)
|
||||
}
|
||||
m := NewMutator(path)
|
||||
// Scope to team-a so the shared 3004 entries are legal; the point of the
|
||||
// test is that the unscoped remove still matches both of them.
|
||||
m.SetActiveContexts([]string{"team-a"})
|
||||
|
||||
if err := m.RemoveForwardByID("shared:3004"); err != nil {
|
||||
t.Fatalf("RemoveForwardByID() failed: %v", err)
|
||||
}
|
||||
|
||||
cfg, err := LoadConfig(path)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to reload config: %v", err)
|
||||
}
|
||||
// Both rate-service entries go; only the two keeper forwards remain.
|
||||
if got := len(cfg.GetAllForwards()); got != 2 {
|
||||
t.Errorf("unscoped remove should match both contexts, %d forwards left", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user