Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -1968,12 +1968,19 @@ protected Pair<String, String> getNextVmHostAndDisplayName(AutoScaleVmGroupVO as
RandomStringUtils.random(VM_HOSTNAME_RANDOM_SUFFIX_LENGTH, 0, 0, true, false, (char[])null, new SecureRandom()).toLowerCase();
// Truncate vm group name because max length of vm name is 63
int subStringLength = Math.min(asGroup.getName().length(), 63 - VM_HOSTNAME_PREFIX.length() - vmHostNameSuffix.length());
String displayName = VM_HOSTNAME_PREFIX + asGroup.getName().substring(0, subStringLength) + vmHostNameSuffix;
String hostName = displayName;
if (isWindows) {
hostName = displayName.substring(Math.max(0, displayName.length() - 15));
String name = VM_HOSTNAME_PREFIX + asGroup.getName().substring(0, subStringLength) + vmHostNameSuffix;
if (!isWindows) {
return new Pair<>(name, name);
}
return new Pair<>(hostName, displayName);
String hostName = name.substring(Math.max(0, name.length() - 15));
if (Character.isLetterOrDigit(hostName.charAt(0))) {
return new Pair<>(hostName, name);
}
String temp = name.substring(0, Math.max(0, name.length() - 15)).replaceAll("[^a-zA-Z0-9]", "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edge case: all of the last 15 chars are special chars or whitespace. I think we better do a replace first and then take the last 15.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DaanHoogland I don't think this case can happen.
The only user-defined part in the VM name is the name of the autoscale group, and we've checks for that to only contain letters, numbers and hyphen. checkAutoScaleVmGroupName` does that

if (!temp.isEmpty()) {
return new Pair<>(temp.charAt(temp.length() - 1) + hostName.substring(1), name);
}
return new Pair<>('a' + hostName.substring(1), name);
}

@Override
Expand Down
Loading