Skip to content
This repository was archived by the owner on Nov 6, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/main/java/de/worldiety/autocd/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

Expand Down
23 changes: 17 additions & 6 deletions src/main/java/de/worldiety/autocd/k8s/K8sClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -627,7 +631,7 @@ private List<ExtensionsV1beta1Ingress> 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"));

Expand All @@ -647,7 +651,7 @@ private List<ExtensionsV1beta1Ingress> 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();

Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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());
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/de/worldiety/autocd/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ private static String bytesToHex(byte[] hash) {
return hexString.toString();
}


public static String hash(String toHash) {
MessageDigest digest = null;
try {
Expand All @@ -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));
}
}