@@ -18,8 +18,11 @@ limitations under the License.
1818package main
1919
2020import (
21+ "context"
2122 "crypto/tls"
23+ "errors"
2224 "flag"
25+ "net/http"
2326 "os"
2427 "time"
2528
@@ -28,11 +31,14 @@ import (
2831 _ "k8s.io/client-go/plugin/pkg/client/auth"
2932
3033 autoscalingv2 "k8s.io/api/autoscaling/v2"
34+ corev1 "k8s.io/api/core/v1"
3135 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3236 "k8s.io/apimachinery/pkg/runtime"
3337 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3438 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3539 ctrl "sigs.k8s.io/controller-runtime"
40+ "sigs.k8s.io/controller-runtime/pkg/cache"
41+ "sigs.k8s.io/controller-runtime/pkg/client"
3642 "sigs.k8s.io/controller-runtime/pkg/healthz"
3743 "sigs.k8s.io/controller-runtime/pkg/log/zap"
3844 "sigs.k8s.io/controller-runtime/pkg/metrics/filters"
@@ -46,6 +52,7 @@ import (
4652 "github.com/gitcommitankit/agentrax/internal/controller"
4753 "github.com/gitcommitankit/agentrax/internal/metrics"
4854 "github.com/gitcommitankit/agentrax/internal/quota"
55+ "github.com/gitcommitankit/agentrax/internal/registry"
4956 "github.com/gitcommitankit/agentrax/internal/rollout"
5057 agentraxwebhook "github.com/gitcommitankit/agentrax/internal/webhook"
5158 // +kubebuilder:scaffold:imports
5663 setupLog = ctrl .Log .WithName ("setup" )
5764)
5865
66+ // registryServerRunnable runs the MCP registry HTTP server on every manager replica.
67+ type registryServerRunnable struct {
68+ registryAddr string
69+ mcpRegistry * registry.Registry
70+ }
71+
72+ // Start starts the MCP discovery HTTP server and listens until context cancellation.
73+ func (r * registryServerRunnable ) Start (ctx context.Context ) error {
74+ r .mcpRegistry .Start (ctx )
75+ srv := & http.Server {
76+ Addr : r .registryAddr ,
77+ Handler : r .mcpRegistry .Handler (),
78+ ReadHeaderTimeout : 10 * time .Second ,
79+ ReadTimeout : 30 * time .Second ,
80+ WriteTimeout : 30 * time .Second ,
81+ IdleTimeout : 120 * time .Second ,
82+ }
83+ go func () {
84+ <- ctx .Done ()
85+ shutdownCtx , cancel := context .WithTimeout (context .Background (), 10 * time .Second )
86+ defer cancel ()
87+ _ = srv .Shutdown (shutdownCtx )
88+ }()
89+ setupLog .Info ("starting MCP discovery registry server" , "addr" , r .registryAddr )
90+ if err := srv .ListenAndServe (); err != nil && ! errors .Is (err , http .ErrServerClosed ) {
91+ return err
92+ }
93+ return nil
94+ }
95+
96+ // NeedLeaderElection returns false so the registry server runs across all manager replicas.
97+ func (r * registryServerRunnable ) NeedLeaderElection () bool {
98+ return false
99+ }
100+
59101// init registers all Kubernetes core, CRD, and monitoring schemes.
60102func init () {
61103 utilruntime .Must (clientgoscheme .AddToScheme (scheme ))
@@ -68,6 +110,8 @@ func init() {
68110 // +kubebuilder:scaffold:scheme
69111}
70112
113+ // +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;patch
114+
71115// main is the entrypoint for the Agentrax controller manager binary.
72116func main () {
73117 var metricsAddr string
@@ -79,6 +123,7 @@ func main() {
79123 var prometheusURL string
80124 var gatewayName string
81125 var gatewayNamespace string
126+ var registryAddr string
82127 var tlsOpts []func (* tls.Config )
83128 flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metrics endpoint binds to. " +
84129 "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service." )
@@ -99,6 +144,8 @@ func main() {
99144 "Name of the Gateway API Gateway object used for canary traffic splitting." )
100145 flag .StringVar (& gatewayNamespace , "gateway-namespace" , "agentrax-system" ,
101146 "Namespace of the Gateway API Gateway object used for canary traffic splitting." )
147+ flag .StringVar (& registryAddr , "registry-bind-address" , ":9090" ,
148+ "The address the MCP discovery registry HTTP endpoint binds to." )
102149 opts := zap.Options {
103150 Development : true ,
104151 }
@@ -159,13 +206,28 @@ func main() {
159206 metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
160207 }
161208
209+ // Determine registry namespace once for both manager cache and registry construction.
210+ registryNamespace := os .Getenv ("POD_NAMESPACE" )
211+ if registryNamespace == "" {
212+ registryNamespace = "agentrax-system"
213+ }
214+
162215 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
163216 Scheme : scheme ,
164217 Metrics : metricsServerOptions ,
165218 WebhookServer : webhookServer ,
166219 HealthProbeBindAddress : probeAddr ,
167220 LeaderElection : enableLeaderElection ,
168221 LeaderElectionID : "ddf1aac5.agentrax.io" ,
222+ Cache : cache.Options {
223+ ByObject : map [client.Object ]cache.ByObject {
224+ & corev1.ConfigMap {}: {
225+ Namespaces : map [string ]cache.Config {
226+ registryNamespace : {},
227+ },
228+ },
229+ },
230+ },
169231 // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
170232 // when the Manager ends. This requires the binary to immediately end when the
171233 // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
@@ -204,12 +266,33 @@ func main() {
204266 setupLog .Info ("canary rollout disabled (no --prometheus-url)" )
205267 }
206268
207- if err = (& controller.AgentDeploymentReconciler {
269+ // Initialize MCP discovery registry and registrar.
270+ mcpRegistry := registry .NewRegistry (mgr .GetClient (), registryNamespace , registry .DefaultTTL )
271+ mcpRegistrar := registry .NewRegistrar (mcpRegistry , registry .NewHTTPMCPClient ())
272+
273+ if registryAddr != "" && registryAddr != "0" {
274+ registryRunnable := & registryServerRunnable {
275+ registryAddr : registryAddr ,
276+ mcpRegistry : mcpRegistry ,
277+ }
278+ if err := mgr .Add (registryRunnable ); err != nil {
279+ setupLog .Error (err , "unable to add registry server to manager" )
280+ os .Exit (1 )
281+ }
282+ }
283+
284+ agentDeploymentReconciler := & controller.AgentDeploymentReconciler {
208285 Client : mgr .GetClient (),
209286 Scheme : mgr .GetScheme (),
210287 GPUResourceName : gpuResourceName ,
211288 CanaryController : canaryController ,
212- }).SetupWithManager (mgr ); err != nil {
289+ Registrar : mcpRegistrar ,
290+ }
291+ if canaryController != nil {
292+ canaryController .Registrar = mcpRegistrar
293+ }
294+
295+ if err = agentDeploymentReconciler .SetupWithManager (mgr ); err != nil {
213296 setupLog .Error (err , "unable to create controller" , "controller" , "AgentDeployment" )
214297 os .Exit (1 )
215298 }
0 commit comments