kubernetes

package
v0.1.94 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 58 Imported by: 4 modules, 2 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

Index

Constants

View Source
const (
	DefaultForwardIdleTimeout = time.Duration(0)
	DefaultMaxForwards        = 64
)

DefaultForwardIdleTimeout disables time-based closure. A resolved URL does not carry a lease, so elapsed time since hydration cannot prove that no database pool is still using the tunnel. Capacity eviction, forwarder completion, and CloseAll provide bounded lifecycle controls without closing active clients.

Variables

View Source
var ErrNoCluster = errors.New("no kubernetes cluster configured: name a connection, or set $KUBECONFIG / ~/.kube/config")

ErrNoCluster is returned by NewClient when no kubeconfig or in-cluster configuration exists.

Functions

func BytesToUtf8Lf

func BytesToUtf8Lf(file []byte) (string, error)

func GetAPIServer

func GetAPIServer(kubeconfigRaw []byte) (string, error)

func GetClusterName

func GetClusterName(config *rest.Config) string

GetClusterName returns the name of the cluster

func GetClusterNameFromKubeconfig

func GetClusterNameFromKubeconfig(config *clientcmdapi.Config) (clusterName string, server string)

func GetUnstructuredObjects

func GetUnstructuredObjects(data []byte) ([]unstructured.Unstructured, error)

func NewClient

func NewClient(log logger.Logger, kubeconfigPaths ...string) (kubernetes.Interface, *rest.Config, error)

func NewClientFromPathOrConfig

func NewClientFromPathOrConfig(
	logger logger.Logger,
	kubeconfigOrPath string,
) (kubernetes.Interface, *rest.Config, error)

func NewClientWithConfig

func NewClientWithConfig(log logger.Logger, kubeConfig []byte) (kubernetes.Interface, *rest.Config, error)

func PortForward added in v0.1.13

func PortForward(ctx context.Context, k8s kubernetes.Interface, restConfig *rest.Config, opts PortForwardOptions) (int, chan struct{}, error)

PortForward establishes an SPDY port-forward tunnel to the workload identified by opts, resolving it to a Ready pod with ResolvePodPort. It returns the allocated local port, a stop channel that tears the tunnel down when closed, and any error.

func PortForwardDeployment added in v0.1.13

func PortForwardDeployment(ctx context.Context, k8s kubernetes.Interface, restConfig *rest.Config, opts PortForwardOptions) (int, chan struct{}, error)

PortForwardDeployment sets up port forwarding to a pod managed by the specified deployment. Without a Name the LabelSelector selects the pods. Returns the local port, a stop channel, and any error.

func PortForwardPod added in v0.1.13

func PortForwardPod(ctx context.Context, k8s kubernetes.Interface, restConfig *rest.Config, opts PortForwardOptions) (int, chan struct{}, error)

PortForwardPod sets up port forwarding to a pod matching the given name or label selector. Returns the local port, a stop channel to close when done, and any error.

func PortForwardService added in v0.1.13

func PortForwardService(ctx context.Context, k8s kubernetes.Interface, restConfig *rest.Config, opts PortForwardOptions) (int, chan struct{}, error)

PortForwardService sets up port forwarding to a pod backing the specified service. The service is found by Name or LabelSelector. Returns the local port, a stop channel, and any error.

func RegisterAPIServerProxy added in v0.1.88

func RegisterAPIServerProxy(proxyBase string, config *rest.Config) error

RegisterAPIServerProxy records that URLs under proxyBase authenticate to the apiserver as config. Registering the same config (or an identical one) again is a no-op; a second identity for the same base makes lookups ambiguous.

func RestConfigFingerprint

func RestConfigFingerprint(rc *rest.Config) string

RestConfigFingerprint generates a unique SHA-256 hash to identify the Kubernetes API server and client authentication details from the REST configuration.

func WorkloadKinds added in v0.1.88

func WorkloadKinds() []string

WorkloadKinds lists every kind a WorkloadTarget accepts, sorted.

Types

type APIServerProxy added in v0.1.88

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

APIServerProxy is the kubeconfig identity that authenticates requests to an apiserver proxy URL (/api/v1/namespaces/<ns>/{services,pods}/<target>/proxy).

The apiserver deletes the Authorization header after authenticating a request, before proxying it, so a backend behind services/proxy or pods/proxy never sees the caller's Authorization header. Backend credentials have to travel another way (a query parameter, or a header the backend reads instead of Authorization).

RBAC: create/get on services/proxy or pods/proxy in the target namespace.

func APIServerProxyFor added in v0.1.88

func APIServerProxyFor(u *url.URL) (*APIServerProxy, error)

APIServerProxyFor returns the identity registered for the proxy base u falls under, or nil when u is not under a registered apiserver proxy URL.

func (*APIServerProxy) Config added in v0.1.88

func (p *APIServerProxy) Config() *rest.Config

Config returns the rest config the proxy authenticates with.

func (*APIServerProxy) Transport added in v0.1.88

func (p *APIServerProxy) Transport() (http.RoundTripper, error)

