-
Notifications
You must be signed in to change notification settings - Fork 169
Description
Summary
Add support for dynamic Kubernetes cluster targeting through HTTP headers, allowing AI systems to specify cluster endpoints and TLS configuration per-request without requiring pre-configured kubeconfig contexts.
Problem Statement
Currently, the MCP server only supports targeting clusters that are pre-configured in a kubeconfig file via the context parameter. This requires:
- Pre-configuration: All target clusters must be known and configured ahead of time
- Static configuration: Cannot dynamically connect to new clusters
- LLM parameter pollution: Adding dynamic cluster support via tool parameters would require LLMs to understand infrastructure details
For AI systems that need to interact with multiple Kubernetes clusters dynamically (e.g., multi-tenant scenarios, ephemeral clusters, or clusters discovered at runtime), the current approach is limiting.
Proposed Solution
Add support for specifying cluster connection details via HTTP headers while maintaining 100% backward compatibility with the existing context-based approach.
New HTTP Headers
X-Kubernetes-Cluster-Server: https://cluster.example.com:6443
X-Kubernetes-Cluster-CA-Data: LS0tLS1CRUdJTi... # base64 encoded CA cert
X-Kubernetes-Cluster-Insecure: false # optional, default: false
X-Kubernetes-Cluster-Server-Name: cluster.example.com # optional, for SNI
Precedence Logic
- If
X-Kubernetes-Cluster-Serverheader is present: Use dynamic cluster from headers - Else if
contextparameter is present: Use kubeconfig context (existing behavior) - Else: Use default kubeconfig context (existing behavior)
Benefits
Clean Separation of Concerns
-
LLM tool parameters: Remain focused on Kubernetes resources and operations
-
Infrastructure configuration: Handled at the HTTP header level by the AI system
-
No parameter pollution: Tool signatures stay clean and focused
Backward Compatibility
- ✅ All existing configurations work unchanged
- ✅ All existing API calls work unchanged
- ✅ Headers are completely optional
- ✅ Zero migration required
Flexibility
- Switch clusters per request without changing tool parameters
- Set headers once and reuse across multiple tool calls
- Support for ephemeral or dynamically discovered clusters
- Standard HTTP header patterns
Example Usage
Current Approach (Unchanged)
POST /mcp/tools/call
Authorization: Bearer token123
Content-Type: application/json
{
"method": "tools/call",
"params": {
"name": "pods_list",
"arguments": {
"context": "prod-cluster", // Uses kubeconfig context
"namespace": "default"
}
}
}
New Dynamic Approach
POST /mcp/tools/call
Authorization: Bearer token123
X-Kubernetes-Cluster-Server: https://my-cluster.example.com:6443
X-Kubernetes-Cluster-CA-Data: LS0tLS1CRUdJTi...
Content-Type: application/json
{
"method": "tools/call",
"params": {
"name": "pods_list",
"arguments": {
"namespace": "default" // Clean tool parameters - no cluster info
}
}
}
Implementation Approach
Phase 1: Core Implementation
- Header extraction: Enhance contextFunc() in pkg/mcp/mcp.go to extract cluster headers
- Manager enhancement: Modify Manager.Derived() in pkg/kubernetes/manager.go to handle dynamic clusters
- Configuration validation: Basic URL parsing and CA certificate validation
Phase 2: Production Features
- Configuration options: Optional settings for enabling/disabling dynamic clusters
- Audit logging: Track dynamic cluster usage for security/compliance
- Rate limiting: Prevent resource exhaustion from dynamic requests
- Error handling: Comprehensive validation and error messages
Configuration (Optional)
# Optional: Enable dynamic cluster headers (default: true)
enable_dynamic_cluster_headers = true
# Optional: Operational controls
[dynamic_clusters]
audit_dynamic_requests = true
max_dynamic_requests_per_minute = 100
request_timeout = "30s"
Security Considerations
Why This Is Safe
- No additional attack surface: Users with valid bearer tokens can already access these clusters directly
- Standard patterns: Uses established HTTP header security practices
- Validation: URL parsing and certificate validation prevent malformed requests
- Audit trail: Optional logging for compliance requirements
Controls
- Rate limiting: Prevent server resource exhaustion
- Request validation: Ensure well-formed cluster specifications
- Audit logging: Track dynamic cluster access for compliance
Files to Modify
pkg/mcp/mcp.go: Enhance contextFunc() to extract cluster headerspkg/kubernetes/manager.go: Modify Derived() method to handle dynamic clusterspkg/config/config.go: Add optional configuration for dynamic cluster support- Tests and documentation
Alternatives Considered
- Tool parameters: Rejected because it pollutes LLM-facing parameters with infrastructure details
- Configuration endpoints: Rejected because it requires additional API complexity
- Replace existing approach: Rejected to maintain backward compatibility
Open Questions
- Should there be any allowlisting of cluster domains/IPs, or is token-based access control sufficient?
- Should we add connection pooling for frequently accessed dynamic clusters?
- Any specific naming preferences for the HTTP headers?