-
Notifications
You must be signed in to change notification settings - Fork 144
feat: Implement VirtualMCPServer Kubernetes controller #2448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
3d6cf76 to
24f2c25
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR implements the VirtualMCPServer controller for the Toolhive operator, enabling management of virtual MCP (Model Context Protocol) servers in Kubernetes. The controller aggregates multiple backend MCP servers through group references and manages their deployment lifecycle.
Key changes:
- Implements comprehensive controller logic for VirtualMCPServer reconciliation with backend discovery, resource management, and status updates
- Adds extensive unit and integration test coverage across controller, deployment, discovery, and configuration components
- Integrates controller setup in main.go with proper initialization
Reviewed Changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| cmd/thv-operator/main.go | Registers VirtualMCPServer controller with manager |
| cmd/thv-operator/controllers/virtualmcpserver_controller.go | Core reconciliation loop and resource management logic |
| cmd/thv-operator/controllers/virtualmcpserver_deployment.go | Deployment and Service creation/management functions |
| cmd/thv-operator/controllers/virtualmcpserver_discovery.go | Backend discovery and health checking logic |
| cmd/thv-operator/controllers/virtualmcpserver_vmcpconfig.go | Configuration generation and ConfigMap management |
| cmd/thv-operator/controllers/virtualmcpserver_controller_test.go | Unit tests for controller reconciliation logic |
| cmd/thv-operator/controllers/virtualmcpserver_deployment_test.go | Unit tests for deployment and service creation |
| cmd/thv-operator/controllers/virtualmcpserver_discovery_test.go | Unit tests for backend discovery |
| cmd/thv-operator/controllers/virtualmcpserver_vmcpconfig_test.go | Unit tests for configuration generation |
| test/e2e/chainsaw/operator/single-tenancy/test-scenarios/vmcp/*.yaml | E2E test resources and assertions for controller integration testing |
Comments suppressed due to low confidence (1)
cmd/thv-operator/controllers/virtualmcpserver_controller.go:804
- This expression compares an expression to itself.
if toolConfig.ToolConfigRef != nil && toolConfig.ToolConfigRef.Name == toolConfig.ToolConfigRef.Name {
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
25cb167 to
d955486
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #2448 +/- ##
==========================================
- Coverage 55.46% 54.90% -0.56%
==========================================
Files 284 291 +7
Lines 26720 27837 +1117
==========================================
+ Hits 14820 15284 +464
- Misses 10537 11151 +614
- Partials 1363 1402 +39 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@claude please review this PR. Verify thay it follows kubernetes controller best practices and check if there's common helper functions we could have reused. |
|
Claude finished @JAORMX's task —— View job Code Review CompleteI've completed a thorough review of the VirtualMCPServer controller implementation. Here are my findings regarding Kubernetes best practices and opportunities for code reuse. SummaryThe implementation follows many Kubernetes controller best practices and is well-structured with good separation of concerns. However, there are several areas where existing helper functions could be reused and some best practices could be strengthened. Critical Issues1. Self-Comparison Bug (Line 804)
|
2542e96 to
7d1f15b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 19 out of 19 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@claude do another review |
|
Claude finished @JAORMX's task —— View job Code Review CompleteI've completed a thorough second review of the VirtualMCPServer controller implementation. Overall, the code is well-structured and follows Kubernetes best practices. Here are my findings: Todo List
SummaryThe VirtualMCPServer controller implementation is solid and follows most Kubernetes controller best practices. Good news: the critical self-comparison bug from the previous review has been fixed! The code is well-organized with good separation of concerns across multiple files. However, there are several areas where existing helper functions could be reused for better consistency, and some improvements to make the implementation more robust. Critical Issues Fixed ✅Self-Comparison Bug (Previously Line 804): The bug identified in the previous review where if tc.ToolConfigRef != nil && tc.ToolConfigRef.Name == toolConfigName {
return true
}This is correct! ✅ Code Reuse Opportunities1. Checksum Implementation - Already Using Helper! ✅I'm pleased to see that the controller is already using the proper helper function for checksums at checksum := ctrlutil.CalculateConfigHash(configMap.Data)This is good! However, I noticed that the checksum system used here is different from the one used by MCPServer and MCPRemoteProxy controllers. Those use Recommendation: Consider whether VirtualMCPServer should align with the same checksum pattern used by MCPServer/MCPRemoteProxy for consistency. The current 2. Service Name and URL Generation - Minor InconsistencyCurrent implementation ( func vmcpServiceAccountName(vmcpName string) string {
return fmt.Sprintf("%s-vmcp", vmcpName)
}
func vmcpServiceName(vmcpName string) string {
return fmt.Sprintf("vmcp-%s", vmcpName)
}
func createVmcpServiceURL(vmcpName, namespace string, port int32) string {
serviceName := vmcpServiceName(vmcpName)
return fmt.Sprintf("http://%s.%s.svc.cluster.local:%d", serviceName, namespace, port)
}Similar helpers exist in
Observation: The naming patterns are different:
Recommendation: This is fine if the different naming patterns are intentional to distinguish resource types. The implementation is consistent within the VirtualMCPServer controller. No change needed unless you want to unify naming conventions across all controllers. 3. Watch Handler Optimization - Still UnoptimizedThe watch handlers at // mapMCPServerToVirtualMCPServer
for _, vmcp := range vmcpList.Items {
requests = append(requests, reconcile.Request{...})
}Impact: This creates unnecessary reconciliation overhead in clusters with many VirtualMCPServers. Recommendation (same as previous review): Consider optimizing by:
The comment at line 743 acknowledges this: "For simplicity, we'll reconcile all VirtualMCPServers". Consider implementing the optimization or creating a GitHub issue to track it. Kubernetes Best PracticesStrengths ✅
Areas for Improvement4. Missing FinalizersThe controller doesn't implement finalizers for cleanup. Question: Does VirtualMCPServer create any external resources or need cleanup on deletion? If so, add finalizer handling. See If no cleanup needed: Document this decision with a comment. 5. Event Recording - Still MissingThe controller doesn't record Kubernetes Events for important state changes. Example from MCPServer: r.Recorder.Event(mcpServer, corev1.EventTypeNormal, "Created", "Deployment created successfully")Recommendation: Add event recording for:
Events provide valuable visibility for users debugging issues. 6. Health Check Interval - TODO Still PresentAt func (*VirtualMCPServerReconciler) getHealthCheckInterval(vmcp *mcpv1alpha1.VirtualMCPServer) time.Duration {
// TODO: Parse the duration string from spec when FailureHandling is configured
//nolint:staticcheck // Empty branch reserved for future duration parsing implementation
if vmcp.Spec.Operational != nil &&
vmcp.Spec.Operational.FailureHandling != nil &&
vmcp.Spec.Operational.FailureHandling.HealthCheckInterval != "" {
// Parse duration when implemented
}
return 30 * time.Second
}Recommendation: Either:
7. Hardcoded Health Probe ParametersAt LivenessProbe: ctrlutil.BuildHealthProbe("/health", "http", 30, 10, 5, 3),
ReadinessProbe: ctrlutil.BuildHealthProbe("/health", "http", 15, 5, 3, 3),While using the helper is good, the parameters are hardcoded. Recommendation:
8. Placeholder Capability DiscoveryAt // TODO: In a real implementation, we would:
// 1. Query each backend's capabilities
// 2. Apply tool filtering from Aggregation.Tools
// 3. Apply conflict resolution strategy
// 4. Count the final aggregated capabilities
//
// For now, we'll estimate based on number of backends
// This should be replaced with actual capability discovery
summary.ToolCount = readyBackends * 5 // Assume avg 5 tools per backendConcern: Placeholder/fake data in status can confuse users. Recommendation: Either:
9. TODO Comments - Need ResolutionMultiple TODO comments exist. These should either be:
Examples:
Recommendation: Review each TODO and decide whether to implement, defer to an issue, or remove. Additional Observations10. Deployment Update PatternAt if r.deploymentNeedsUpdate(ctx, deployment, vmcp, vmcpConfigChecksum) {
newDeployment := r.deploymentForVirtualMCPServer(ctx, vmcp, vmcpConfigChecksum)
deployment.Spec.Template = newDeployment.Spec.Template
deployment.Labels = newDeployment.Labels
deployment.Annotations = newDeployment.Annotations
if err := r.Update(ctx, deployment); err != nil {
// ...
}
}Observation: The update preserves specific fields. This is good practice for HPA compatibility. Consideration: The pattern doesn't check if an update conflict occurs. Most controllers handle conflicts with retry logic or by relying on the reconciliation loop to handle it naturally. Current approach is acceptable - the reconciliation loop will retry on conflicts. 11. RBAC RulesAt var vmcpRBACRules = []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"configmaps", "secrets"},
Verbs: []string{"get", "list", "watch"},
},
}Observation: Minimal permissions - good security practice! The vmcp server only needs read access to config and secrets. Question: Does vmcp need any additional permissions? If it needs to discover other resources or create/update resources, add them here. If not, this is good! ✅ 12. Status Reconciliation LogicAt The status update logic is comprehensive and handles multiple scenarios (all healthy, some healthy, degraded, failed, pending). Strength: Good use of backend health results to determine overall status. ✅ Minor consideration: Status updates happen on every reconciliation. Consider whether status-only subresource updates could reduce load, though for most use cases the current approach is fine. 13. Security Context HandlingAt func (r *VirtualMCPServerReconciler) buildSecurityContextsForVmcp(...) {
if r.PlatformDetector == nil {
r.PlatformDetector = ctrlutil.NewSharedPlatformDetector()
}
detectedPlatform, err := r.PlatformDetector.DetectPlatform(ctx)
if err != nil {
ctxLogger.Error(err, "Failed to detect platform, defaulting to Kubernetes", ...)
}
securityBuilder := kubernetes.NewSecurityContextBuilder(detectedPlatform)
return securityBuilder.BuildPodSecurityContext(), securityBuilder.BuildContainerSecurityContext()
}Excellent! ✅ This correctly uses the shared platform detection and security context builder pattern. This ensures compatibility across different Kubernetes distributions (OpenShift, standard K8s, etc.). Test CoverageFrom codecov, patch coverage is 55.53% with 418 lines missing coverage. The test files show good coverage of core scenarios:
Recommendation: Focus additional testing on:
Note: Test coverage above 50% for new code is reasonable, especially for controller logic. The missing coverage is likely in error paths and edge cases. Architecture DocumentationPer Recommendation: Consider updating:
This ensures the documentation stays in sync with the codebase. Summary Recommendations by PriorityHigh Priority (address before merge):
Medium Priority (consider for this PR or follow-up): Low Priority (nice to have): ConclusionThis is a well-implemented controller that follows Kubernetes best practices. The code is clean, well-organized, and properly uses existing helpers. The critical bug from the previous review has been fixed. The main areas for improvement are:
The controller is functional and follows good patterns. The recommendations above will make it more robust, maintainable, and scalable. Great work on this implementation! 🎉 |
|
@claude please review again |
|
Claude finished @yrobla's task —— View job Code Review CompleteI've completed a thorough review of the VirtualMCPServer controller implementation. The code is well-structured and follows most Kubernetes best practices. Here are my findings: SummaryThe VirtualMCPServer controller is a solid implementation with excellent separation of concerns, proper error handling, and good use of existing patterns. The critical bug from previous reviews has been fixed. The implementation includes comprehensive event recording, proper checksum handling, optimized watch handlers, and well-documented design decisions. Key Improvements Since Last Review ✅
Remaining Observations1. Watch Handler Optimizations - Partially CompleteMCPServer Watch: ✅ Optimized! The MCPExternalAuthConfig Watch: for _, vmcp := range vmcpList.Items {
requests = append(requests, reconcile.Request{...})
}This reconciles ALL VirtualMCPServers when any MCPExternalAuthConfig changes. Consider optimizing by:
Impact: Low-medium. Only causes extra reconciliations when auth configs change, which should be infrequent. 2. TODO Comments StatusSeveral TODOs remain in the code: Deployment file (
VMCPConfig file (
Recommendation: These TODOs are acceptable if the features aren't fully implemented yet. Consider:
3. Service Naming Pattern - Intentionally DifferentThe VirtualMCPServer uses different naming patterns than MCPServer/MCPRemoteProxy:
Excellent documentation (
This is intentional and well-documented. No change needed! ✅ 4. Status Update Pattern - Well DocumentedThe status update logic includes excellent documentation (
This is great documentation! ✅ Kubernetes Best Practices AssessmentStrengths ✅
Minor ConsiderationsChecksum Consistency: The implementation uses Conflict Handling: All Deployment Update Strategy: The selective field update strategy preserves Test CoverageFrom codecov: 60.49% patch coverage with 401 lines missing coverage. The test files show good coverage of core scenarios:
60% coverage is reasonable for controller code. Missing coverage is likely in error paths and edge cases. Consider adding tests for:
Architecture DocumentationThe PR includes updates to:
Great job keeping documentation in sync! ✅ Summary of Recommendations by PriorityHigh Priority (address before or shortly after merge):
Medium Priority (consider for follow-up):
Low Priority (nice to have):
ConclusionThis is an excellent controller implementation that follows Kubernetes best practices and addresses all critical issues from previous reviews. The code demonstrates:
All critical issues from previous reviews have been resolved. The remaining observations are minor optimizations and documentation suggestions that can be addressed in follow-up work. Great work on this implementation! The controller is ready for merge. 🎉 |
JAORMX
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed for Kubernetes best practices, security, and code reusability.
JAORMX
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional security concern identified.
JAORMX
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code organization suggestion.
JAORMX
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Highlighting excellent patterns.
66ac7a0 to
9843819
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 26 out of 26 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
2783275 to
5dcbc69
Compare
5dcbc69 to
d374dba
Compare
f50d51c to
c911fce
Compare
Implements the controller for VirtualMCPServer custom resource that orchestrates Virtual MCP Servers in Kubernetes, including backend discovery, configuration management, and resource orchestration. Closes: #2446
203cfc8 to
919322d
Compare
919322d to
65edfb9
Compare
|
@claude please review again |
|
Claude finished @yrobla's task —— View job Code Review CompleteI've completed a thorough review of the VirtualMCPServer controller implementation. The code is well-structured and follows most Kubernetes best practices. Here are my findings: SummaryThe VirtualMCPServer controller is a solid, production-ready implementation with excellent separation of concerns, proper error handling, and good use of existing patterns. The code demonstrates strong adherence to Kubernetes controller best practices and has addressed most issues from previous reviews. Key Strengths ✅
Critical Issues from Previous Reviews - StatusBased on the review comments visible in the PR, here's the status of key issues: ✅ Addressed Issues
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 42 out of 42 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Implements the controller for VirtualMCPServer custom resource that orchestrates Virtual MCP Servers in Kubernetes, including backend discovery, configuration management, and resource orchestration.
Closes: #2446