From 8c31125738e95d9c09d49e3672a487a0280d88d6 Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Mon, 25 Sep 2023 14:02:10 +0200 Subject: Revert "Remove unused endpoint name building from config-model" --- .../model/api/ApplicationClusterEndpoint.java | 75 +++++++++++++++++++++- 1 file changed, 72 insertions(+), 3 deletions(-) (limited to 'config-model-api') diff --git a/config-model-api/src/main/java/com/yahoo/config/model/api/ApplicationClusterEndpoint.java b/config-model-api/src/main/java/com/yahoo/config/model/api/ApplicationClusterEndpoint.java index 215439aa42a..69749ee6f96 100644 --- a/config-model-api/src/main/java/com/yahoo/config/model/api/ApplicationClusterEndpoint.java +++ b/config-model-api/src/main/java/com/yahoo/config/model/api/ApplicationClusterEndpoint.java @@ -2,8 +2,15 @@ package com.yahoo.config.model.api; +import com.yahoo.config.provision.ApplicationId; +import com.yahoo.config.provision.ClusterSpec; +import com.yahoo.config.provision.SystemName; + import java.util.List; import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; /** * Represents one endpoint for an application cluster @@ -147,21 +154,83 @@ public class ApplicationClusterEndpoint { } - public record DnsName(String name) implements Comparable { + public static class DnsName implements Comparable { + + private static final int MAX_LABEL_LENGTH = 63; + + private final String name; + + private DnsName(String name) { + this.name = name; + } public String value() { return name; } + public static DnsName sharedNameFrom(SystemName systemName, ClusterSpec.Id cluster, ApplicationId applicationId, String suffix) { + String name = dnsParts(systemName, cluster, applicationId) + .filter(Objects::nonNull) // remove null values that were "default" + .collect(Collectors.joining("--")); + return new DnsName(sanitize(name) + suffix); // Need to sanitize name since it is considered one label + } + + public static DnsName sharedL4NameFrom(SystemName systemName, ClusterSpec.Id cluster, ApplicationId applicationId, String suffix) { + String name = dnsParts(systemName, cluster, applicationId) + .filter(Objects::nonNull) // remove null values that were "default" + .map(DnsName::sanitize) + .collect(Collectors.joining(".")); + return new DnsName(name + suffix); + } + public static DnsName from(String name) { return new DnsName(name); } + private static Stream dnsParts(SystemName systemName, ClusterSpec.Id cluster, ApplicationId applicationId) { + return Stream.of( + nullIfDefault(cluster.value()), + systemPart(systemName), + nullIfDefault(applicationId.instance().value()), + applicationId.application().value(), + applicationId.tenant().value() + ); + } + + /** + * Remove any invalid characters from the hostnames + */ + private static String sanitize(String id) { + return shortenIfNeeded(id.toLowerCase() + .replace('_', '-') + .replaceAll("[^a-z0-9-]*", "")); + } + + /** + * Truncate the given string at the front so its length does not exceed 63 characters. + */ + private static String shortenIfNeeded(String id) { + return id.substring(Math.max(0, id.length() - MAX_LABEL_LENGTH)); + } + + private static String nullIfDefault(String string) { + return Optional.of(string).filter(s -> !s.equals("default")).orElse(null); + } + + private static String systemPart(SystemName systemName) { + return "cd".equals(systemName.value()) ? systemName.value() : null; + } + + @Override + public String toString() { + return "DnsName{" + + "name='" + name + '\'' + + '}'; + } + @Override public int compareTo(DnsName o) { return name.compareTo(o.name); } - } - } -- cgit v1.2.3