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 ¶
- Variables
- func ClusterServers(paths []string, name string) []string
- func CurrentContext(paths []string) (string, error)
- func Lookup(paths []string, contextName string) (server, proxyURL string, err error)
- func ReadByDefault(path string) bool
- func ResolvePath(flag string) (string, error)
- func SearchPaths(flag string) ([]string, error)
- func Shadowed(path string) bool
- func UniqueFiles(paths []string) []string
- type Editor
- type Entry
- type ExecUser
- type Holding
- type Removal
- type Target
Constants ¶
This section is empty.
Variables ¶
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
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
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 ¶
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 ¶
ReadByDefault reports whether kubectl, run without --kubeconfig, reads path.
func ResolvePath ¶
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 ¶
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 ¶
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
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 ¶
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 ¶
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 ¶
Close releases the write lock. It is safe to call more than once, and it never removes a lock this Editor did not create.
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 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.
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
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.