Transport returns an apiserver-authenticated transport (bearer, basic, exec, client certificate and CA from the rest config). It is rebuilt when the config's identity changes in place, e.g. a refreshed token.

type Client

type Client struct {
	kubernetes.Interface

	Config *rest.Config // Prefer updaating token in place
	// contains filtered or unexported fields
}

func NewKubeClient

func NewKubeClient(logger logger.Logger, client kubernetes.Interface, config *rest.Config) *Client

func (*Client) Apply added in v0.1.1

func (c *Client) Apply(ctx context.Context, manifest string) (Resources, error)

func (*Client) ApplyFile added in v0.1.1

func (c *Client) ApplyFile(ctx context.Context, files ...string) (Resources, error)

func (*Client) DeleteByGVK

func (c *Client) DeleteByGVK(ctx context.Context, namespace, name string, gvk schema.GroupVersionKind) (bool, error)

func (*Client) ExecutePodf

func (c *Client) ExecutePodf(ctx context.Context, opts PodExecOptions) (string, string, error)

func (*Client) ExpandNamespaces

func (c *Client) ExpandNamespaces(ctx context.Context, namespace string) ([]string, error)

func (*Client) FetchResources

func (c *Client) FetchResources(
	ctx context.Context,
	resources ...unstructured.Unstructured,
) ([]unstructured.Unstructured, error)

func (*Client) Get added in v0.1.1

func (c *Client) Get(ctx context.Context, kind, namespace, name string) (*unstructured.Unstructured, error)

func (*Client) GetClientByGroupVersionKind

func (c *Client) GetClientByGroupVersionKind(
	ctx context.Context, group, version, kind string,
) (dynamic.NamespaceableResourceInterface, *meta.RESTMapping, error)

func (*Client) GetClientByKind

WARN: "Kind" is not specific enough. A cluster can have various resources with the same Kind. example: helmchrats.helm.cattle.io & helmcharts.source.toolkit.fluxcd.io both have HelmChart as the kind.

Use GetClientByGroupVersionKind instead.

func (*Client) GetDynamicClient

func (c *Client) GetDynamicClient() (dynamic.Interface, error)

GetDynamicClient creates a new k8s client

func (*Client) GetPodIP added in v0.1.1

func (c *Client) GetPodIP(ctx context.Context, namespace, selector string) (string, error)

func (*Client) GetPodLogs

func (c *Client) GetPodLogs(ctx context.Context, namespace, podName, container string) (io.ReadCloser, error)

func (*Client) GetResource added in v0.1.1

func (c *Client) GetResource(ctx context.Context, kind, namespace, name string) (*Resource, error)

func (*Client) GetRestMapper

func (c *Client) GetRestMapper() (meta.RESTMapper, error)

func (*Client) List added in v0.1.1

func (c *Client) List(ctx context.Context, kind, namespace, selector string) ([]unstructured.Unstructured, error)

func (*Client) QueryResources

func (c *Client) QueryResources(ctx context.Context, selector types.ResourceSelector) ([]unstructured.Unstructured, error)

func (*Client) Reset

func (c *Client) Reset()

func (*Client) ResetRestMapper

func (c *Client) ResetRestMapper()

func (*Client) RestConfig

func (c *Client) RestConfig() *rest.Config

func (*Client) SetLogger

func (c *Client) SetLogger(logger logger.Logger)

func (*Client) StreamLogsV2

func (c *Client) StreamLogsV2(ctx context.Context, opts ContainerWaitOptions) error

func (*Client) WaitFor added in v0.1.1

func (c *Client) WaitFor(ctx context.Context, ref ResourceRef, condition func(*unstructured.Unstructured) bool, timeout time.Duration) (*unstructured.Unstructured, error)

func (*Client) WaitForContainerStart

func (c *Client) WaitForContainerStart(ctx context.Context, opts ContainerWaitOptions) error

WaitForContainerStart waits for the specified containers to be started (or any container if no names are specified) - returns an error if the timeout is exceeded

func (*Client) WaitForJob added in v0.1.4

func (c *Client) WaitForJob(
	ctx context.Context,
	namespace, name string,
	timeout time.Duration,
) error

WaitForJob waits for a job to complete or fail within the specified timeout. Returns nil if the job completes successfully, or an error if the job fails or times out.

func (*Client) WaitForPod

func (c *Client) WaitForPod(ctx context.Context, opts PodWaitOptions) error

WaitForPod waits for a pod to be in the specified phase, or returns an error if the timeout is exceeded

func (*Client) WaitForReady added in v0.1.1

func (c *Client) WaitForReady(ctx context.Context, ref ResourceRef, timeout time.Duration) (*Resource, error)

WaitForReady polls until ref is healthy; a zero timeout waits one minute.

func (*Client) WithNamespace added in v0.1.1

func (c *Client) WithNamespace(namespace string) *Client

type ContainerWaitOptions added in v0.1.62

type ContainerWaitOptions struct {
	Namespace      string
	Name           string
	Timeout        time.Duration
	ContainerNames []string
}

