diff --git a/src/main/java/de/worldiety/autocd/Main.java b/src/main/java/de/worldiety/autocd/Main.java index f9d532f..f108291 100644 --- a/src/main/java/de/worldiety/autocd/Main.java +++ b/src/main/java/de/worldiety/autocd/Main.java @@ -212,16 +212,14 @@ private static void removeWithDependencies(AutoCD autoCD, K8sClient k8sClient) { } /** - * Creates a name for a depending service out of the project name and a hashed registry image path from the main service + * Creates a name for a depending service * * @param main * @param other */ private static void setServiceNameForOtherImages(AutoCD main, AutoCD other) { if (other.getServiceName() == null) { - other.setServiceName(Util.hash( - System.getenv(Environment.CI_PROJECT_NAME.toString()) + main.getIdentifierRegistryImagePath()).substring(0, 20) - ); + other.setServiceName(Util.slugify(main.getServiceName() + "-" + main.getIdentifierRegistryImagePath())); } } diff --git a/src/main/java/de/worldiety/autocd/k8s/K8sClient.java b/src/main/java/de/worldiety/autocd/k8s/K8sClient.java index c11f7e3..68e6b21 100644 --- a/src/main/java/de/worldiety/autocd/k8s/K8sClient.java +++ b/src/main/java/de/worldiety/autocd/k8s/K8sClient.java @@ -149,7 +149,7 @@ private void deleteStatefulSet(V1StatefulSet set) { private V1StatefulSet getStatefulSet(AutoCD autoCD) { var meta = getNamespacedMeta(); var projName = System.getenv(Environment.CI_PROJECT_NAME.toString()); - meta.setName(Util.hash(getNamespaceString() + autoCD.getIdentifierRegistryImagePath() + projName).substring(0, 20)); + meta.setName(Util.slugify(projName + "--" + autoCD.getServiceName())); var labels = Map.of("k8s-app", getK8sApp(autoCD), "serviceName", getCleanServiceNameLabel(autoCD)); meta.setLabels(labels); @@ -576,6 +576,10 @@ private void createNamespace(V1Namespace nameSpace) { @NotNull private String getPVCName(Volume volume, @NotNull AutoCD autoCD) { + // TODO: we should switch to an unhashed version for readability reasons - what is the best way to migrate the old porjects? + //var str = getNamespaceString() + "--" + getServiceName(autoCD) + "--" + "claim" + autoCD.getVolumes().indexOf(volume); + //return Util.slugify(str); + var str = getNamespaceString() + "-" + getName() + "-" + autoCD.getIdentifierRegistryImagePath() + "-" + autoCD.getVolumes().indexOf(volume) + "-claim"; return hash(str).substring(0, 20); } @@ -627,7 +631,7 @@ private List getIngress(@NotNull AutoCD autoCD) { var ingress = new ExtensionsV1beta1Ingress(); ingress.setKind("Ingress"); var meta = getNamespacedMeta(); - meta.setName(Util.hash(subdomain + getNamespaceString() + "-" + getName() + "-ingress" + autoCD.getIdentifierRegistryImagePath()).substring(0, 20)); + meta.setName(Util.slugify(getNamespaceString() + "--" + subdomain + "--" + getName())); meta.setAnnotations(Map.of("cert-manager.io/cluster-issuer", "letsencrypt-prod", "kubernetes.io/ingress.class", "nginx")); @@ -647,7 +651,7 @@ private List getIngress(@NotNull AutoCD autoCD) { .withRules(rules.build()) .withTls(new ExtensionsV1beta1IngressTLSBuilder() .withHosts(subdomain) - .withSecretName(Util.hash(subdomain).substring(0, 10)) + .withSecretName(Util.slugify(getNamespaceString() + "--" + subdomain)) .build()) .build(); @@ -691,7 +695,14 @@ private String getServiceName(@NotNull AutoCD autoCD) { return autoCD.getServiceName(); } - return "service-" + Util.hash(getNamespaceString() + "-" + getName() + "-service").substring(0, 20); + String serviceName = "service--" + getName(); + + // Validate the length of the new service name - should be less then 63 characters: + // https://github.com/kubernetes/kubernetes/blob/master/pkg/apis/discovery/types.go#L122-L130 + if (serviceName.length() > 63) { + throw new IllegalArgumentException("The service name MUST NOT have more then 63 characters!"); + } + return serviceName; } @NotNull @@ -718,7 +729,7 @@ private V1Service getService(@NotNull AutoCD autoCD) { private ExtensionsV1beta1Deployment getDeployment(@NotNull AutoCD autoCD) { var meta = getNamespacedMeta(); var projName = System.getenv(Environment.CI_PROJECT_NAME.toString()); - meta.setName(Util.hash(getNamespaceString() + autoCD.getIdentifierRegistryImagePath() + projName)); + meta.setName(Util.slugify(projName + "--" + autoCD.getServiceName())); var labels = Map.of("k8s-app", getK8sApp(autoCD)); meta.setLabels(labels); @@ -819,7 +830,7 @@ private V1ContainerBuilder getV1ContainerBuilder(@NotNull AutoCD autoCD) { @NotNull private String getK8sApp(@NotNull AutoCD autoCD) { - return Util.hash(getNamespaceString() + "-" + getName() + "-" + Util.hash(autoCD.getIdentifierRegistryImagePath())).substring(0, 20) + hyphenedBuildType; + return Util.slugify(getNamespaceString() + "-" + autoCD.getServiceName()); } /** diff --git a/src/main/java/de/worldiety/autocd/util/Util.java b/src/main/java/de/worldiety/autocd/util/Util.java index f0b86f7..9a49855 100644 --- a/src/main/java/de/worldiety/autocd/util/Util.java +++ b/src/main/java/de/worldiety/autocd/util/Util.java @@ -51,7 +51,6 @@ private static String bytesToHex(byte[] hash) { return hexString.toString(); } - public static String hash(String toHash) { MessageDigest digest = null; try { @@ -64,4 +63,16 @@ public static String hash(String toHash) { return Util.bytesToHex(encodedhash); } + + public static String slugify(String toSlug) { + return slugify(toSlug, 255); + } + + public static String slugify(String toSlug, int maxLength) { + if (toSlug == null) { + return null; + } + String slugified = toSlug.toLowerCase().replaceAll("[^a-z0-9\\-]", ""); + return slugified.substring(0, Integer.min(slugified.length(), maxLength)); + } }