Introducing a new way to review your PRs.

Read the announcement

hcavarsan / kftray

kubectl port-forward manager and reverse tunnel (ngrok-like) for exposing local services publicly, with TLS termination, HTTP traffic inspection, UDP forwarding, multi-hop proxy routing through k8s clusters, stateful config via filesystem or git - GUI and TUI available

View on GitHub

19 documents · Updated Nov 23, 2026

docs / desktop-native-orchestration

Native desktop orchestration

Native desktop orchestration connects the kftray web UI to the local services that manage Kubernetes port forwards, saved settings, windows, the system tray, and desktop tools. The Tauri application starts these services, exposes a safe command surface to the UI, and keeps native state in step with user actions.

System boundary

The Rust crate at crates/…/main.rs is the desktop entry point. It builds the Tauri application, loads shared state, starts background work, registers native commands, and runs the event loop. The library entry point at crates/…/lib.rs exposes the same native modules for other crate targets.

The web UI uses Tauri commands and events rather than accessing local services directly. The native layer owns work that needs local files, Kubernetes access, system processes, desktop windows, notifications, or the tray. See the desktop application page for the wider product view and desktop frontend for the UI side of this boundary.

Native request boundary

The command layer maps UI requests to product actions. Its groups cover port forwards, Kubernetes discovery, configuration data, settings, logs, SSL, updates, shortcuts, helper access, telemetry, and window state. The command list in the application entry point is the contract that makes these actions callable from the web UI.

The native layer also emits events when state changes outside a direct request. Configuration changes and active pod changes therefore reach the UI even when they come from a background check or another process. Configuration ownership and user workflows are described in configuration workflows.

References: crates/…/main.rs• L163-176 crates/…/main.rs• L412-531 crates/…/mod.rs• L1-18 crates/…/portforward.rs• L48-121

Application lifecycle

The application starts early platform safeguards before Tauri creates the webview. It then creates the database and settings context, restores native services, creates the tray, and enters the event loop. Most startup jobs run in the background so the UI can become usable while monitoring and service work continues.

The main stages are:

flowchart TD
    A[Process entry point] --> B[Platform safeguards]
    B --> C[Logger and shared runtime]
    C --> D[Tauri builder and app state]
    D --> E[Database and settings]
    E --> F[Port and service restoration]
    F --> G[Main window and tray]
    G --> H[Command and event loop]

Startup stages

Startup creates the local database and applies config migrations before it loads saved settings. It also starts shortcut handling, removes stale host entries, checks saved port-forward state, starts optional monitoring, and starts the optional MCP service. In production it can check for updates after launch when that setting is enabled.

The single-instance guard focuses an existing main window instead of opening a second application. Exit requests pass through the port-forward shutdown path so native resources and state have a chance to close together.

References: crates/…/main.rs• L113-161 crates/…/main.rs• L185-291 crates/…/main.rs• L293-355 crates/…/tray.rs• L476-493

Command layer

The command layer is the native service facade for the frontend. Each command group owns one product area while shared crates perform the deeper data, Kubernetes, port-forward, and security work.

Command areaNative responsibility
Port forwards and Kubernetes contextStart, stop, inspect, and restore forwarding work.
Configurations and config stateRead and change saved configs and their running state while protecting active forwards.
Window and settingsControl desktop state and user preferences.
Logs, telemetry, and diagnosticsProvide local support data and user-controlled reporting.
SSL, certificates, helper, and SSHConnect UI choices to local certificates, trust setup, privileges, and secure access.
Shortcuts and updaterManage global actions and application updates.
Server resources and MCPClean local or cluster resources and expose the optional local MCP service.

Configuration commands

Configuration commands manage the saved forwarding plans that the rest of the desktop app can start. They keep deletion separate from active forwarding and refresh SSL coverage when a successful change can alter the set of names used by certificates.

The command surface supports these user actions:

  • Create, read, update, and delete one or more saved configurations.
  • Import and export configuration data.
  • Add configurations for the pods behind a headless service.

An active forward blocks deletion until its native work is idle. Successful configuration changes can rebuild the shared SSL certificate and restart affected HTTPS forwards. A configuration with both automatic loopback allocation and a manual local address remains valid, but the automatic address takes precedence.

References: crates/…/config.rs• L30-38 crates/…/config.rs• L298-421

GitHub configuration sync

GitHub sync brings saved forwarding configurations from selected repository paths into the local configuration store. The command fetches the remote content, then either imports it alongside current data or replaces saved configurations only after active forwards and concurrent changes are checked.

GitHub credentials use the system keyring. When a custom KFTRAY_CONFIG directory is active, the keyring account includes its normalized path so separate configuration stores do not share credentials by accident. A flush sync also reloads registered shortcuts after import and preserves shortcut targets that still refer to retained configurations.

References: crates/…/github.rs• L79-127 crates/…/github.rs• L129-192

SSL settings and certificates

SSL commands provide the UI with one place to control encrypted forwarding and its certificates. Enabling SSL creates a certificate for all configured domains and can add its certificate authority to the system trust store. Disabling SSL removes the local SSL artifacts without making cleanup errors block the settings change.

