kubectl

package
v0.0.0-...-abaeddd Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 36 Imported by: ? 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 kubectl provides helpers for interacting with Kubernetes through kubectl.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(ctx context.Context, o ApplyOptions) (string, error)

Apply runs kubectl apply -f <file>.

func ApplyInline

func ApplyInline(ctx context.Context, o ApplyInlineOptions) (string, error)

ApplyInline runs kubectl apply -f - with Content on stdin. Content arrives resolved: an inline manifest is part of its expectation, whose ${NAME} is expanded before it runs.

func Assert

func Assert(ctx context.Context, o BlockOptions) error

Assert runs the block's reads and their assertions, stopping at the first that fails.

func Classify

func Classify(out string, err error) error

Classify wraps err in a CommandError with Kind inferred from the output. Returns nil when err is nil.

func Cp

func Cp(ctx context.Context, o CpOptions) (string, error)

Cp runs kubectl cp.

func CurrentContext

func CurrentContext(ctx context.Context) (string, error)

CurrentContext returns the kubeconfig's current context.

func Delete

func Delete(ctx context.Context, o DeleteOptions) (string, error)

Delete runs kubectl delete.

func Describe

func Describe(ctx context.Context, o DescribeOptions) (string, error)

Describe runs kubectl describe.

func DryRun

func DryRun() bool

DryRun reports whether dry-run mode is on. Tests use it to assert the flag without reading package state.

func Events

func Events(ctx context.Context, o EventsOptions) (string, error)

Events runs kubectl events.

func Exec

func Exec(ctx context.Context, o ExecOptions) (string, error)

Exec runs kubectl exec.

func Get

func Get(ctx context.Context, o GetOptions) (string, error)

Get runs kubectl get.

func Logs

func Logs(ctx context.Context, o LogsOptions) (string, error)

Logs runs kubectl logs.

func Patch

func Patch(ctx context.Context, o PatchOptions) (string, error)

Patch runs kubectl patch.

func PatchFile

func PatchFile(ctx context.Context, o PatchFileOptions) (string, error)

PatchFile runs kubectl patch -f <file>, the manifest form of patch. It patches every object in File, so it has no -n: kubectl reads the namespace from each document.

func Post

func Post(ctx context.Context, o BlockOptions, report func(label, out string, err error))

Post runs every entry in the block once, the writes and then the reads, and hands each one's label, output and error to report. Unlike a step it never stops early: a failed entry is reported and the next one runs, so a cleanup does all it can and a diagnostic prints all it can.

func Restart

func Restart(ctx context.Context, o RestartOptions) (string, error)

Restart runs kubectl rollout restart.

func Rollout

func Rollout(ctx context.Context, o RolloutOptions) (string, error)

Rollout runs kubectl rollout.

func Scale

func Scale(ctx context.Context, o ScaleOptions) (string, error)

Scale runs kubectl scale.

func SetDryRun

func SetDryRun(v bool)

SetDryRun toggles dry-run mode for every subsequent kubectl invocation in this process. The E2E runner sets it once at startup, from --dry-run.

func Top

func Top(ctx context.Context, o TopOptions) (string, error)

Top runs kubectl top.

func UseContext

func UseContext(ctx context.Context, name string) (string, error)

UseContext switches the kubeconfig's current context to name.

func Validate

func Validate(expects spec.Expectations, baseDir string) []error

Validate checks every kubectl: block across the expectations: each step's, and each one's onFailure and finally post blocks.

func ValidateBlock

func ValidateBlock(loc string, k *spec.Kubectl, baseDir string, post bool) []error

ValidateBlock checks one kubectl: block; loc prefixes each error. A post block (onFailure, finally) is checked by the same rules except the step-only ones: a read there may only print, so it needs no assertion, and a get or describe may cover every object of its kind.

func Write

func Write(ctx context.Context, o BlockOptions) error

Write runs the block's writes, stopping at the first that fails. A step runs them once per attempt, before its assertions.

Types

type ApplyInlineOptions

type ApplyInlineOptions struct {
	Content   string
	Namespace string
	WorkDir   string
	Env       env.Resolver
}

ApplyInlineOptions is one `kubectl apply -f - [-n <ns>]` fed through stdin.

type ApplyOptions

type ApplyOptions struct {
	File      string
	Namespace string
	WorkDir   string
	Env       env.Resolver
}

ApplyOptions is one `kubectl apply -f <file> [-n <ns>]`.

type BlockOptions

type BlockOptions struct {
	Kubectl *spec.Kubectl        // the block whose entries run, in order
	Env     env.Resolver         // the expectation's env, in every invocation's environment
	WorkDir string               // cwd for each kubectl call; relative paths resolve here
	Client  kubernetes.Interface // for lease/exec/cp lookups that need the API
	Config  *rest.Config         // for port-forward (SPDY dialer)
	Dynamic dynamic.Interface    // for client-go-native get instead of kubectl subprocess
	Mapper  apimeta.RESTMapper   // resolves kind strings to GVR for the dynamic path
	// Capture saves what a get's capture: entries captured, once its
	// checks pass.
	Capture func(map[string]string)
}

BlockOptions is one kubectl: block and what its entries need to reach the cluster. Write, Assert and Post all take it.

type CommandError

type CommandError struct {
	Kind   Kind
	Output string
	Err    error
}

CommandError is a failed kubectl invocation with a classified Kind.

func (*CommandError) Error

