@@ -18,6 +18,7 @@ package controller
1818
1919import (
2020 "context"
21+ "kagent.dev/kmcp/pkg/agentgateway"
2122
2223 "k8s.io/apimachinery/pkg/runtime"
2324 ctrl "sigs.k8s.io/controller-runtime"
@@ -49,7 +50,29 @@ type MCPServerReconciler struct {
4950func (r * MCPServerReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
5051 _ = log .FromContext (ctx )
5152
52- // TODO(user): your logic here
53+ // Fetch the MCPServer instance
54+ mcpServer := & kagentdevv1alpha1.MCPServer {}
55+ if err := r .Get (ctx , req .NamespacedName , mcpServer ); err != nil {
56+ // If the resource is not found, we can ignore the error since it will be requeued later
57+ return ctrl.Result {}, client .IgnoreNotFound (err )
58+ }
59+
60+ t := agentgateway .NewAgentGatewayTranslator (r .Scheme )
61+ outputs , err := t .TranslateAgentGatewayOutputs (mcpServer )
62+ if err != nil {
63+ log .FromContext (ctx ).Error (err , "Failed to translate MCPServer outputs" )
64+ r .reconcileStatus (ctx , mcpServer , err )
65+ return ctrl.Result {}, err
66+ }
67+
68+ err = r .reconcileOutputs (ctx , outputs )
69+ if err != nil {
70+ log .FromContext (ctx ).Error (err , "Failed to reconcile outputs" )
71+ r .reconcileStatus (ctx , mcpServer , err )
72+ return ctrl.Result {}, err
73+ }
74+
75+ r .reconcileStatus (ctx , mcpServer , nil )
5376
5477 return ctrl.Result {}, nil
5578}
@@ -61,3 +84,50 @@ func (r *MCPServerReconciler) SetupWithManager(mgr ctrl.Manager) error {
6184 Named ("mcpserver" ).
6285 Complete (r )
6386}
87+
88+ func (r * MCPServerReconciler ) reconcileOutputs (ctx context.Context , outputs * agentgateway.AgentGatewayOutputs ) error {
89+ // upsert the outputs to the cluster
90+ if outputs .Deployment != nil {
91+ if err := upsertOutput (ctx , r .Client , outputs .Deployment ); err != nil {
92+ return err
93+ }
94+ }
95+ if outputs .Service != nil {
96+ if err := upsertOutput (ctx , r .Client , outputs .Service ); err != nil {
97+ return err
98+ }
99+ }
100+ if outputs .ConfigMap != nil {
101+ if err := upsertOutput (ctx , r .Client , outputs .ConfigMap ); err != nil {
102+ return err
103+ }
104+ }
105+
106+ return nil
107+ }
108+
109+ func (r * MCPServerReconciler ) reconcileStatus (ctx context.Context , server * kagentdevv1alpha1.MCPServer , err error ) {
110+ // TODO: Implement status reconciliation logic
111+ // log for now
112+ log .FromContext (ctx ).Info ("Reconcile status" , "server" , server .Name , "error" , err )
113+ }
114+
115+ func upsertOutput (ctx context.Context , kube client.Client , output client.Object ) error {
116+ existing := output .DeepCopyObject ().(client.Object )
117+ if err := kube .Get (ctx , client .ObjectKeyFromObject (existing ), existing ); err != nil {
118+ if client .IgnoreNotFound (err ) != nil {
119+ return err
120+ }
121+ // If not found, create it
122+ if err := kube .Create (ctx , output ); err != nil {
123+ return err
124+ }
125+ } else {
126+ // If found, update it
127+ output .SetResourceVersion (existing .GetResourceVersion ())
128+ if err := kube .Update (ctx , output ); err != nil {
129+ return err
130+ }
131+ }
132+ return nil
133+ }
0 commit comments