Introducing a new way to review your PRs.

Read the announcement

nklmilojevic / sofka

A Kubernetes TUI, reimagined in Rust - built on kube-rs and ratatui, async-first from the ground up.

View on GitHub

19 documents · Updated Nov 4, 2026

docs / kubeconfig-discovery-and-authentication

Kubeconfig, discovery, and authentication

Sofka uses kubeconfig as the source of cluster destinations and credentials. It builds one connection context for the selected cluster, learns the API types that the identity can reach, and uses that shared context for views, permission checks, and cluster commands.

Connection context

The connection context joins a kubeconfig selection with the live Kubernetes client and the metadata needed by the rest of the application. It keeps the active context name, cluster name, API server, namespace defaults, discovered resource catalog, and shell command context together so that a context switch changes the whole session coherently. See Kubernetes data integration for the wider data flow.

Kubeconfig sources

The application reads the file selected by KUBECONFIG, or the default kubeconfig location when that variable is not set. The --kubeconfig option sets KUBECONFIG for the full process, so Sofka and its kubectl commands use the same file. The file supplies cluster endpoints, context links, namespace hints, and authentication material such as tokens, certificates, or credential commands.

The application normalizes inline certificate data before it builds the client. It also removes empty token values and prevents credential helper commands from using the TUI terminal. These steps preserve the kubeconfig identity and trust rules while keeping authentication failures clear to the user.

References: src/main.rs• L58-73 src/main.rs• L168-174 src/…/kubeconfig.rs• L7-44 src/k8s.rs• L33-51

Context selection

Startup uses the kubeconfig current-context unless --context names a context. The interactive context picker reads all context names and can retry another context when the current API server is unavailable. If normal config inference cannot provide a client, a missing current context becomes an explicit selection error; successful in-cluster credentials can still connect without a named kubeconfig context.

Selecting a context rebuilds the client and runs discovery again. The application then resolves the new context's configuration and namespace before it starts a new resource view. This prevents data, aliases, and policy from the old cluster from remaining active after the switch.

References: src/main.rs• L274-313 src/k8s.rs• L414-457 src/…/pickers.rs• L579-629 src/…/pickers.rs• L810-873 src/…/pickers.rs• L879-949

Connection identity

The active connection exposes both a human-facing context name and the cluster destination behind it. The same context name is passed to kubectl shell-outs when it is known, so actions such as edit, describe, or port-forward target the cluster shown by Sofka. A server URL can also map back to a kubeconfig context for integrations that identify a cluster by its API endpoint; only contexts with a usable cluster server enter that mapping.

Offline connection information can still report the requested or current context, its kubeconfig cluster name, and its server URL before a live client exists. The server URL is destination metadata and does not carry credentials. For an in-cluster connection without a named context, Sofka keeps the live destination while leaving the kubectl context unset so kubectl can use its own default.

The client keeps kubeconfig authentication and TLS settings with the selected destination. Sofka can opt in to legacy X.509 v1 client certificates and can disable TLS session resumption for a run when a cluster or proxy does not handle resumed authenticated connections correctly.

References: src/k8s.rs• L278-315 src/k8s.rs• L334-393 src/k8s.rs• L548-615 src/k8s.rs• L1115-1141 src/…/proxy.rs• L6-35 src/main.rs• L67-73

The following flow shows how a kubeconfig choice becomes the shared context used by the application.

flowchart LR
    A[Kubeconfig file] --> B[Selected context]
    B --> C[Authenticated cluster client]
    C --> D[API discovery]
    D --> E[Resource and permission-aware session]
    E --> F[Views and cluster commands]

API discovery

API discovery turns the selected cluster's API description into the catalog used by the command palette and resource views. It supports built-in APIs, custom resources, and aggregated APIs without assuming that every cluster exposes the same types.

Discovery catalog

Discovery records each resource's group, version, kind, plural name, short names, scope, list capability, and scale capability. Sofka chooses one stable served version for a resource kind and keeps qualified names when groups share a plural or kind. The resulting catalog drives resource resolution, namespace lookup, completion, and the set of namespaced resources that can be opened.

