Documentation
¶
Overview ¶
Package client provides a unified client abstraction for accessing muster resources both locally (filesystem) and in-cluster (Kubernetes API).
Overview ¶
The client package implements the unified access pattern described in Issues #30 and #36, providing seamless operation across different deployment environments:
- **Local Development**: Filesystem-based storage with YAML files - **In-Cluster**: Native Kubernetes API access using controller-runtime - **External Tools**: kubectl-compatible access patterns
Architecture ¶
The package implements a facade pattern with automatic environment detection:
┌─────────────────┐
│ MusterClient │ ← Unified Interface
│ (Interface) │
└─────────────────┘
│
┌────┴────┐
│ Factory │ ← Environment Detection
└────┬────┘
│
┌─────┴─────┐
│ │
┌──▼──┐ ┌───▼──┐
│ K8s │ │ File │ ← Backend Implementations
│ │ │ │
└─────┘ └──────┘
Usage Examples ¶
## Basic Usage (Automatic Detection)
client, err := client.NewMusterClient()
if err != nil {
log.Fatal(err)
}
defer client.Close()
servers, err := client.ListMCPServers(ctx, "default")
if err != nil {
log.Fatal(err)
}
## Explicit Configuration
config := &client.MusterClientConfig{
Namespace: "muster-system",
ForceFilesystemMode: true,
}
client, err := client.NewMusterClientWithConfig(config)
Environment Detection ¶
The client automatically detects the execution environment:
1. **Kubernetes Detection**: Uses controller-runtime's standard config detection
- In-cluster service account credentials
- kubeconfig file (~/.kube/config)
- Environment variables (KUBECONFIG)
2. **Filesystem Fallback**: Used when Kubernetes is not available
- Local development environments
- Standalone deployment scenarios
- Testing and debugging
Interface Compatibility ¶
MusterClient extends controller-runtime's client.Client interface, ensuring compatibility with existing Kubernetes tooling while adding muster-specific convenience methods.
Future Extensibility ¶
The interface design supports future CRD implementations:
// Current (v1alpha1) client.GetMCPServer(ctx, name, namespace) // Future (as CRDs are implemented) client.GetServiceClass(ctx, name, namespace) client.ListWorkflows(ctx, namespace) client.CreateService(ctx, service)
Error Handling ¶
The client provides consistent error handling across both backends:
server, err := client.GetMCPServer(ctx, "server-name", "default")
if err != nil {
if errors.IsNotFound(err) {
// Handle not found consistently across backends
}
// Handle other errors
}
Thread Safety ¶
All client implementations are thread-safe and can be used concurrently from multiple goroutines.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MusterClient ¶
type MusterClient interface {
// Controller-runtime client interface for basic CRUD operations
client.Client
// MCPServer operations
GetMCPServer(ctx context.Context, name, namespace string) (*musterv1alpha1.MCPServer, error)
ListMCPServers(ctx context.Context, namespace string) ([]musterv1alpha1.MCPServer, error)
CreateMCPServer(ctx context.Context, server *musterv1alpha1.MCPServer) error
UpdateMCPServer(ctx context.Context, server *musterv1alpha1.MCPServer) error
DeleteMCPServer(ctx context.Context, name, namespace string) error
// ServiceClass operations
GetServiceClass(ctx context.Context, name, namespace string) (*musterv1alpha1.ServiceClass, error)
ListServiceClasses(ctx context.Context, namespace string) ([]musterv1alpha1.ServiceClass, error)
CreateServiceClass(ctx context.Context, serviceClass *musterv1alpha1.ServiceClass) error
UpdateServiceClass(ctx context.Context, serviceClass *musterv1alpha1.ServiceClass) error
DeleteServiceClass(ctx context.Context, name, namespace string) error
// Workflow operations
GetWorkflow(ctx context.Context, name, namespace string) (*musterv1alpha1.Workflow, error)
ListWorkflows(ctx context.Context, namespace string) ([]musterv1alpha1.Workflow, error)
CreateWorkflow(ctx context.Context, workflow *musterv1alpha1.Workflow) error
UpdateWorkflow(ctx context.Context, workflow *musterv1alpha1.Workflow) error
DeleteWorkflow(ctx context.Context, name, namespace string) error
// Status update operations (uses Status subresource in Kubernetes mode)
// These methods update only the Status field of the resource.
// See ADR 007 for details on what status fields are synced.
UpdateMCPServerStatus(ctx context.Context, server *musterv1alpha1.MCPServer) error
UpdateServiceClassStatus(ctx context.Context, serviceClass *musterv1alpha1.ServiceClass) error
UpdateWorkflowStatus(ctx context.Context, workflow *musterv1alpha1.Workflow) error
// Event operations
CreateEvent(ctx context.Context, obj client.Object, reason, message, eventType string) error
CreateEventForCRD(ctx context.Context, crdType, name, namespace, reason, message, eventType string) error
QueryEvents(ctx context.Context, options api.EventQueryOptions) (*api.EventQueryResult, error)
// Utility methods
IsKubernetesMode() bool
Close() error
}
MusterClient is a unified interface that abstracts both Kubernetes and filesystem clients. It provides a single interface for interacting with muster resources regardless of the deployment mode (Kubernetes cluster vs filesystem configuration).
The interface automatically adapts to the environment:
- If Kubernetes cluster access is available, it uses the Kubernetes API
- If Kubernetes is not available, it falls back to filesystem operations
This abstraction allows the same code to work in both environments without modification.
func NewFilesystemClient ¶
func NewFilesystemClient(cfg *MusterClientConfig) (MusterClient, error)
NewFilesystemClient creates a new filesystem-based muster client.
This client stores resources as YAML files in the local filesystem, providing compatibility with existing file-based configurations.
Args:
- cfg: Client configuration (optional)
Returns:
- MusterClient: The filesystem-backed client
- error: Error if client creation fails
func NewKubernetesClient ¶
func NewKubernetesClient(config *rest.Config) (MusterClient, error)
NewKubernetesClient creates a new Kubernetes-based muster client.
This client uses controller-runtime for efficient Kubernetes API access with proper caching, informers, and watch functionality.
Args:
- config: Kubernetes REST configuration
Returns:
- MusterClient: The Kubernetes-backed client
- error: Error if client creation fails or CRDs are not available
func NewMusterClient ¶
func NewMusterClient() (MusterClient, error)
NewMusterClient creates a new unified muster client with automatic environment detection.
The client will attempt to use Kubernetes configuration (from kubeconfig, in-cluster config, or other standard methods). If Kubernetes is not available, it will fall back to filesystem mode.
Returns:
- MusterClient: The unified client interface
- error: Error if client creation fails
func NewMusterClientWithConfig ¶
func NewMusterClientWithConfig(cfg *MusterClientConfig) (MusterClient, error)
NewMusterClientWithConfig creates a new unified muster client with optional configuration.
This function provides more control over client creation for advanced use cases.
Args:
- cfg: Optional Kubernetes configuration. If nil, uses standard detection methods.
Returns:
- MusterClient: The unified client interface
- error: Error if client creation fails
type MusterClientConfig ¶
type MusterClientConfig struct {
// Namespace is the default namespace for operations (defaults to "default")
Namespace string
// FilesystemPath is the base path for filesystem storage (defaults to current directory)
FilesystemPath string
// ForceFilesystemMode forces filesystem mode even if Kubernetes is available
ForceFilesystemMode bool
// Debug enables debug-level logging and warnings
Debug bool
}
MusterClientConfig provides configuration options for client creation.