Skip to content

Commit 47720a5

Browse files
xaijfaltermeier
authored andcommitted
Truncate labels longer than 63 chars
Signed-off-by: Olaf Lessenich <olessenich@eclipsesource.com>
1 parent b8b461c commit 47720a5

File tree

1 file changed

+15
-2
lines changed
  • java/common/org.eclipse.theia.cloud.common/src/main/java/org/eclipse/theia/cloud/common/util

1 file changed

+15
-2
lines changed

java/common/org.eclipse.theia.cloud.common/src/main/java/org/eclipse/theia/cloud/common/util/LabelsUtil.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import java.util.logging.Logger;
56

67
import org.eclipse.theia.cloud.common.k8s.resource.appdefinition.AppDefinitionSpec;
78
import org.eclipse.theia.cloud.common.k8s.resource.session.SessionSpec;
89

910
public class LabelsUtil {
11+
private static final Logger LOGGER = Logger.getLogger(LabelsUtil.class.getName());
12+
1013
public static final String LABEL_CUSTOM_PREFIX = "theia-cloud.io";
1114

1215
public static final String LABEL_KEY_SESSION = "app.kubernetes.io/component";
@@ -18,14 +21,24 @@ public class LabelsUtil {
1821
public static final String LABEL_KEY_USER = LABEL_CUSTOM_PREFIX + "/user";
1922
public static final String LABEL_KEY_APPDEF = LABEL_CUSTOM_PREFIX + "/app-definition";
2023

24+
private static final int MAX_LABEL_LENGTH = 63;
25+
26+
private static String truncateLabelValue(String value) {
27+
if (value.length() > MAX_LABEL_LENGTH) {
28+
LOGGER.warning("Label value truncated: " + value);
29+
return value.substring(0, MAX_LABEL_LENGTH);
30+
}
31+
return value;
32+
}
33+
2134
public static Map<String, String> createSessionLabels(SessionSpec sessionSpec,
2235
AppDefinitionSpec appDefinitionSpec) {
2336
Map<String, String> labels = new HashMap<>();
2437
labels.put(LABEL_KEY_SESSION, LABEL_VALUE_SESSION);
2538
labels.put(LABEL_KEY_THEIACLOUD, LABEL_VALUE_THEIACLOUD);
2639
String sanitizedUser = sessionSpec.getUser().replaceAll("@", "_at_").replaceAll("[^a-zA-Z0-9]", "_");
27-
labels.put(LABEL_KEY_USER, sanitizedUser);
28-
labels.put(LABEL_KEY_APPDEF, appDefinitionSpec.getName());
40+
labels.put(LABEL_KEY_USER, truncateLabelValue(sanitizedUser));
41+
labels.put(LABEL_KEY_APPDEF, truncateLabelValue(appDefinitionSpec.getName()));
2942
return labels;
3043
}
3144
}

0 commit comments

Comments
 (0)