type Contexts added in v0.1.72

type Contexts struct {
	CurrentContext string   `json:"currentContext"`
	Contexts       []string `json:"contexts"`
}

type ForwardManager added in v0.1.13

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

ForwardManager owns live port-forward tunnels and reuses them across hydrations and connections. Tunnels are keyed by cluster + target so concurrent callers requesting the same workload share one tunnel, and a reaper closes tunnels left idle past idleTimeout.

func DefaultForwardManager added in v0.1.13

func DefaultForwardManager() *ForwardManager

DefaultForwardManager returns the process-wide forward manager, creating it on first use.

func NewForwardManager added in v0.1.13

func NewForwardManager(idleTimeout time.Duration) *ForwardManager

NewForwardManager returns a manager with an optional idle timeout. A zero timeout disables time-based reaping.

func (*ForwardManager) CloseAll added in v0.1.15

func (m *ForwardManager) CloseAll()

CloseAll terminates every tunnel owned by the manager. It is safe to call repeatedly and gives servers/tests an explicit lifecycle boundary.

func (*ForwardManager) Forward added in v0.1.13

func (m *ForwardManager) Forward(ctx context.Context, k8s kubernetes.Interface, restConfig *rest.Config, opts PortForwardOptions) (int, error)

Forward returns a local port that tunnels to the workload identified by opts, reusing a live tunnel when one already exists for the same cluster + target and (re)establishing otherwise.

type Options added in v0.1.72

type Options struct {
	Config      []byte `json:"-" yaml:"-"`
	Kubeconfig  string `json:"kubeconfig,omitempty" yaml:"kubeconfig,omitempty"`
	Kubecontext string `json:"kubecontext,omitempty" yaml:"kubecontext,omitempty"`
}

func (Options) ContextName added in v0.1.72

func (o Options) ContextName() (string, error)

func (Options) Contexts added in v0.1.72

func (o Options) Contexts() (Contexts, error)

func (Options) Explicit added in v0.1.72

func (o Options) Explicit() bool

func (Options) Namespace added in v0.1.72

func (o Options) Namespace() (string, error)

func (Options) RESTConfig added in v0.1.72

func (o Options) RESTConfig() (*rest.Config, error)

type PodExecOptions added in v0.1.62

type PodExecOptions struct {
	Namespace string
	Pod       string
	Container string
	Command   []string
}

type PodPort added in v0.1.88

type PodPort struct {
	Namespace string
	Pod       string
	Port      int
}

PodPort is the Ready pod and numeric container port a WorkloadTarget resolved to.

func ResolvePodPort added in v0.1.88

func ResolvePodPort(ctx context.Context, k8s kubernetes.Interface, target WorkloadTarget) (PodPort, error)

ResolvePodPort resolves target to one Ready pod, the first by name among the candidates, and the numeric container port it listens on.

type PodWaitOptions added in v0.1.62

type PodWaitOptions struct {
	Namespace string
	Name      string
	Timeout   time.Duration
	Phases    []v1.PodPhase
}

type PortForwardOptions added in v0.1.13

type PortForwardOptions struct {
	Namespace     string `json:"namespace,omitempty"`
	Name          string `json:"name,omitempty"`
	LabelSelector string `json:"labelSelector,omitempty"`
	RemotePort    int    `json:"remotePort,omitempty"`
	Kind          string `json:"kind"`
}

PortForwardOptions configures port forwarding to a Kubernetes resource. Either Name or LabelSelector must be provided to identify the target resource; Kind is one of WorkloadKinds.

type Resource added in v0.1.1

type Resource struct {
	unstructured.Unstructured
	health.HealthStatus
}

func (*Resource) IsHealthy added in v0.1.1

func (r *Resource) IsHealthy() bool

func (Resource) Pretty added in v0.1.1

func (r Resource) Pretty() api.Text

type ResourceRef added in v0.1.62

type ResourceRef struct {
	Kind      string
	Namespace string
	Name      string
}

type Resources added in v0.1.1

type Resources []Resource

func NewResources added in v0.1.1

func NewResources(objs ...unstructured.Unstructured) Resources

func (Resources) AsUnstructured added in v0.1.1

func (r Resources) AsUnstructured() []unstructured.Unstructured

func (Resources) Pretty added in v0.1.1

func (r Resources) Pretty() api.Text

type WorkloadTarget added in v0.1.88

type WorkloadTarget struct {
	Namespace     string
	Kind          string
	Name          string
	LabelSelector string
	Port          string
}

WorkloadTarget names a port on a pod-backed workload. Kind is "pod", "service" or a controller kind (deployment, statefulset, daemonset, replicaset). Name selects the object; without a Name, LabelSelector selects the pods directly (services for kind service). Port is a number, a port name (a Service port for kind service, else a container port) or empty for the first container port.

Resolution needs get/list on pods, get on the named kind, and list on services when a service is selected by label.

func (WorkloadTarget) String added in v0.1.88

func (t WorkloadTarget) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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