Discovery also identifies listable namespaced resources as candidates for child views. This lets the application offer workload-to-child navigation only when the active cluster advertises the required resource and operation.

References: src/…/discovery.rs• L11-25 src/…/discovery.rs• L38-55 src/…/discovery.rs• L95-129 src/…/discovery.rs• L214-229 src/k8s.rs• L648-713

Discovery paths

Sofka first tries aggregated discovery and falls back to the older per-group discovery path when the server does not provide the aggregated format or the request fails. A failed optional API group is recorded as a warning and does not hide the resource types discovered from other groups. The core API group remains required because it provides the base Kubernetes resource set.

Discovery runs during startup and after every context switch. The server version is useful status data and does not block a usable connection when the optional version request fails.

References: src/…/discovery.rs• L25-37 src/…/discovery.rs• L58-93 src/…/discovery.rs• L133-172 src/k8s.rs• L487-545

Discovery results

The catalog is the boundary between cluster capability and interactive behavior. Resource names resolve to API resources only after discovery has registered them, and later data requests use the scope and API group recorded in that catalog. This is why a missing kind, a missing short name, or an unavailable custom API is usually a discovery issue before it is a table issue.

The startup check and runtime information report show the selected context, server, Kubernetes revision, number of discovered types, and groups that could not be read. They expose enough state to separate connection failure from partial discovery.

References: src/k8s.rs• L710-747 src/main.rs• L332-369 src/main.rs• L811-839

Authorization behavior

Kubernetes remains the final authority for every API request. Sofka adds user-facing checks and filters to make the current identity's limits clear, but it does not replace server-side authorization.

Access-aware resource views

When a resource view starts, Sofka may ask Kubernetes for a SelfSubjectRulesReview in the active namespace. If the review gives a complete list of listable resources, the command palette hides resources that the identity cannot list. An incomplete or unusable review leaves the full discovered catalog visible because delegated authorizers can return partial results. Opening a resource still sends the request to Kubernetes, which returns the actual access result.

This filter is therefore a navigation aid, not an access grant. It is tied to the active namespace and generation so a result from an older view cannot change a newer one.

References: src/…/lifecycle.rs• L560-622 src/…/lifecycle.rs• L1222-1228 docs/safety.md• L94-102

Permission checks

The :can-i feature asks Kubernetes to evaluate the current identity. The overview uses SelfSubjectRulesReview for a namespace-wide rule view, while a specific command checks one verb and resource with SelfSubjectAccessReview. Resource aliases are resolved through discovery before the specific check, so a check uses the correct plural and API group.

The result is shown as an answer for the selected namespace or as a warning when the review itself fails. A denied result remains a denial even when the response also contains an allowed signal.

References: src/…/authz.rs• L8-49 src/…/authz.rs• L52-124 docs/safety.md• L94-102

Read-only application policy

Read-only mode is an application-level safety policy that blocks actions that can change the cluster or run a mutating plugin. It can come from the command line or resolved configuration. A command-line choice applies for the whole session and takes precedence over per-context settings; otherwise the policy is re-resolved after a context switch.

Read-only mode is separate from Kubernetes authorization. A user can have Kubernetes permission to write and still be blocked by Sofka, or can be allowed by Sofka and still receive a Kubernetes denial from the server.

References: src/main.rs• L75-84 src/main.rs• L506-514 src/…/actions.rs• L2277-2284 src/…/pickers.rs• L929-943 docs/safety.md• L94-102

Context changes

A context change is a new connection lifecycle, not a label change on the current view. Sofka stops work for the old cluster, connects with the selected kubeconfig context, repeats discovery, and only then installs the new session state.

Context switch lifecycle

When an engineer selects a context, Sofka performs these steps:

  1. Stop watches and clear or preserve only the temporary view state needed during the transition.
  2. Load the named kubeconfig context and build its authenticated client.
  3. Run API discovery and collect the new cluster metadata.
  4. Resolve context- and cluster-specific application settings.
  5. Select the namespace and resource view for the new context.
  6. Start new watches against the new API resources.

