kubeconfig

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 13 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 ClusterServers added in v1.19.0

func ClusterServers(paths []string, name string) []string

ClusterServers returns the server of every cluster named name in paths, in order and without repeats: the clusters Remove deletes, each of which may have been written for another workspace. A file that is missing or cannot be parsed is skipped.

func CurrentContext added in v1.19.0

func CurrentContext(paths []string) (string, error)

CurrentContext returns the current-context kubectl would use across paths: the first file that sets a non-empty one. A missing file is skipped and a malformed one fails the call, as in Lookup.

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.

func UniqueFiles added in v1.19.0

func UniqueFiles(paths []string) []string

UniqueFiles drops every path that names a file an earlier one already names, through another spelling or a symbolic link, keeping the order. Opening one file twice would find the lock the first open took.

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) Remove added in v1.19.0

func (ed *Editor) Remove(name string) Removal

Remove deletes every cluster, user and context named name. A current-context naming it is set to "", the value kubectl writes for a fresh config, so the file does not keep pointing at a context that is gone. Nothing reaches disk until Save.

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
	Exec                  *ExecUser
	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. The user authenticates with Exec when it is set and with Token otherwise.

type ExecUser added in v1.18.0

type ExecUser struct {
	Command string
	Args    []string
}

ExecUser is a credential plugin kubectl runs for a token.

type Holding added in v1.19.0

type Holding struct {
	Entry   bool // it has a cluster, user or context of that name
	Current bool // its current-context is that name
}

Holding is what one kubeconfig file holds of a name, read without its lock.

func Inspect added in v1.19.0

func Inspect(path, name string) (Holding, error)

Inspect reads what path holds of name. A missing file is an empty Holding; a file this package refuses to edit wraps ErrMalformed.

type Removal added in v1.19.0

type Removal struct {
	Changed        bool
	ClearedCurrent bool
	// StaticToken reports that the removed user carried a bearer token in the
	// file, which is what 'kube config' writes: no saved record names its
	// session, so the caller cannot close it.
	StaticToken bool
	// KeptUser is the user the removed context named under another name. Remove
	// leaves it, since another context or a hand edit may own it.
	KeptUser string
	// KeptUserToken reports that KeptUser carries a bearer token.
	KeptUserToken bool
	// Dangling lists the contexts left in the file that name the removed
	// cluster or user, such as one 'kubectl config rename-context' renamed.
	Dangling []string
}

Removal says what Remove changed.

type Target added in v1.19.0

type Target struct {
	// Server is the server of the cluster the context names, empty when no
	// file defines that cluster or the context names none.
	Server string
	// PluginCluster is the --cluster the context's user passes to
	// 'alpacon kube credentials', empty for a user that does not run it.
	PluginCluster string
	// StaticToken reports that the context's user carries a bearer token.
	StaticToken bool
}

Target is what a context points at once kubectl has merged the files.

func ContextTargets added in v1.19.0

func ContextTargets(paths []string, name string) []Target

ContextTargets returns, for each file in paths that defines the context name, in order, what that definition points at as kubectl merges paths: the cluster and user it names are taken from the first file that defines each, which need not be the file holding the context. The first Target is the context kubectl uses. With no context of that name anywhere, the cluster and user called name stand in for it. A file that is missing or cannot be parsed is skipped.

Jump to

Keyboard shortcuts

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