Skip to content

Commit

Permalink
Make it easier to read.
Browse files Browse the repository at this point in the history
Signed-off-by: David Festal <dfestal@redhat.com>
  • Loading branch information
davidfestal committed May 29, 2017
1 parent 4801916 commit 6401f22
Showing 1 changed file with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,34 @@ public static String getCheServerLabelPrefix() {
*/
public static Map<String, String> labelsToNames(Map<String, String> labels) {
Map<String, String> names = new HashMap<>();
if (labels != null) {
for (Map.Entry<String, String> 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<String, String> 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());
}
}
}
Expand All @@ -105,27 +106,28 @@ public static Map<String, String> labelsToNames(Map<String, String> labels) {
*/
public static Map<String, String> namesToLabels(Map<String, String> names) {
Map<String, String> labels = new HashMap<>();
if (names != null) {
for (Map.Entry<String, String> 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<String, String> 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;
}
Expand Down

0 comments on commit 6401f22

Please sign in to comment.