Skip to content

Commit 38551be

Browse files
Merge pull request #8 from gitcommitankit/phase-5
feat: implement MCP discovery registry and consolidated RESTful routes
2 parents b51ac0c + d6dff68 commit 38551be

22 files changed

Lines changed: 3160 additions & 19 deletions

‎.golangci.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ linters:
2121
enable:
2222
- dupl
2323
- errcheck
24-
- exportloopref
24+
- copyloopvar
2525
- ginkgolinter
2626
- goconst
2727
- gocyclo

‎Makefile‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ lint: golangci-lint ## Run golangci-lint linter
7676
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
7777
$(GOLANGCI_LINT) run --fix
7878

79+
.PHONY: setup-git-hooks
80+
setup-git-hooks: ## Install pre-commit and pre-push Git hooks.
81+
./hack/setup-git-hooks.sh
82+
7983
##@ Build
8084

8185
.PHONY: build

‎api/v1alpha1/error_rate_test.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ func TestParseErrorRate(t *testing.T) {
4949
{" 5%", 0, true},
5050
}
5151
for _, tc := range tests {
52-
tc := tc
5352
t.Run(tc.input, func(t *testing.T) {
5453
t.Parallel()
5554
got, err := agentraxv1alpha1.ParseErrorRate(tc.input)

‎cmd/main.go‎

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ limitations under the License.
1818
package main
1919

2020
import (
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
@@ -56,6 +63,41 @@ var (
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.
60102
func 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.
72116
func 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
}

‎config/manager/kustomization.yaml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
resources:
22
- manager.yaml
3+
- registry_service.yaml
4+
apiVersion: kustomize.config.k8s.io/v1beta1
5+
kind: Kustomization
6+
images:
7+
- name: controller
8+
newName: controller
9+
newTag: v0.1.0
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: v1
2+
kind: Service
3+
metadata:
4+
name: agentrax-registry
5+
namespace: system
6+
labels:
7+
app.kubernetes.io/name: agentrax
8+
app.kubernetes.io/component: registry
9+
spec:
10+
selector:
11+
control-plane: controller-manager
12+
ports:
13+
- name: registry
14+
port: 9090
15+
targetPort: 9090
16+
protocol: TCP
17+
type: ClusterIP

‎config/rbac/role.yaml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ kind: ClusterRole
44
metadata:
55
name: manager-role
66
rules:
7+
- apiGroups:
8+
- ""
9+
resources:
10+
- configmaps
11+
verbs:
12+
- create
13+
- get
14+
- list
15+
- patch
16+
- update
17+
- watch
718
- apiGroups:
819
- agentrax.io
920
resources:

0 commit comments

Comments
 (0)