Documentation
¶
Overview ¶
Package k8s provides Kubernetes client functionality for the k8s-controller application. It handles kubeconfig loading, client creation, and connection testing with structured logging.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadKubeconfig ¶
LoadKubeconfig loads the Kubernetes configuration from various sources. It follows the standard precedence: in-cluster config > kubeconfig file > default locations. Returns a *rest.Config that can be used to create a Kubernetes client.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client wraps the Kubernetes clientset with additional functionality. It provides structured logging and connection management for k8s operations.
func CreateClient ¶
func CreateClient(config ClientConfig, logger zerolog.Logger) (*Client, error)
CreateClient creates a new Kubernetes client with the provided configuration. It returns a Client instance that wraps the clientset with additional functionality.
Example ¶
ExampleCreateClient demonstrates how to create a Kubernetes client.
logger := zerolog.New(os.Stderr)
config := ClientConfig{
KubeconfigPath: "/path/to/kubeconfig",
Context: "my-context",
}
client, err := CreateClient(config, logger)
if err != nil {
logger.Fatal().Err(err).Msg("Failed to create Kubernetes client")
}
defer func() {
if err := client.Close(); err != nil {
logger.Error().Err(err).Msg("Failed to close client")
}
}()
// Test the connection
ctx := context.Background()
if err := client.TestConnection(ctx); err != nil {
logger.Fatal().Err(err).Msg("Failed to connect to Kubernetes")
}
logger.Info().Msg("Successfully connected to Kubernetes")
func (*Client) Close ¶
Close performs cleanup operations for the client. Currently, there's no cleanup needed for the Kubernetes client, but this method is provided for future extensibility.
func (*Client) GetClientset ¶
func (c *Client) GetClientset() kubernetes.Interface
GetClientset returns the underlying Kubernetes clientset. This allows access to all Kubernetes API operations.
func (*Client) GetConfig ¶
GetConfig returns the underlying REST config. This can be useful for creating other types of clients.
func (*Client) TestConnection ¶
TestConnection verifies that the client can connect to the Kubernetes API server. It performs a simple API call to list namespaces with a timeout.
Example ¶
ExampleClient_TestConnection demonstrates how to test a Kubernetes connection.
logger := zerolog.New(os.Stderr)
// Assuming you have a client already created
config := ClientConfig{}
client, err := CreateClient(config, logger)
if err != nil {
return
}
defer func() {
if err := client.Close(); err != nil {
logger.Error().Err(err).Msg("Failed to close client")
}
}()
// Test connection with timeout
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.TestConnection(ctx)
if err != nil {
logger.Error().Err(err).Msg("Connection test failed")
return
}
logger.Info().Msg("Connection test successful")
type ClientConfig ¶
type ClientConfig struct {
// KubeconfigPath specifies the path to the kubeconfig file.
// If empty, the default locations will be checked.
KubeconfigPath string
// Context specifies which context to use from the kubeconfig.
// If empty, the current context will be used.
Context string
}
ClientConfig holds configuration options for creating a Kubernetes client.