26
26
import java .util .Collections ;
27
27
import java .util .List ;
28
28
import java .util .Map ;
29
+ import java .util .HashMap ;
29
30
import java .util .Optional ;
30
31
31
32
import org .apache .logging .log4j .LogManager ;
39
40
import org .eclipse .theia .cloud .common .k8s .resource .session .SessionSpec ;
40
41
import org .eclipse .theia .cloud .common .k8s .resource .session .SessionStatus ;
41
42
import org .eclipse .theia .cloud .common .k8s .resource .workspace .Workspace ;
43
+ import org .eclipse .theia .cloud .common .util .LabelsUtil ;
42
44
import org .eclipse .theia .cloud .common .util .TheiaCloudError ;
43
45
import org .eclipse .theia .cloud .common .util .WorkspaceUtil ;
44
46
import org .eclipse .theia .cloud .operator .TheiaCloudOperatorArguments ;
@@ -155,6 +157,10 @@ protected boolean doSessionAdded(Session session, String correlationId) {
155
157
return false ;
156
158
}
157
159
AppDefinition appDefinition = optionalAppDefinition .get ();
160
+ AppDefinitionSpec appDefinitionSpec = appDefinition .getSpec ();
161
+
162
+ /* label maps */
163
+ Map <String , String > labelsToAdd = LabelsUtil .createSessionLabels (session .getSpec (), appDefinitionSpec );
158
164
159
165
if (hasMaxInstancesReached (appDefinition , session , correlationId )) {
160
166
client .sessions ().updateStatus (correlationId , session , s -> {
@@ -200,7 +206,7 @@ protected boolean doSessionAdded(Session session, String correlationId) {
200
206
}
201
207
202
208
Optional <Service > serviceToUse = createAndApplyService (correlationId , sessionResourceName , sessionResourceUID ,
203
- session , appDefinition . getSpec () , arguments .isUseKeycloak ());
209
+ session , appDefinitionSpec , arguments .isUseKeycloak (), labelsToAdd );
204
210
if (serviceToUse .isEmpty ()) {
205
211
LOGGER .error (formatLogMessage (correlationId , "Unable to create service for session " + sessionSpec ));
206
212
client .sessions ().updateStatus (correlationId , session , s -> {
@@ -226,9 +232,9 @@ protected boolean doSessionAdded(Session session, String correlationId) {
226
232
// this handler
227
233
return true ;
228
234
}
229
- createAndApplyEmailConfigMap (correlationId , sessionResourceName , sessionResourceUID , session );
235
+ createAndApplyEmailConfigMap (correlationId , sessionResourceName , sessionResourceUID , session , labelsToAdd );
230
236
createAndApplyProxyConfigMap (correlationId , sessionResourceName , sessionResourceUID , session ,
231
- appDefinition );
237
+ appDefinition , labelsToAdd );
232
238
}
233
239
234
240
/* Create deployment for this session */
@@ -247,7 +253,7 @@ protected boolean doSessionAdded(Session session, String correlationId) {
247
253
248
254
Optional <String > storageName = getStorageName (session , correlationId );
249
255
createAndApplyDeployment (correlationId , sessionResourceName , sessionResourceUID , session , appDefinition ,
250
- storageName , arguments .isUseKeycloak ());
256
+ storageName , arguments .isUseKeycloak (), labelsToAdd );
251
257
252
258
/* adjust the ingress */
253
259
String host ;
@@ -371,7 +377,8 @@ protected Optional<String> getStorageName(Session session, String correlationId)
371
377
}
372
378
373
379
protected Optional <Service > createAndApplyService (String correlationId , String sessionResourceName ,
374
- String sessionResourceUID , Session session , AppDefinitionSpec appDefinitionSpec , boolean useOAuth2Proxy ) {
380
+ String sessionResourceUID , Session session , AppDefinitionSpec appDefinitionSpec , boolean useOAuth2Proxy ,
381
+ Map <String , String > labelsToAdd ) {
375
382
Map <String , String > replacements = TheiaCloudServiceUtil .getServiceReplacements (client .namespace (), session ,
376
383
appDefinitionSpec );
377
384
String templateYaml = useOAuth2Proxy ? AddedHandlerUtil .TEMPLATE_SERVICE_YAML
@@ -385,11 +392,11 @@ protected Optional<Service> createAndApplyService(String correlationId, String s
385
392
return Optional .empty ();
386
393
}
387
394
return K8sUtil .loadAndCreateServiceWithOwnerReference (client .kubernetes (), client .namespace (), correlationId ,
388
- serviceYaml , Session .API , Session .KIND , sessionResourceName , sessionResourceUID , 0 );
395
+ serviceYaml , Session .API , Session .KIND , sessionResourceName , sessionResourceUID , 0 , labelsToAdd );
389
396
}
390
397
391
398
protected void createAndApplyEmailConfigMap (String correlationId , String sessionResourceName ,
392
- String sessionResourceUID , Session session ) {
399
+ String sessionResourceUID , Session session , Map < String , String > labelsToAdd ) {
393
400
Map <String , String > replacements = TheiaCloudConfigMapUtil .getEmailConfigMapReplacements (client .namespace (),
394
401
session );
395
402
String configMapYaml ;
@@ -401,14 +408,15 @@ protected void createAndApplyEmailConfigMap(String correlationId, String session
401
408
return ;
402
409
}
403
410
K8sUtil .loadAndCreateConfigMapWithOwnerReference (client .kubernetes (), client .namespace (), correlationId ,
404
- configMapYaml , Session .API , Session .KIND , sessionResourceName , sessionResourceUID , 0 , configmap -> {
411
+ configMapYaml , Session .API , Session .KIND , sessionResourceName , sessionResourceUID , 0 ,
412
+ labelsToAdd , configmap -> {
405
413
configmap .setData (Collections .singletonMap (AddedHandlerUtil .FILENAME_AUTHENTICATED_EMAILS_LIST ,
406
414
session .getSpec ().getUser ()));
407
415
});
408
416
}
409
417
410
418
protected void createAndApplyProxyConfigMap (String correlationId , String sessionResourceName ,
411
- String sessionResourceUID , Session session , AppDefinition appDefinition ) {
419
+ String sessionResourceUID , Session session , AppDefinition appDefinition , Map < String , String > labelsToAdd ) {
412
420
Map <String , String > replacements = TheiaCloudConfigMapUtil .getProxyConfigMapReplacements (client .namespace (),
413
421
session );
414
422
String configMapYaml ;
@@ -420,7 +428,8 @@ protected void createAndApplyProxyConfigMap(String correlationId, String session
420
428
return ;
421
429
}
422
430
K8sUtil .loadAndCreateConfigMapWithOwnerReference (client .kubernetes (), client .namespace (), correlationId ,
423
- configMapYaml , Session .API , Session .KIND , sessionResourceName , sessionResourceUID , 0 , configMap -> {
431
+ configMapYaml , Session .API , Session .KIND , sessionResourceName , sessionResourceUID , 0 ,
432
+ labelsToAdd , configMap -> {
424
433
String host = arguments .getInstancesHost () + ingressPathProvider .getPath (appDefinition , session );
425
434
int port = appDefinition .getSpec ().getPort ();
426
435
AddedHandlerUtil .updateProxyConfigMap (client .kubernetes (), client .namespace (), configMap , host ,
@@ -429,7 +438,8 @@ protected void createAndApplyProxyConfigMap(String correlationId, String session
429
438
}
430
439
431
440
protected void createAndApplyDeployment (String correlationId , String sessionResourceName , String sessionResourceUID ,
432
- Session session , AppDefinition appDefinition , Optional <String > pvName , boolean useOAuth2Proxy ) {
441
+ Session session , AppDefinition appDefinition , Optional <String > pvName , boolean useOAuth2Proxy ,
442
+ Map <String , String > labelsToAdd ) {
433
443
Map <String , String > replacements = deploymentReplacements .getReplacements (client .namespace (), appDefinition ,
434
444
session );
435
445
String templateYaml = useOAuth2Proxy ? AddedHandlerUtil .TEMPLATE_DEPLOYMENT_YAML
@@ -443,7 +453,17 @@ protected void createAndApplyDeployment(String correlationId, String sessionReso
443
453
return ;
444
454
}
445
455
K8sUtil .loadAndCreateDeploymentWithOwnerReference (client .kubernetes (), client .namespace (), correlationId ,
446
- deploymentYaml , Session .API , Session .KIND , sessionResourceName , sessionResourceUID , 0 , deployment -> {
456
+ deploymentYaml , Session .API , Session .KIND , sessionResourceName , sessionResourceUID , 0 ,
457
+ labelsToAdd , deployment -> {
458
+
459
+ LOGGER .debug ("Setting session labels" );
460
+ Map <String , String > labels = deployment .getSpec ().getTemplate ().getMetadata ().getLabels ();
461
+ if (labels == null ) {
462
+ labels = new HashMap <>();
463
+ deployment .getSpec ().getTemplate ().getMetadata ().setLabels (labels );
464
+ }
465
+ labels .putAll (labelsToAdd );
466
+
447
467
pvName .ifPresent (name -> addVolumeClaim (deployment , name , appDefinition .getSpec ()));
448
468
bandwidthLimiter .limit (deployment , appDefinition .getSpec ().getDownlinkLimit (),
449
469
appDefinition .getSpec ().getUplinkLimit (), correlationId );
0 commit comments