func (e *CommandError) Error() string

func (*CommandError) Unwrap

func (e *CommandError) Unwrap() error

type CpOptions

type CpOptions struct {
	Source    string // "ns/pod:/path/inside"
	Dest      string // local path
	Container string
	WorkDir   string
	Env       env.Resolver
}

CpOptions is one `kubectl cp <src> <dst> [-c <container>]`.

type DeleteOptions

type DeleteOptions struct {
	File           string
	Kind           string
	Name           string
	Namespace      string
	IgnoreNotFound bool
	Timeout        string // --timeout=<dur>; empty means kubectl's default
	Wait           *bool  // --wait=false when set to false; nil means kubectl's default (wait)
	WorkDir        string
	Env            env.Resolver
}

DeleteOptions is one `kubectl delete -f <file>` or `kubectl delete <kind> <name> -n <ns>`.

type DescribeOptions

type DescribeOptions struct {
	Kind          string
	Name          string // mutually exclusive with LabelSelector
	LabelSelector string
	Namespace     string
	WorkDir       string
	Env           env.Resolver
}

DescribeOptions is one `kubectl describe <kind> [name|-l selector] -n <ns>`.

type EventsOptions

type EventsOptions struct {
	Kind      string
	Name      string
	Namespace string
	WorkDir   string
	Env       env.Resolver
}

EventsOptions is one `kubectl events --for <kind>/<name> -n <ns>`.

type ExecOptions

type ExecOptions struct {
	Pod       string
	Namespace string
	Container string
	Command   []string // argv after --
	WorkDir   string
	Env       env.Resolver
}

ExecOptions is one `kubectl exec -n <ns> <pod> [-- <command…>]`.

type ExecTarget

type ExecTarget struct {
	Pod       string
	Namespace string
}

ExecTarget is the resolved pod and namespace an exec runs against.

type GetOptions

type GetOptions struct {
	Kind           string
	Name           string // mutually exclusive with LabelSelector
	LabelSelector  string
	AllNamespaces  bool // --all-namespaces; suppresses -n
	Namespace      string
	Format         string
	WorkDir        string
	Env            env.Resolver
	IgnoreNotFound bool // --ignore-not-found
}

GetOptions is one `kubectl get <kind> [name] [-l <selector>] -n <ns> [-o <format>]` or, with AllNS, `kubectl get <kind> --all-namespaces`.

type Kind

type Kind string

Kind classifies a kubectl failure so callers can branch on it without string-matching stderr.

const (
	KindNotFound  Kind = "not-found"
	KindForbidden Kind = "forbidden"
	KindInvalid   Kind = "invalid"
	KindUnknown   Kind = "unknown"
)

type LogsOptions

type LogsOptions struct {
	Target        string // pod name; mutually exclusive with LabelSelector
	LabelSelector string // -l <selector>
	Namespace     string
	Container     string
	Since         string
	WorkDir       string
	Env           env.Resolver
}

LogsOptions is one `kubectl logs -n <ns> <target> [-c <container>] [--since=<d>]`.

type LogsTarget

type LogsTarget struct {
	Pod           string
	LabelSelector string
	Namespace     string
}

LogsTarget is the resolved target for a logs invocation: a pod name, or a label selector, plus the namespace the pod lives in.

type PatchFileOptions

type PatchFileOptions struct {
	File      string
	Type      string // "merge" (default), "strategic", "json"
	Patch     string
	PatchFile string // used when Patch is empty
	WorkDir   string
	Env       env.Resolver
}

PatchFileOptions is one `kubectl patch -f <file> --type <type>` with -p <patch> or --patch-file <file>.

type PatchOptions

type PatchOptions struct {
	Kind      string
	Name      string
	Namespace string
	Type      string // "merge" (default), "strategic", "json"
	Patch     string
	PatchFile string // used when Patch is empty
	WorkDir   string
	Env       env.Resolver
}

PatchOptions is one `kubectl patch <kind> <name> -n <ns> --type <type>` with -p <patch> or --patch-file <file>.

type RestartOptions

type RestartOptions struct {
	Target    string // "deployment/name", "statefulset/name", …
	Namespace string
	WorkDir   string
	Env       env.Resolver
}

RestartOptions is one `kubectl rollout restart <target> -n <ns>`.

type RolloutOptions

type RolloutOptions struct {
	Sub       RolloutSub
	Target    string
	Namespace string
	WorkDir   string
	Env       env.Resolver
}

RolloutOptions is one `kubectl rollout <sub> <target> -n <ns>`.

type RolloutSub

type RolloutSub string

RolloutSub is a kubectl rollout subcommand: restart, status, undo, …

const (
	RolloutRestart RolloutSub = "restart"
	RolloutStatus  RolloutSub = "status"
)

type ScaleOptions

type ScaleOptions struct {
	Target    string
	Replicas  int
	Namespace string
	WorkDir   string
	Env       env.Resolver
}

ScaleOptions is one `kubectl scale <target> --replicas=<n> -n <ns>`.

type TopOptions

type TopOptions struct {
	Kind          string // "pod"/"pods" or "node"/"nodes"
	Name          string
	LabelSelector string
	Namespace     string
	Containers    bool
	WorkDir       string
	Env           env.Resolver
}

TopOptions is one `kubectl top pod|node [name|-l selector] [-n <ns>] [--containers]`.

Jump to

Keyboard shortcuts

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