mirror of
https://github.com/lukaszraczylo/kportal.git
synced 2026-07-10 06:52:00 +00:00
Add an UI to display the current port forwards status.
This commit is contained in:
@@ -9,6 +9,13 @@ import (
|
||||
"github.com/nvm/kportal/internal/k8s"
|
||||
)
|
||||
|
||||
// StatusUpdater is an interface for updating forward status
|
||||
type StatusUpdater interface {
|
||||
UpdateStatus(id string, status string)
|
||||
AddForward(id string, fwd *config.Forward)
|
||||
Remove(id string)
|
||||
}
|
||||
|
||||
// Manager orchestrates all port-forward workers.
|
||||
// It handles starting, stopping, and hot-reloading forwards.
|
||||
type Manager struct {
|
||||
@@ -20,6 +27,7 @@ type Manager struct {
|
||||
portChecker *PortChecker
|
||||
verbose bool
|
||||
currentConfig *config.Config
|
||||
statusUI StatusUpdater
|
||||
}
|
||||
|
||||
// NewManager creates a new forward Manager.
|
||||
@@ -42,6 +50,11 @@ func NewManager(verbose bool) *Manager {
|
||||
}
|
||||
}
|
||||
|
||||
// SetStatusUI sets the status updater for the manager
|
||||
func (m *Manager) SetStatusUI(ui StatusUpdater) {
|
||||
m.statusUI = ui
|
||||
}
|
||||
|
||||
// Start initializes and starts all port-forwards from the configuration.
|
||||
func (m *Manager) Start(cfg *config.Config) error {
|
||||
if cfg == nil {
|
||||
@@ -230,8 +243,13 @@ func (m *Manager) startWorker(fwd config.Forward) error {
|
||||
return fmt.Errorf("worker already exists for %s", fwd.ID())
|
||||
}
|
||||
|
||||
// Notify UI about new forward
|
||||
if m.statusUI != nil {
|
||||
m.statusUI.AddForward(fwd.ID(), &fwd)
|
||||
}
|
||||
|
||||
// Create and start worker
|
||||
worker := NewForwardWorker(fwd, m.portForwarder, m.verbose)
|
||||
worker := NewForwardWorker(fwd, m.portForwarder, m.verbose, m.statusUI)
|
||||
worker.Start()
|
||||
|
||||
// Store worker
|
||||
|
||||
@@ -22,10 +22,11 @@ type ForwardWorker struct {
|
||||
doneChan chan struct{}
|
||||
verbose bool
|
||||
lastPod string // Track the last pod we connected to
|
||||
statusUI StatusUpdater
|
||||
}
|
||||
|
||||
// NewForwardWorker creates a new ForwardWorker for a single forward configuration.
|
||||
func NewForwardWorker(fwd config.Forward, portForwarder *k8s.PortForwarder, verbose bool) *ForwardWorker {
|
||||
func NewForwardWorker(fwd config.Forward, portForwarder *k8s.PortForwarder, verbose bool, statusUI StatusUpdater) *ForwardWorker {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
return &ForwardWorker{
|
||||
@@ -36,6 +37,7 @@ func NewForwardWorker(fwd config.Forward, portForwarder *k8s.PortForwarder, verb
|
||||
stopChan: make(chan struct{}),
|
||||
doneChan: make(chan struct{}),
|
||||
verbose: verbose,
|
||||
statusUI: statusUI,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,6 +88,9 @@ func (w *ForwardWorker) run() {
|
||||
|
||||
// Check if pod changed (restart detected)
|
||||
if w.lastPod != "" && w.lastPod != podName {
|
||||
if w.statusUI != nil {
|
||||
w.statusUI.UpdateStatus(w.forward.ID(), "Reconnecting")
|
||||
}
|
||||
log.Printf("[%s] Switched to new pod: %s → %s", w.forward.ID(), w.lastPod, podName)
|
||||
} else if w.lastPod == "" {
|
||||
log.Printf("[%s] Forwarding %s → localhost:%d",
|
||||
@@ -94,6 +99,11 @@ func (w *ForwardWorker) run() {
|
||||
|
||||
w.lastPod = podName
|
||||
|
||||
// Update status to active
|
||||
if w.statusUI != nil {
|
||||
w.statusUI.UpdateStatus(w.forward.ID(), "Active")
|
||||
}
|
||||
|
||||
// Establish port-forward connection
|
||||
err = w.establishForward(podName)
|
||||
|
||||
@@ -104,6 +114,11 @@ func (w *ForwardWorker) run() {
|
||||
return
|
||||
}
|
||||
|
||||
// Update status to error
|
||||
if w.statusUI != nil {
|
||||
w.statusUI.UpdateStatus(w.forward.ID(), "Reconnecting")
|
||||
}
|
||||
|
||||
// Log the error
|
||||
log.Printf("[%s] Port-forward connection failed: %v", w.forward.ID(), err)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user