33import io .kubernetes .client .custom .IntOrString ;
44import io .kubernetes .client .custom .Quantity ;
55import io .kubernetes .client .openapi .ApiClient ;
6+ import io .kubernetes .client .openapi .ApiException ;
67import io .kubernetes .client .openapi .apis .CoreV1Api ;
78import io .kubernetes .client .openapi .models .*;
89import io .kubernetes .client .util .Config ;
10+ import io .sentrius .sso .core .dto .AgentRegistrationDTO ;
911import lombok .extern .slf4j .Slf4j ;
1012import org .springframework .beans .factory .annotation .Value ;
1113import org .springframework .stereotype .Service ;
@@ -116,7 +118,7 @@ public void deleteAgentById(String agentId) throws Exception {
116118 }
117119
118120
119- public V1Pod launchAgentPod (String agentId , String callbackUrl ) throws Exception {
121+ public V1Pod launchAgentPod (AgentRegistrationDTO agent ) throws Exception {
120122 var myAgentRegistry = "" ;
121123 if (agentRegistry != null ) {
122124 if ("local" .equalsIgnoreCase (agentRegistry )) {
@@ -125,6 +127,22 @@ public V1Pod launchAgentPod(String agentId, String callbackUrl) throws Exception
125127 myAgentRegistry += "/" ;
126128 }
127129 }
130+ String agentId = agent .getAgentName ();
131+ String callbackUrl = agent .getAgentCallbackUrl ();
132+ String agentType = agent .getAgentType ();
133+ String agentFile = "chat-helper.yaml" ;
134+ // TODO This should be pluggable
135+ switch (agentType ){
136+ case "chat" :
137+ agentFile = "chat-helper.yaml" ;
138+ break ;
139+ case "atpl-helper" :
140+ agentFile = "chat-atpl-helper.yaml" ;
141+ break ;
142+ case "default" :
143+ default :
144+ agentFile = "chat-helper.yaml" ;
145+ }
128146
129147 var constructedCallbackUrl = buildAgentCallbackUrl (agentId );
130148
@@ -142,7 +160,8 @@ public V1Pod launchAgentPod(String agentId, String callbackUrl) throws Exception
142160 .imagePullPolicy ("IfNotPresent" )
143161
144162 .args (List .of ("--spring.config.location=file:/config/agent.properties" ,
145- "--agent.namePrefix=" + agentId , "--agent.ai.config=/config/chat-helper.yaml" , "--agent.listen.websocket=true" ,
163+ "--agent.namePrefix=" + agentId , "--agent.ai.config=/config/" + agentFile , "--agent.listen" +
164+ ".websocket=true" ,
146165 "--agent.callback.url=" + constructedCallbackUrl
147166 ))
148167 .resources (new V1ResourceRequirements ()
@@ -169,24 +188,34 @@ public V1Pod launchAgentPod(String agentId, String callbackUrl) throws Exception
169188
170189 var createdPod = coreV1Api .createNamespacedPod (agentNamespace , pod ).execute ();
171190
172- // Create corresponding service for WebSocket routing
173- V1Service service = new V1Service ()
174- .metadata (new V1ObjectMeta ()
175- .name ("sentrius-agent-" + agentId )
176- .labels (Map .of ("agentId" , agentId )))
177- .spec (new V1ServiceSpec ()
178- .selector (Map .of ("agentId" , agentId ))
179- .ports (List .of (new V1ServicePort ()
180- .protocol ("TCP" )
181- .port (8090 )
182- .targetPort (new IntOrString (8090 ))
183- ))
184- .type ("ClusterIP" )
185- );
186-
187- log .info ("Created service pod: {} and service {}" , createdPod , service );
188- coreV1Api .createNamespacedService (agentNamespace , service ).execute ();
189-
191+ try {
192+ // Create corresponding service for WebSocket routing
193+ V1Service service = new V1Service ()
194+ .metadata (new V1ObjectMeta ()
195+ .name ("sentrius-agent-" + agentId )
196+ .labels (Map .of ("agentId" , agentId )))
197+ .spec (new V1ServiceSpec ()
198+ .selector (Map .of ("agentId" , agentId ))
199+ .ports (List .of (new V1ServicePort ()
200+ .protocol ("TCP" )
201+ .port (8090 )
202+ .targetPort (new IntOrString (8090 ))
203+ ))
204+ .type ("ClusterIP" )
205+ );
206+
207+ log .info ("Created service pod: {} and service {}" , createdPod , service );
208+ coreV1Api .createNamespacedService (agentNamespace , service ).execute ();
209+
210+ }catch (ApiException e ){
211+ if (e .getCode () == 409 ){
212+ log .info ("Service for agent {} already exists, skipping creation" , agentId );
213+ }
214+ else {
215+ throw e ;
216+ }
217+ }
190218 return createdPod ;
191219 }
220+
192221}
0 commit comments