kubeconfig

package
v1.16.1 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package kubeconfig writes one cluster, user and context into a kubeconfig file and reads them back. It edits the parsed YAML tree rather than a decoded struct, so entries, keys and comments it does not own survive the rewrite.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrLocked marks a kubeconfig another process holds the write lock on.
	ErrLocked = errors.New("kubeconfig is locked")

	// ErrMalformed marks a kubeconfig whose shape this package refuses to edit.
	ErrMalformed = errors.New("unsupported kubeconfig")

	// ErrContextNotFound and ErrClusterNotFound mark a Lookup miss.
	ErrContextNotFound = errors.New("context not found")
	ErrClusterNotFound = errors.New("cluster not found")
)

Functions

func Lookup

func Lookup(paths []string, contextName string) (server, proxyURL string, err error)

Lookup returns the server and proxy-url of the cluster that contextName points at, merging paths the way kubectl does: each name is taken from the first file that defines it, a missing file is skipped, and a malformed one fails the whole lookup. A context found nowhere wraps ErrContextNotFound; a cluster found nowhere wraps ErrClusterNotFound.

func ReadByDefault

func ReadByDefault(path string) bool

ReadByDefault reports whether kubectl, run without --kubeconfig, reads path.

func ResolvePath

func ResolvePath(flag string) (string, error)

ResolvePath picks the file 'kubectl config' itself would write to: the explicit path when one is given, then the first $KUBECONFIG entry that exists, else the last entry listed, and <home>/.kube/config when $KUBECONFIG names nothing.

func SearchPaths

func SearchPaths(flag string) ([]string, error)

SearchPaths returns the files kubectl reads, in the order it merges them: the explicit path alone when one is given, else every $KUBECONFIG entry, else <home>/.kube/config. A $KUBECONFIG that is set but names no file is an error: kubectl then reads nothing and does not fall back to the home file.

func Shadowed

func Shadowed(path string) bool

Shadowed reports whether path is a $KUBECONFIG entry listed after another one. kubectl takes current-context and each name from the first file that sets it, so what was written to path may not be what kubectl ends up using.

Types

type Editor

type Editor struct {
	// contains filtered or unexported fields
}

Editor holds a kubeconfig's write lock and its parsed contents between Open and Save, so a file that cannot be edited is found out before anything is created for it.

func Open

func Open(path string) (*Editor, error)

Open takes kubectl's write lock on path (<path>.lock, created exclusively, so a kubectl editing the file at the same time and this command exclude each other), then reads and checks the file. A missing, empty or comment-only file opens as a fresh config. Close must be called on the returned Editor.

func (*Editor) Apply

func (ed *Editor) Apply(e Entry) (previousContext string, replacedServer string)

Apply writes e into the parsed file. It returns the current-context it replaced (which may equal e.Name), and the host of the cluster's previous server when that differs from e.Server's, so the caller can say another endpoint's entry was overwritten. Nothing reaches disk until Save.

func (*Editor) Close

func (ed *Editor) Close() error

Close releases the write lock. It is safe to call more than once, and it never removes a lock this Editor did not create.

func (*Editor) Save

func (ed *Editor) Save() error

Save writes the edited file atomically in kubectl's layout: two-space indent, with list items flush under their key.

type Entry

type Entry struct {
	Name                  string
	Server                string
	ProxyURL              string // empty removes a proxy-url left by an earlier write
	Token                 string
	InsecureSkipTLSVerify bool
}

Entry is what Apply writes. Name is shared by the cluster, the user and the context, as one session yields exactly one of each.

Jump to

Keyboard shortcuts

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