Changing the enabled state also restarts running HTTPS forwards so they use the new certificate path. The restart uses the normal workload and protocol dispatch, retries a small number of times, and reports the affected configuration IDs if some forwards remain down. Saving other SSL settings does not restart forwards when the enabled state does not change.

Configuration changes use the same certificate coverage check. They avoid a restart when the current certificate already covers all domains, but they regenerate the certificate and restart affected HTTPS forwards when the domain set changes.

References: crates/…/ssl.rs• L29-166 crates/…/config.rs• L40-95 crates/…/config.rs• L104-295

Native action flow

When the UI invokes a command, the native layer validates the request, calls the shared service, and returns a result or an error. Background work uses the same service paths where possible, which keeps a button action, a shortcut, and startup restoration aligned.

sequenceDiagram
    participant UI as Web UI
    participant Tauri as Tauri command layer
    participant Service as Shared native service
    participant State as Local state or cluster
    UI->>Tauri: Invoke command
    Tauri->>Service: Validate and run action
    Service->>State: Read or change state
    State-->>Service: Result
    Service-->>Tauri: Result or error
    Tauri-->>UI: Command response
    Service-->>Tauri: Background state change
    Tauri-->>UI: Native event

References: crates/…/main.rs• L413-531 crates/…/portforward.rs• L144-161 crates/…/config.rs• L298-421 crates/…/ssl.rs• L29-166 crates/…/settings.rs• L39-117

Startup validation

Startup validation protects saved intent from stale local state, another live kftray process, or a cluster that is not ready. It checks what the saved state says, compares it with local ports and process ownership, and restores only work that this process can safely own.

Configuration directory checks

The application checks for more than one configuration directory and shows a warning when it finds multiple locations. This prevents a user from changing one config store while the application reads another.

References: crates/…/validation.rs• L17-47 crates/…/main.rs• L229-241

Port-forward restoration

The restore pass reads running config state and skips rows owned by another live kftray or kftui process. For each remaining row it checks the expected local port, then starts the forward when no conflicting process blocks it. Temporary connection, timeout, and pod-readiness errors use a bounded retry path; a user stop or a new owner cancels the pending restore.

This ownership check matters because startup must not clear or replace another process's live forward. A failed restore changes saved state only when the application has enough evidence that the row belongs to this process and the forward is not active.

References: crates/…/init_check.rs• L167-232 crates/…/init_check.rs• L234-280 crates/…/init_check.rs• L283-335 crates/…/init_check.rs• L337-473

Window and tray behavior

The desktop shell uses a main webview window and can create a separate log viewer window. The main window starts hidden and normally behaves as a tray popover. Users can switch to window mode when they want a normal taskbar window or when the platform has no usable tray.

Window state

Window state keeps the desktop shell usable across sessions and monitor layouts. The application saves size and position after a short delay, rejects positions outside available monitors, and falls back to a centered or tray-relative position when saved data is missing or invalid. Wayland lets the compositor manage position instead of forcing a saved coordinate.

Window focus, blur, close, pin, resize, and move events are part of the native policy. In tray mode an unfocused unpinned main window hides after a short delay. Closing the main window requests application shutdown rather than only closing the webview.

References: crates/…/tauri.conf.json• L7-23 crates/…/migrated.json• L5-44 crates/…/window.rs• L38-93 crates/…/window.rs• L148-204 crates/…/tray.rs• L345-474

Tray modes

Tray mode and window mode are the two effective display modes. A saved tray preference selects the preferred mode, but a missing Linux tray forces window mode. Switching mode updates the tray visibility, taskbar presence, window visibility, and macOS activation policy, then emits a mode event for the UI.

Tray activation toggles the main window. The native menu also supports pinning, position changes, log viewing, and quit. Linux uses the desktop StatusNotifier service and changes to window mode after a short grace period when that service is unavailable.

References: crates/…/tray_mode.rs• L20-28 crates/…/tray_mode.rs• L107-196 crates/…/tray.rs• L105-123 crates/…/tray.rs• L125-343 crates/…/tray_linux.rs• L71-91 crates/…/tray_linux.rs• L193-241

Platform initialization

Platform initialization removes differences that would otherwise cause blank windows, focus errors, or missing tray behavior. It runs before normal Tauri setup and selects platform-specific services without changing the product-level command model.

Linux display setup

Linux startup sets safe WebKit and GPU defaults only when the user has not set them. In an AppImage Wayland session it can restart the process with the host Wayland library when the bundled library is not suitable. On X11 it initializes thread support before any X11 work.

These steps address display and thread safety before GTK or the webview starts. They do not change the saved app mode or the command contract.

References: crates/…/main.rs• L59-73 crates/…/main.rs• L113-136 crates/…/appimage_wayland_fixup.rs• L34-88 crates/…/x11_init.rs• L8-18

Native service setup

