client

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package client builds a *kubernetes.Clientset from a kubeconfig file or in-cluster credentials, and exposes the REST config and HTTP client it was built from so callers can share one client.

Index

Constants

View Source
const (
	ContentTypeJSON     = "application/json"
	ContentTypeProtobuf = "application/vnd.kubernetes.protobuf"
)

Wire content types accepted by the Kubernetes API server.

Variables

This section is empty.

Functions

func GetConfigForFile

func GetConfigForFile(kubeConfig string) (*rest.Config, error)

GetConfigForFile returns a REST configuration parsed from the given kubeconfig file path. Thin wrapper over clientcmd.BuildConfigFromFlags exposed for callers that need a config but not a full Client.

func InClusterConfig

func InClusterConfig() (*rest.Config, error)

InClusterConfig returns the REST configuration for the pod's service account, if the process is running inside a Kubernetes cluster. Returns rest.ErrNotInCluster (wrapped) when not in a cluster.

Types

type Client

type Client struct {
	*kubernetes.Clientset
	// contains filtered or unexported fields
}

Client wraps a Kubernetes clientset together with the REST config and HTTP client it was built from. Use the embedded Clientset directly for API calls, or HttpClient()/RestConfig() to build other clients sharing the same transport.

func New

func New(options ...Option) (*Client, error)

New constructs a Client by applying the given options in order, defaulting to WithInClusterConfig() if none set a REST config.

func (*Client) Close

func (c *Client) Close()

Close releases resources held by the Client. Safe to call on a nil or already-closed Client.

func (*Client) HttpClient

func (c *Client) HttpClient() *http.Client

HttpClient returns the Client's underlying HTTP client, e.g. for constructing other clients that share the same transport.

func (*Client) K8sClient

func (c *Client) K8sClient() *kubernetes.Clientset

K8sClient returns the Client's underlying *kubernetes.Clientset. Callers may alternatively use the embedded Clientset directly on the Client value.

func (*Client) RestConfig

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

RestConfig returns a copy of the Client's REST config. Top-level and value-typed nested fields may be freely overwritten, but nested maps/slices (e.g. TLSClientConfig.CAData) share storage with the Client's internal config and must not be mutated.

type Option

type Option func(*Client) error

Option configures a Client during construction via New. Options apply in order; config-dependent options (WithContentType, WithAcceptContentTypes) require a config-source option (WithKubeConfig, WithInClusterConfig, or WithRestConfig) earlier in the list.

func WithAcceptContentTypes

func WithAcceptContentTypes(contentTypes ...string) Option

WithAcceptContentTypes sets the Accept content types to negotiate with the API server, joined with commas. Requires a config-source option earlier in the list.

func WithContentType

func WithContentType(contentType string) Option

WithContentType sets the wire content type used for requests. Requires a config-source option earlier in the list.

func WithHttpClient

func WithHttpClient(hc *http.Client) Option

WithHttpClient returns an Option that uses the given pre-built HTTP client. Useful when multiple components should share one client (and therefore its connection pool).

func WithInClusterConfig

func WithInClusterConfig() Option

WithInClusterConfig returns an Option that resolves the REST config from the pod's service-account credentials.

func WithKubeConfig

func WithKubeConfig(file string) Option

WithKubeConfig returns an Option that resolves the REST config from the given kubeconfig file.

func WithKubeOrInClusterConfig

func WithKubeOrInClusterConfig(file string) Option

WithKubeOrInClusterConfig resolves the REST config from the given kubeconfig file if non-empty, or from in-cluster credentials otherwise.

func WithRestConfig

func WithRestConfig(cfg *rest.Config) Option

WithRestConfig uses a deep copy (via rest.CopyConfig) of the given REST config, so the caller keeps ownership of the original.

Jump to

Keyboard shortcuts

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