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
The fallback applies to automatic detection only. With MusterClientConfig.RequireKubernetesMode — set by `muster serve` for `kubernetes: true` — a Kubernetes client that cannot be created is an error after a bounded retry (KubernetesClientBackoff), never a filesystem client: a muster configured for an apiserver that comes up against an empty directory reconciles every existing CR as deleted (issue #1143).
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) client.GetWorkflow(ctx, name, namespace)
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 ¶
var KubernetesClientBackoff = wait.Backoff{ Duration: 2 * time.Second, Factor: 2, Steps: 5, }
KubernetesClientBackoff bounds how long a required Kubernetes client is retried before its creation fails: five attempts with about half a minute of sleeps between them (2, 4, 8 and 16 s). That covers the window in which an apiserver, or the kube-proxy rules in front of it, comes back after a node restart — the situation in which muster used to come up in the wrong mode (issue #1143). Anything longer is left to the kubelet's restart backoff, which is visible, rather than waited out inside a process that reports nothing.
No Cap on purpose: wait.Backoff.Step zeroes the remaining steps once the sleep reaches the cap, which would silently cut the attempts short.
It is a variable so tests can shorten it.
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
// 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
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)
WatchEvents(ctx context.Context, options api.EventQueryOptions) (<-chan api.EventResult, 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 adapts to the environment:
- If Kubernetes cluster access is available, it uses the Kubernetes API
- If Kubernetes is not available and no mode was required, it falls back to filesystem operations
This abstraction allows the same code to work in both environments without modification.
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.
Mode selection:
- ForceFilesystemMode: the filesystem client, no apiserver is contacted.
- RequireKubernetesMode: the Kubernetes client, or an error. A muster that was configured to run against an apiserver must never come up against an empty directory instead — that is not a degraded mode but a different program, which deletes the services of every CR it can no longer see (issue #1143). Creation is retried per KubernetesClientBackoff so a brief apiserver outage at startup is waited out; a persistent one fails startup so the kubelet restarts the container visibly.
- neither: Kubernetes when reachable, otherwise the filesystem, with a warning naming the fallback.
Args:
- cfg: Optional 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
// RequireKubernetesMode makes a failure to create the Kubernetes client an
// error instead of a fallback to the filesystem. Set it whenever
// Kubernetes mode is configured explicitly (`kubernetes: true`).
// Mutually exclusive with ForceFilesystemMode.
RequireKubernetesMode bool
// Debug enables debug-level logging and warnings
Debug bool
}
MusterClientConfig provides configuration options for client creation.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package filesystem is the filesystem-backed implementation of the unified muster client interface defined in the parent client package.
|
Package filesystem is the filesystem-backed implementation of the unified muster client interface defined in the parent client package. |