The process prepares the local keyring and crypto provider before forwarding work begins. It also starts the optional network monitor and MCP server from saved settings. The MCP service listens on localhost and binds a new port before it replaces its prior listener. If the new port is unavailable, the existing server stays active.

References: crates/…/main.rs• L149-157 crates/…/main.rs• L264-277 crates/…/mcp.rs• L37-145

Native capabilities

Tauri capabilities define which native operations the webview may request. The migrated capability applies to the main and logs windows and grants only the desktop functions needed by this application, such as window control, dialogs, file access, notifications, global shortcuts, clipboard access, platform information, updates, and local shell actions.

The shell permission allows the AWS CLI and the bundled kftray-helper sidecar. This boundary lets the UI reach local support services without turning every webview action into unrestricted process access. The bundle configuration supplies the helper and packages the application for the supported desktop targets.

References: crates/…/migrated.json• L1-99 crates/…/tauri.conf.json• L33-73 crates/…/Cargo.toml• L44-57

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the command registration in main.rs, command modules, and the frontend invoke names aligned.
  • Keep emitted event names and payload shapes aligned with the UI listeners, especially config state and active pod changes.
  • Keep tray mode changes consistent across the saved app-mode setting, tray availability, taskbar state, and window visibility.
  • Keep the main and logs window labels aligned across Tauri config, capabilities, and native window lookup.
  • Keep shell and sidecar capability permissions aligned with helper and AWS CLI use.
  • Keep startup restore ownership checks aligned with config-state writes so one process cannot clear another process's forward.
  • Keep configuration deletion guards aligned with every path that can start or stop a forward, including shortcuts.
  • Keep SSL domain collection, certificate coverage checks, and HTTPS restart targets aligned when configuration fields change.
  • Keep SSL setting results clear about the difference between saved settings, certificate trust installation, and forwards that did not restart.
  • Keep GitHub credential account scoping aligned with custom configuration-directory selection so separate stores do not reuse credentials.
  • Keep GitHub flush-sync guards aligned with config-state writes and shortcut target updates so a sync cannot replace live or concurrently added configurations.

Important failure modes

TriggerConsequenceGuard
Multiple config directories existThe user may edit a config store that is not active.Show a startup warning with the detected locations.
A saved running row belongs to another live processStartup could duplicate or erase another process's forward.Skip foreign owners and preserve their state.
The cluster is not ready at loginA saved forward may fail even though it can work later.Retry transient failures with bounded backoff and cancel on changed intent.
Linux has no StatusNotifier tray serviceTray controls are not available.Force window mode and keep the main window usable.
Saved window coordinates are invalid or compositor-managedThe main window can open off-screen or fight the desktop compositor.Validate monitor bounds and use centering or compositor placement.
An MCP port change cannot bind the new portThe MCP service could stop even though the requested replacement is unusable.Bind the new listener first and keep the current server until the replacement is ready.
A native capability is missingThe related UI action fails at the Tauri boundary.Keep capability entries aligned with command and plugin use.
A configuration change adds or removes SSL domainsThe global certificate may not cover a running HTTPS forward.Recheck domain coverage and restart affected SSL forwards after a successful change.
SSL is enabled but certificate creation failsThe setting change is rejected and SSL stays disabled.Generate the global certificate before completing the enable operation.
An SSL state change cannot restart one or more forwardsSettings are saved but some HTTPS forwards remain down.Retry the restart and return the affected configuration IDs.
The system trust store rejects CA installationSSL settings are saved but clients may not trust the local certificate.Return a clear trust-install warning after the certificate work completes.
A GitHub flush sync overlaps a running forward or a newly saved configurationThe sync can fail instead of replacing data, or leave the user with stale repository content.Block replacement while forwards are active and ask the user to sync again when the saved set changed during the fetch.
Two custom configuration directories use the same keyring accountCredentials from one store can be read by another store.Scope the account by a normalized custom configuration path.

Key files

FileWhy to read it
crates/…/main.rsShows the application entry point, startup sequence, plugins, commands, and run loop.
crates/…/mod.rsShows the product areas exposed by the native command layer.
crates/…/config.rsShows configuration lifecycle guards and SSL reconciliation after config changes.
crates/…/github.rsShows GitHub configuration import, sync safety checks, shortcut refresh, and keyring scoping.
crates/…/ssl.rsShows the UI contract for SSL settings, certificate work, trust installation, and forward restart.
crates/…/portforward.rsShows the bridge from UI actions to port-forward work and change events.
crates/…/init_check.rsShows startup restore, port checks, retries, and process ownership rules.
crates/…/window.rsShows the main window position, size, focus, and visibility policy.
crates/…/tray_mode.rsShows the relationship between user mode preference and effective desktop mode.
crates/…/tray.rsShows cross-platform tray menus and window event handling.
crates/…/tray_linux.rsShows Linux tray availability and fallback behavior.
crates/…/mcp.rsShows localhost MCP startup, replacement, and shutdown behavior.
crates/…/migrated.jsonShows the native permissions granted to the desktop windows.
crates/…/tauri.conf.jsonShows the main window defaults, build link to the frontend, and bundle targets.