diff --git a/java/common/org.eclipse.theia.cloud.common/src/main/java/org/eclipse/theia/cloud/common/util/LabelsUtil.java b/java/common/org.eclipse.theia.cloud.common/src/main/java/org/eclipse/theia/cloud/common/util/LabelsUtil.java new file mode 100644 index 00000000..b43ea6ad --- /dev/null +++ b/java/common/org.eclipse.theia.cloud.common/src/main/java/org/eclipse/theia/cloud/common/util/LabelsUtil.java @@ -0,0 +1,26 @@ +package org.eclipse.theia.cloud.common.util; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.theia.cloud.common.k8s.resource.appdefinition.AppDefinitionSpec; +import org.eclipse.theia.cloud.common.k8s.resource.session.SessionSpec; + +public class LabelsUtil { + public static final String LABEL_CUSTOM_PREFIX = "theia-cloud.io"; + + public static final String LABEL_KEY_SESSION = "app.kubernetes.io/component"; + public static final String LABEL_VALUE_SESSION = "session"; + + public static final String LABEL_KEY_USER = LABEL_CUSTOM_PREFIX + "/user"; + public static final String LABEL_KEY_APPDEF = LABEL_CUSTOM_PREFIX + "/app-definition"; + + public static Map createSessionLabels(SessionSpec sessionSpec, AppDefinitionSpec appDefinitionSpec) { + Map labels = new HashMap<>(); + labels.put(LABEL_KEY_SESSION, LABEL_VALUE_SESSION); + String userName = sessionSpec.getUser().split("@")[0]; + labels.put(LABEL_KEY_USER, userName); + labels.put(LABEL_KEY_APPDEF, appDefinitionSpec.getName()); + return labels; + } +} diff --git a/java/operator/org.eclipse.theia.cloud.operator/src/main/java/org/eclipse/theia/cloud/operator/handler/session/EagerStartSessionHandler.java b/java/operator/org.eclipse.theia.cloud.operator/src/main/java/org/eclipse/theia/cloud/operator/handler/session/EagerStartSessionHandler.java index c9ddd205..88afaf30 100644 --- a/java/operator/org.eclipse.theia.cloud.operator/src/main/java/org/eclipse/theia/cloud/operator/handler/session/EagerStartSessionHandler.java +++ b/java/operator/org.eclipse.theia.cloud.operator/src/main/java/org/eclipse/theia/cloud/operator/handler/session/EagerStartSessionHandler.java @@ -21,6 +21,8 @@ import java.util.Collections; import java.util.List; import java.util.Map.Entry; +import java.util.Map; +import java.util.HashMap; import java.util.Optional; import org.apache.logging.log4j.LogManager; @@ -30,6 +32,7 @@ import org.eclipse.theia.cloud.common.k8s.resource.session.Session; import org.eclipse.theia.cloud.common.k8s.resource.session.SessionSpec; import org.eclipse.theia.cloud.common.util.JavaUtil; +import org.eclipse.theia.cloud.common.util.LabelsUtil; import org.eclipse.theia.cloud.operator.TheiaCloudOperatorArguments; import org.eclipse.theia.cloud.operator.handler.AddedHandlerUtil; import org.eclipse.theia.cloud.operator.ingress.IngressPathProvider; @@ -115,6 +118,25 @@ public boolean sessionAdded(Session session, String correlationId) { return false; } + try { + client.services().inNamespace(client.namespace()).withName(serviceToUse.get().getMetadata().getName()) + .edit(service -> { + LOGGER.info("Setting pod labels"); + Map labels = service.getMetadata().getLabels(); + if (labels == null) { + labels = new HashMap<>(); + service.getMetadata().setLabels(labels); + } + Map newLabels = LabelsUtil.createSessionLabels(spec, appDefinition.get().getSpec()); + labels.putAll(newLabels); + return service; + }); + } catch (KubernetesClientException e) { + LOGGER.error(formatLogMessage(correlationId, + "Error while adding labels to service " + (serviceToUse.get().getMetadata().getName())), e); + return false; + } + /* get the deployment for the service and add as owner */ Integer instance = TheiaCloudServiceUtil.getId(correlationId, appDefinition.get(), serviceToUse.get()); if (instance == null) { diff --git a/java/operator/org.eclipse.theia.cloud.operator/src/main/java/org/eclipse/theia/cloud/operator/handler/session/LazySessionHandler.java b/java/operator/org.eclipse.theia.cloud.operator/src/main/java/org/eclipse/theia/cloud/operator/handler/session/LazySessionHandler.java index 5cc0fd16..8ed86e05 100644 --- a/java/operator/org.eclipse.theia.cloud.operator/src/main/java/org/eclipse/theia/cloud/operator/handler/session/LazySessionHandler.java +++ b/java/operator/org.eclipse.theia.cloud.operator/src/main/java/org/eclipse/theia/cloud/operator/handler/session/LazySessionHandler.java @@ -26,6 +26,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.HashMap; import java.util.Optional; import org.apache.logging.log4j.LogManager; @@ -39,6 +40,7 @@ import org.eclipse.theia.cloud.common.k8s.resource.session.SessionSpec; import org.eclipse.theia.cloud.common.k8s.resource.session.SessionStatus; import org.eclipse.theia.cloud.common.k8s.resource.workspace.Workspace; +import org.eclipse.theia.cloud.common.util.LabelsUtil; import org.eclipse.theia.cloud.common.util.TheiaCloudError; import org.eclipse.theia.cloud.common.util.WorkspaceUtil; import org.eclipse.theia.cloud.operator.TheiaCloudOperatorArguments; @@ -444,6 +446,16 @@ protected void createAndApplyDeployment(String correlationId, String sessionReso } K8sUtil.loadAndCreateDeploymentWithOwnerReference(client.kubernetes(), client.namespace(), correlationId, deploymentYaml, Session.API, Session.KIND, sessionResourceName, sessionResourceUID, 0, deployment -> { + + LOGGER.info("Setting pod labels"); + Map labels = deployment.getSpec().getTemplate().getMetadata().getLabels(); + if (labels == null) { + labels = new HashMap<>(); + deployment.getSpec().getTemplate().getMetadata().setLabels(labels); + } + Map newLabels = LabelsUtil.createSessionLabels(session.getSpec(), appDefinition.getSpec()); + labels.putAll(newLabels); + pvName.ifPresent(name -> addVolumeClaim(deployment, name, appDefinition.getSpec())); bandwidthLimiter.limit(deployment, appDefinition.getSpec().getDownlinkLimit(), appDefinition.getSpec().getUplinkLimit(), correlationId);