kubeconfig

package
v1.11.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package kubeconfig reports whether a Kubernetes client configuration can be resolved, without terminating the process when it cannot.

It exists because the shared client singletons in kubevela/pkg are built on controller-runtime's config.GetConfigOrDie, which calls os.Exit(1) when no kubeconfig is present. That is not a panic and not a returned error, so a caller cannot recover from it, log it, or fall back: the process is simply gone. Callers that are able to run without a cluster should consult Check before reaching for anything that resolves a client singleton.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssumeAvailable

func AssumeAvailable() (restore func())

AssumeAvailable records that a Kubernetes client configuration has been supplied directly, bypassing the environment. Call it alongside singleton.KubeConfig.Set, as the envtest harnesses do: a singleton that has already been populated never reaches GetConfigOrDie, so it cannot exit the process, and the work this package guards is safe to attempt.

This has to be recorded explicitly because the singleton exposes no way to ask whether it has been set. Without it, a suite that supplies an envtest config would still be told to skip, and would silently lose behaviour that depends on cluster access, such as loading external CUE packages.

The returned function restores the state the call displaced rather than clearing the assumption outright, so calls nest. Call it once the supplied configuration is no longer valid, typically when the harness tears its control plane down.

func AssumeUnavailable

func AssumeUnavailable() (restore func())

AssumeUnavailable withdraws any outstanding assumption until the returned function is called, so Check consults the environment again. It is the inverse of AssumeAvailable, for tests that exercise the skip path from inside a suite that has already supplied a config. Without it such a test has to clear the suite-wide assumption and remember to reinstate it by hand.

func Available

func Available() bool

Available reports whether Check succeeds. It is a convenience for call sites that only branch on the outcome and do not log the reason.

func Check

func Check() error

Check returns nil when using the shared client singletons is safe: either a configuration was supplied out of band (see AssumeAvailable) or a REST config can be resolved from the ambient environment (--kubeconfig, $KUBECONFIG, ~/.kube/config, or in-cluster service account). Otherwise it returns the resolution error.

It resolves the config exactly the way config.GetConfigOrDie does, but hands back the failure instead of exiting, so callers can decide what to do. A nil return does not promise the cluster is reachable, only that a configuration exists to describe it. Reachability still surfaces as an ordinary error on the first request.

func CheckFor

func CheckFor(purpose string) error

CheckFor returns the reason the shared client singletons are unsafe to use, naming the work about to be skipped, or nil when a configuration is available. It hands the reason back as well as reporting it, so a caller able to surface the failure in its own terms is not forced to re-derive it.

The report goes through klog.ErrorS rather than klog.Warningf because the vela CLI installs a klog sink (references/cli/log) whose Info path raises level 0 to 1 and gates on V(1), so a warning is dropped at the default verbosity. The CLI is the main consumer of this degraded path, and degrading silently would leave an operator with no explanation for why, say, external CUE packages are missing. The sink's Error path is not level-gated and writes to stderr, so the reason is visible without disturbing whatever the command prints on stdout.

Note that this inspects the ambient environment, not the client singletons. A process that populates singleton.KubeConfig directly, as the envtest suites do, has a working client even though nothing resolvable exists on disk, and will be told to skip unless it also calls AssumeAvailable. That is the safe direction to be wrong in: the singleton offers no way to ask whether it has already been set, and guessing wrong the other way exits the process.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL