client

package
v0.1.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

README

pkg/k8s/client

Kubernetes client initialization.

Overview

Provides utilities for creating and configuring Kubernetes clients.

License

See main repository for license information.

Documentation

Overview

Package client provides a wrapper around the Kubernetes client-go library.

This package simplifies Kubernetes client creation and provides utilities for common operations like namespace discovery.

Index

Constants

View Source
const (
	// DefaultNamespaceFile is the standard location for the service account namespace.
	DefaultNamespaceFile = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
)

Variables

This section is empty.

Functions

func DiscoverNamespace

func DiscoverNamespace() (string, error)

DiscoverNamespace reads the current namespace from the service account token.

Returns:

  • The namespace string
  • An error if the namespace cannot be discovered

The namespace is read from /var/run/secrets/kubernetes.io/serviceaccount/namespace which is automatically mounted in pods by Kubernetes.

func DiscoverNamespaceFromFile

func DiscoverNamespaceFromFile(path string) (string, error)

DiscoverNamespaceFromFile reads the namespace from the specified file. This is primarily useful for testing.

Types

type Client

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

Client wraps a Kubernetes clientset with additional utilities.

func New

func New(cfg Config) (*Client, error)

New creates a new Kubernetes client with the provided configuration.

If Config.Kubeconfig is empty, uses in-cluster configuration. If Config.Namespace is empty, discovers namespace from service account token.

Example:

// In-cluster client
client, err := client.New(client.Config{})

// Out-of-cluster client
client, err := client.New(client.Config{
    Kubeconfig: "/path/to/kubeconfig",
    Namespace:  "default",
})

func NewFromClientset

func NewFromClientset(clientset kubernetes.Interface, dynamicClient dynamic.Interface, namespace string) *Client

NewFromClientset creates a Client from an existing Kubernetes clientset. This is useful for testing with fake clients.

func (*Client) Clientset

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

Clientset returns the underlying Kubernetes clientset.

func (*Client) DynamicClient

func (c *Client) DynamicClient() dynamic.Interface

DynamicClient returns the underlying dynamic client.

func (*Client) GetResource

GetResource fetches a specific Kubernetes resource by name in the client's namespace.

The resource is fetched from the client's default namespace (auto-detected from service account or specified during client creation).

Parameters:

  • ctx: Context for cancellation and timeout
  • gvr: GroupVersionResource identifying the resource type
  • name: Name of the specific resource to fetch

Returns:

  • The resource as an unstructured.Unstructured object
  • An error if the resource cannot be fetched

Example:

// Fetch a ConfigMap
gvr := schema.GroupVersionResource{
    Group:    "",
    Version:  "v1",
    Resource: "configmaps",
}
cm, err := client.GetResource(ctx, gvr, "my-config")

func (*Client) Namespace

func (c *Client) Namespace() string

Namespace returns the default namespace for this client.

func (*Client) RestConfig

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

RestConfig returns the underlying REST configuration.

type ClientError

type ClientError struct {
	Operation string
	Cause     error
}

ClientError represents errors that occur during Kubernetes client operations.

func (*ClientError) Error

func (e *ClientError) Error() string

func (*ClientError) Unwrap

func (e *ClientError) Unwrap() error

type Config

type Config struct {
	// Kubeconfig path for out-of-cluster configuration.
	// If empty, uses in-cluster configuration.
	Kubeconfig string

	// Namespace is the default namespace for operations.
	// If empty, will be discovered from service account.
	Namespace string
}

Config contains configuration options for creating a Kubernetes client.

type NamespaceDiscoveryError

type NamespaceDiscoveryError struct {
	Path  string
	Cause error
}

NamespaceDiscoveryError represents errors that occur during namespace discovery.

func (*NamespaceDiscoveryError) Error

func (e *NamespaceDiscoveryError) Error() string

func (*NamespaceDiscoveryError) Unwrap

func (e *NamespaceDiscoveryError) Unwrap() error

Jump to

Keyboard shortcuts

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