The application tags asynchronous results with a generation. Results from the previous context are discarded, which prevents an old watch or permission review from updating the new cluster's view.

References: src/…/pickers.rs• L817-873 src/…/pickers.rs• L879-949 src/…/lifecycle.rs• L1842-1863 src/store.rs• L272-282

Context consistency

The active context is the shared identity for Kubernetes requests, kubectl commands, namespace history, and per-context application settings. The namespace can come from the selected context or saved session state according to configuration, but it is always resolved again when the context changes. This keeps the visible namespace and downstream commands aligned with the new cluster.

Related behavior appears in Resource tables and API proxy, which uses the connected client and discovered API resources to build data views.

References: src/k8s.rs• L278-315 src/k8s.rs• L1121-1139 src/…/pickers.rs• L934-942 src/…/pickers.rs• L155-162

Change checklist

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

  • Keep --kubeconfig, KUBECONFIG, context selection, and kubectl shell-outs on the same kubeconfig source.
  • Keep startup behavior distinct for successful in-cluster inference and failed inference with no current context.
  • Keep every context switch path aligned with client rebuild, discovery, generation changes, namespace resolution, and per-context policy reload.
  • Preserve the distinction between partial authorization reviews and confirmed access denial. An incomplete review must not hide the resource catalog.
  • Keep qualified API group names and selected discovery versions consistent with resource resolution and any downstream table or proxy request.
  • Keep authentication and TLS compatibility options active after context switches, not only during initial startup.

Important failure modes

TriggerConsequenceGuard
No current context and no usable inferred clientStartup cannot choose a kubeconfig cluster; interactive startup opens the context picker.Select a named context, set current-context, or provide working in-cluster credentials.
Kubeconfig parse failureThe picker reports an error instead of showing an empty context list.Fix the selected kubeconfig file or point --kubeconfig at a valid file.
Context points to a missing or server-less cluster entryThe context cannot provide a usable destination and is excluded from server-based context matching.Repair the cluster entry or select a context with a server URL.
Aggregated discovery is rejectedSofka uses per-group discovery and reports the fallback reason.Check discovery warnings and API group access.
Optional API group cannot be readSome resource types are absent from the catalog.Check the discovery warning and the identity's access to that group.
Core API discovery cannot be readConnection setup fails because the base resource catalog is unavailable.Restore API reachability or permission to read core discovery.
Authorization review is incompletePalette filtering is skipped, so inaccessible resources can remain visible until opened.Treat the Kubernetes response as authoritative and use :can-i for a direct check.
Auth credential helper needs terminal input or its session is expiredClient authentication fails without allowing the helper to take over the TUI.Log in with the credential provider before starting Sofka; for AWS SSO, run aws sso login.
TLS client certificate needs legacy handling or resumed sessions failRequests can fail with certificate or HTTP 401 errors.Use --allow-v1-client-cert or --no-tls-resumption for the affected run.

Key files

FileWhy to read it
src/k8s.rsDefines the connection context, startup and context-specific connections, discovery integration, offline identity metadata, and client-facing resource registry.
src/…/kubeconfig.rsShows kubeconfig inference and certificate-data normalization before client creation.
src/…/discovery.rsExplains the aggregated and legacy discovery paths and the capability data recorded for each resource.
src/…/proxy.rsShows how the selected kubeconfig context and proxy exclusions affect the API destination.
src/main.rsConnects command-line selection, headless checks, interactive fallback, and session-wide authentication or TLS options.
src/…/pickers.rsShows context listing, asynchronous context switching, and installation of the new cluster session.
src/…/lifecycle.rsShows namespace-scoped authorization filtering and generation-safe handling of asynchronous results.
src/…/authz.rsDefines the user-facing :can-i overview and single-action permission checks.
docs/safety.mdGives the user-facing meaning of authorization checks and their limits.
docs/debugging.mdExplains how discovery, authentication, watch, and diagnostic failures appear to users.