diff --git a/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/kubernetes/KubernetesLabelConverter.java b/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/kubernetes/KubernetesLabelConverter.java index f2c4c17cacc0..2931e36ab57a 100644 --- a/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/kubernetes/KubernetesLabelConverter.java +++ b/plugins/plugin-docker/che-plugin-openshift-client/src/main/java/org/eclipse/che/plugin/openshift/client/kubernetes/KubernetesLabelConverter.java @@ -64,33 +64,34 @@ public static String getCheServerLabelPrefix() { */ public static Map labelsToNames(Map labels) { Map names = new HashMap<>(); - if (labels != null) { - for (Map.Entry label : labels.entrySet()) { - - if (!hasConversionProblems(label)) { - - String key = label.getKey(); - String value = label.getValue(); - - // Convert keys: e.g. "che:server:4401/tcp:ref" -> - // "che.server.4401-tcp.ref" - key = convertLabelKey(key); - // Convert values: e.g. "/api" -> ".api" -- note values may - // include '-' e.g. "tomcat-debug" - value = convertLabelValue(value); - - // Add padding since DNS names must start and end with - // alphanumeric characters - key = addPadding(key); - value = addPadding(value); - - if (matchesKubernetesLabelRegex(key) && matchesKubernetesLabelRegex(value)) { - names.put(key, value); - } else { - LOG.error( - "Could not convert label {} into Kubernetes annotation: labels must be alphanumeric with ':' and '/'", - label.toString()); - } + if (labels == null) { + return names; + } + for (Map.Entry label : labels.entrySet()) { + + if (!hasConversionProblems(label)) { + + String key = label.getKey(); + String value = label.getValue(); + + // Convert keys: e.g. "che:server:4401/tcp:ref" -> + // "che.server.4401-tcp.ref" + key = convertLabelKey(key); + // Convert values: e.g. "/api" -> ".api" -- note values may + // include '-' e.g. "tomcat-debug" + value = convertLabelValue(value); + + // Add padding since DNS names must start and end with + // alphanumeric characters + key = addPadding(key); + value = addPadding(value); + + if (matchesKubernetesLabelRegex(key) && matchesKubernetesLabelRegex(value)) { + names.put(key, value); + } else { + LOG.error( + "Could not convert label {} into Kubernetes annotation: labels must be alphanumeric with ':' and '/'", + label.toString()); } } } @@ -105,27 +106,28 @@ public static Map labelsToNames(Map labels) { */ public static Map namesToLabels(Map names) { Map labels = new HashMap<>(); - if (names != null) { - for (Map.Entry entry: names.entrySet()){ - String key = entry.getKey(); - String value = entry.getValue(); - - // Remove padding - Matcher keyMatcher = CHE_SERVER_LABEL_KEY.matcher(key); - Matcher valueMatcher = CHE_SERVER_LABEL_KEY.matcher(value); - if (!keyMatcher.matches() || !valueMatcher.matches()) { - continue; - } - key = keyMatcher.group(1); - value = valueMatcher.group(1); + if (names == null) { + return labels; + } + for (Map.Entry entry: names.entrySet()){ + String key = entry.getKey(); + String value = entry.getValue(); + + // Remove padding + Matcher keyMatcher = CHE_SERVER_LABEL_KEY.matcher(key); + Matcher valueMatcher = CHE_SERVER_LABEL_KEY.matcher(value); + if (!keyMatcher.matches() || !valueMatcher.matches()) { + continue; + } + key = keyMatcher.group(1); + value = valueMatcher.group(1); - // Convert key: e.g. "che.server.4401_tcp.ref" -> "che:server:4401/tcp:ref" - key = key.replaceAll("\\.", ":").replaceAll("_", "/"); - // Convert value: e.g. Convert values: e.g. "_api" -> "/api" - value = value.replaceAll("_", "/"); + // Convert key: e.g. "che.server.4401_tcp.ref" -> "che:server:4401/tcp:ref" + key = key.replaceAll("\\.", ":").replaceAll("_", "/"); + // Convert value: e.g. Convert values: e.g. "_api" -> "/api" + value = value.replaceAll("_", "/"); - labels.put(key, value); - } + labels.put(key, value); } return labels; }