kube

package
v0.17.4 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: AGPL-3.0 Imports: 31 Imported by: 0 modules, 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 kube talks to the Kubernetes API directly over HTTPS, from one kubeconfig context, without client-go. It covers what komando's cluster commands need: discovery, get/list, logs, patch, server-side apply, and exec and port-forward over WebSocket (stream.go).

Index

Constants

View Source
const DialTimeout = 30 * time.Second

DialTimeout limits a direct connection to the API server; without it an address that never answers waits for the system's own limit (75 s and more).

Variables

View Source
var ErrUncheckedLogin = errors.New("a login made from this computer's AWS or Google Cloud login goes to a cluster only over https with its certificate checked: take insecure-skip-tls-verify out of the kubeconfig (certificate-authority-data trusts the cluster's CA) and import it again")

ErrUncheckedLogin stops a token made from this computer's cloud login on its way to a server over plain http, or one whose certificate is not checked: the token is good for the cloud account, not only this cluster.

Functions

func Nearest

func Nearest(w string, known []string) string

Nearest is the word in known closest to w, or "" when none is close (more than a third of its letters differ).

Types

type APIError

type APIError struct {
	Code    int
	Reason  string
	Message string
}

APIError is an error status from the API server.

func (*APIError) Error

func (e *APIError) Error() string

type AuthInfo

type AuthInfo struct {
	ClientCertificate     string    `yaml:"client-certificate,omitempty"`
	ClientCertificateData string    `yaml:"client-certificate-data,omitempty"`
	ClientKey             string    `yaml:"client-key,omitempty"`
	ClientKeyData         string    `yaml:"client-key-data,omitempty"`
	Token                 string    `yaml:"token,omitempty"`
	TokenFile             string    `yaml:"tokenFile,omitempty"`
	Username              string    `yaml:"username,omitempty"`
	Password              string    `yaml:"password,omitempty"`
	Exec                  *ExecAuth `yaml:"exec,omitempty"`
}

type Client

type Client struct {
	Base      *url.URL
	Namespace string
	// Env is the environment a credential plugin runs with (the
	// command's); nil is this process's.
	Env []string
	// contains filtered or unexported fields
}

Client calls one cluster's API.

func New

func New(x *Context, dial Dialer) (*Client, error)

New builds a client for a context. dial may be nil (direct).

func (*Client) Do

func (c *Client) Do(ctx context.Context, method, path string, q url.Values, body []byte, contentType string) ([]byte, error)

Do sends a request; path is relative to the server ("/api/v1/...").

func (*Client) Exec

func (c *Client) Exec(ctx context.Context, o ExecOptions) error

Exec runs a command in a container and waits for it. A non-zero exit comes back as *ExitError.

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, q url.Values, v any) error

Get decodes a GET into v.

func (*Client) PortForward

func (c *Client) PortForward(ctx context.Context, namespace, pod string, port int, conn net.Conn) error

PortForward connects conn to port on a pod, until either side closes. One WebSocket carries one connection.

func (*Client) Resource

func (c *Client) Resource(ctx context.Context, name string) (ResourceType, error)

Resource finds a ResourceType by a name people type: "pods", "pod", "po", "deploy" or "deployments.apps".

func (*Client) ResourceFor

func (c *Client) ResourceFor(ctx context.Context, apiVersion, kind string) (ResourceType, error)

ResourceFor finds the ResourceType of an object (apiVersion + kind).

func (*Client) Resources

func (c *Client) Resources(ctx context.Context) ([]ResourceType, error)

Resources lists every resource type the cluster serves, once each.

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, path string, q url.Values) (io.ReadCloser, error)

Stream sends a GET and returns the body as it arrives (follow=true logs). The caller closes it.

func (*Client) Table

func (c *Client) Table(ctx context.Context, path string, q url.Values) (*Table, error)

