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
- func DiscoverNamespace() (string, error)
- func DiscoverNamespaceFromFile(path string) (string, error)
- type Client
- func (c *Client) Clientset() kubernetes.Interface
- func (c *Client) DynamicClient() dynamic.Interface
- func (c *Client) GetResource(ctx context.Context, gvr schema.GroupVersionResource, name string) (*unstructured.Unstructured, error)
- func (c *Client) Namespace() string
- func (c *Client) RestConfig() *rest.Config
- type ClientError
- type Config
- type NamespaceDiscoveryError
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
DynamicClient returns the underlying dynamic client.
func (*Client) GetResource ¶
func (c *Client) GetResource(ctx context.Context, gvr schema.GroupVersionResource, name string) (*unstructured.Unstructured, error)
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) RestConfig ¶
RestConfig returns the underlying REST configuration.
type ClientError ¶
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 ¶
NamespaceDiscoveryError represents errors that occur during namespace discovery.
func (*NamespaceDiscoveryError) Error ¶
func (e *NamespaceDiscoveryError) Error() string
func (*NamespaceDiscoveryError) Unwrap ¶
func (e *NamespaceDiscoveryError) Unwrap() error