Table asks for path as a table (with each row's metadata).

func (*Client) Watch

func (c *Client) Watch(ctx context.Context, path string, q url.Values) (io.ReadCloser, error)

Watch streams change events (one JSON object per line) for path.

type Cluster

type Cluster struct {
	Server                   string `yaml:"server"`
	CertificateAuthority     string `yaml:"certificate-authority,omitempty"`
	CertificateAuthorityData string `yaml:"certificate-authority-data,omitempty"`
	InsecureSkipTLSVerify    bool   `yaml:"insecure-skip-tls-verify,omitempty"`
	TLSServerName            string `yaml:"tls-server-name,omitempty"`
	ProxyURL                 string `yaml:"proxy-url,omitempty"`
}

type Config

type Config struct {
	CurrentContext string         `yaml:"current-context"`
	Clusters       []NamedCluster `yaml:"clusters"`
	Users          []NamedUser    `yaml:"users"`
	Contexts       []NamedContext `yaml:"contexts"`
}

Config is the part of a kubeconfig komando uses.

func Load added in v0.12.0

func Load(read func(path string) ([]byte, error), paths ...string) (*Config, error)

Load reads kubeconfig files the way kubectl reads a KUBECONFIG list: the first file that names a context, cluster or user wins, and so does the first current-context. Relative file paths in a file (a minikube or kind certificate) count from that file's folder, not the current one. read reads one file; "-" is stdin and keeps paths as they are.

func Parse

func Parse(b []byte) (*Config, error)

Parse reads a kubeconfig.

func (*Config) ContextNames

func (c *Config) ContextNames() []string

ContextNames lists the context names in file order.

func (*Config) Resolve

func (c *Config) Resolve(name string) (*Context, error)

Resolve finds a context ("" = the current one) and its cluster and user.

type Context

type Context struct {
	Name      string
	Cluster   Cluster
	User      AuthInfo
	Namespace string
}

Context is one resolved kubeconfig context.

func (*Context) CloudLogin added in v0.15.0

func (x *Context) CloudLogin() *ExecAuth

CloudLogin is the plugin whose token komando makes itself from this computer's AWS or Google Cloud login (Builtin), or nil. No program runs, but a credential of this computer goes to the context's server.

func (*Context) Plugin added in v0.3.0

func (x *Context) Plugin() *ExecAuth

Plugin is the credential plugin that logging in to this context runs as a program on this computer, or nil: none, a static token, or one of the plugins komando makes the token for itself (Builtin).

func (*Context) Standalone

func (x *Context) Standalone(read func(path string) ([]byte, error)) ([]byte, error)

Standalone returns a kubeconfig with only this context, files read into the *-data fields, so it can live in the vault and travel. The files the kubeconfig names (CA, client certificate and key, token) are read with read, so the caller's rules for local files apply to them as they do to the kubeconfig itself.

type Dialer

type Dialer func(ctx context.Context, network, addr string) (net.Conn, error)

Dialer opens TCP connections, possibly through a --via path.

type ExecAuth

type ExecAuth struct {
	APIVersion string   `yaml:"apiVersion,omitempty"`
	Command    string   `yaml:"command"`
	Args       []string `yaml:"args,omitempty"`
	Env        []struct {
		Name  string `yaml:"name"`
		Value string `yaml:"value"`
	} `yaml:"env,omitempty"`
	// InstallHint is what the kubeconfig says to do when Command is not
	// installed (kubectl prints it too).
	InstallHint string `yaml:"installHint,omitempty"`
	// SearchPath, when set, is the PATH the program runs with instead of
	// the caller's (the cli sets it for a program a person trusted, so an
	// AI agent's PATH does not pick the program's helpers); the variables
	// that steer a program are then left out too (pluginEnv).
	SearchPath string `yaml:"-"`
	// Check, when set, runs right before the program starts; an error
	// stops the login (the cli checks again that the file is the one a
	// person trusted).
	Check func() error `yaml:"-"`
}

ExecAuth is a credential plugin (aws eks get-token, gke-gcloud-auth-plugin, ...).

func (*ExecAuth) Builtin added in v0.3.0

func (e *ExecAuth) Builtin() bool

Builtin reports whether komando makes this plugin's token itself (builtinToken), so its command never runs.

type ExecOptions

type ExecOptions struct {
	Namespace, Pod, Container string
	Command                   []string
	Stdin                     io.Reader
	Stdout, Stderr            io.Writer
	TTY                       bool
	Resize                    <-chan TermSize // only with TTY
}

ExecOptions describe one command in a container.

type ExitError

type ExitError struct{ Code int }

ExitError is a command that ended with a non-zero exit code.

func (*ExitError) Error

func (e *ExitError) Error() string

type NamedCluster added in v0.12.0

type NamedCluster struct {
	Name    string  `yaml:"name"`
	Cluster Cluster `yaml:"cluster"`
}

type NamedContext added in v0.12.0

type NamedContext struct {
	Name    string `yaml:"name"`
	Context struct {
		Cluster   string `yaml:"cluster"`
		User      string `yaml:"user"`
		Namespace string `yaml:"namespace,omitempty"`
	} `yaml:"context"`
}

type NamedUser added in v0.12.0

type NamedUser struct {
	Name string   `yaml:"name"`
	User AuthInfo `yaml:"user"`
}

type PluginError added in v0.13.0

type PluginError struct {
	Command     string
	InstallHint string
	Missing     bool
	Detail      string // the plugin's own words (stderr), or why its output was no token
}

PluginError is a credential plugin that could not log in: its program is not on this computer (Missing), it failed, or it printed no token.

func (*PluginError) Error added in v0.13.0

func (e *PluginError) Error() string

type ResourceType

type ResourceType struct {
	Path       string // "/api/v1" or "/apis/apps/v1"
	Name       string // plural: deployments
	Kind       string
	Namespaced bool
	ShortNames []string
}

func (ResourceType) APIVersion

func (r ResourceType) APIVersion() string

APIVersion is what objects of r carry in apiVersion ("v1", "apps/v1").

func (ResourceType) Group

func (r ResourceType) Group() string

Group is the API group of r ("" for the core group, "apps", ...).

func (ResourceType) IsNamespaced

func (r ResourceType) IsNamespaced() bool

func (ResourceType) KindName

func (r ResourceType) KindName() string

func (ResourceType) ObjectPath

func (r ResourceType) ObjectPath(ns, name string) string

ObjectPath is the API path of a ResourceType (all namespaces when ns is "").

func (ResourceType) Version

func (r ResourceType) Version() string

Version is r's API version ("v1", ...).

type Table

type Table struct {
	ColumnDefinitions []struct {
		Name     string `json:"name"`
		Priority int    `json:"priority"`
	} `json:"columnDefinitions"`
	Rows []struct {
		Cells  []any `json:"cells"`
		Object struct {
			Metadata struct {
				Namespace string `json:"namespace"`
			} `json:"metadata"`
		} `json:"object"`
	} `json:"rows"`
}

Table is the server-side table the API prints for a list or an object, the same columns kubectl shows.

type TermSize

type TermSize struct{ Width, Height uint16 }

TermSize is a terminal's size in characters.

type UnknownKindError

type UnknownKindError struct {
	Kind       string
	APIVersion string
	Suggest    string
}

UnknownKindError is a kind the cluster does not serve. APIVersion is set for an object's apiVersion and kind (ResourceFor).

func (*UnknownKindError) Error

func (e *UnknownKindError) Error() string

Jump to

Keyboard shortcuts

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