-
Notifications
You must be signed in to change notification settings - Fork 1.2k
server: trim autoscale Windows VM hostname #11327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
36e636a
0c34a76
aa21706
b97ef66
4c9de62
4c9ff4c
16240b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,6 @@ | |
|
||
import javax.inject.Inject; | ||
|
||
import com.cloud.network.NetworkModel; | ||
import org.apache.cloudstack.acl.ControlledEntity; | ||
import org.apache.cloudstack.affinity.AffinityGroupVO; | ||
import org.apache.cloudstack.affinity.dao.AffinityGroupDao; | ||
|
@@ -113,6 +112,7 @@ | |
import com.cloud.network.Network; | ||
import com.cloud.network.Network.Capability; | ||
import com.cloud.network.Network.IpAddresses; | ||
import com.cloud.network.NetworkModel; | ||
import com.cloud.network.as.AutoScaleCounter.AutoScaleCounterParam; | ||
import com.cloud.network.as.dao.AutoScalePolicyConditionMapDao; | ||
import com.cloud.network.as.dao.AutoScalePolicyDao; | ||
|
@@ -146,7 +146,9 @@ | |
import com.cloud.server.ResourceTag; | ||
import com.cloud.service.ServiceOfferingVO; | ||
import com.cloud.service.dao.ServiceOfferingDao; | ||
import com.cloud.storage.GuestOSVO; | ||
import com.cloud.storage.dao.DiskOfferingDao; | ||
import com.cloud.storage.dao.GuestOSDao; | ||
import com.cloud.template.TemplateManager; | ||
import com.cloud.template.VirtualMachineTemplate; | ||
import com.cloud.user.Account; | ||
|
@@ -280,6 +282,8 @@ public class AutoScaleManagerImpl extends ManagerBase implements AutoScaleManage | |
private NetworkOfferingDao networkOfferingDao; | ||
@Inject | ||
private VirtualMachineManager virtualMachineManager; | ||
@Inject | ||
GuestOSDao guestOSDao; | ||
|
||
private static final String PARAM_ROOT_DISK_SIZE = "rootdisksize"; | ||
private static final String PARAM_DISK_OFFERING_ID = "diskofferingid"; | ||
|
@@ -1810,26 +1814,28 @@ protected UserVm createNewVM(AutoScaleVmGroupVO asGroup) { | |
List<Long> affinityGroupIdList = getVmAffinityGroupId(deployParams); | ||
updateVmDetails(deployParams, customParameters); | ||
|
||
String vmHostName = getNextVmHostName(asGroup); | ||
Pair<String, String> vmHostAndDisplayName = getNextVmHostAndDisplayName(asGroup, template); | ||
String vmHostName = vmHostAndDisplayName.first(); | ||
String vmDisplayName = vmHostAndDisplayName.second(); | ||
asGroup.setNextVmSeq(asGroup.getNextVmSeq() + 1); | ||
autoScaleVmGroupDao.persist(asGroup); | ||
|
||
if (zone.getNetworkType() == NetworkType.Basic) { | ||
vm = userVmService.createBasicSecurityGroupVirtualMachine(zone, serviceOffering, template, null, owner, vmHostName, | ||
vmHostName, diskOfferingId, dataDiskSize, null, null, | ||
vmDisplayName, diskOfferingId, dataDiskSize, null, null, | ||
hypervisorType, HTTPMethod.GET, userData, userDataId, userDataDetails, sshKeyPairs, | ||
null, null, true, null, affinityGroupIdList, customParameters, null, null, null, | ||
null, true, overrideDiskOfferingId, null, null); | ||
} else { | ||
if (networkModel.checkSecurityGroupSupportForNetwork(owner, zone, networkIds, | ||
Collections.emptyList())) { | ||
vm = userVmService.createAdvancedSecurityGroupVirtualMachine(zone, serviceOffering, template, networkIds, null, | ||
owner, vmHostName,vmHostName, diskOfferingId, dataDiskSize, null, null, | ||
owner, vmHostName, vmDisplayName, diskOfferingId, dataDiskSize, null, null, | ||
hypervisorType, HTTPMethod.GET, userData, userDataId, userDataDetails, sshKeyPairs, | ||
null, null, true, null, affinityGroupIdList, customParameters, null, null, null, | ||
null, true, overrideDiskOfferingId, null, null, null); | ||
} else { | ||
vm = userVmService.createAdvancedVirtualMachine(zone, serviceOffering, template, networkIds, owner, vmHostName, vmHostName, | ||
vm = userVmService.createAdvancedVirtualMachine(zone, serviceOffering, template, networkIds, owner, vmHostName, vmDisplayName, | ||
diskOfferingId, dataDiskSize, null, null, | ||
hypervisorType, HTTPMethod.GET, userData, userDataId, userDataDetails, sshKeyPairs, | ||
null, addrs, true, null, affinityGroupIdList, customParameters, null, null, null, | ||
|
@@ -1954,13 +1960,27 @@ public void updateVmDetails(Map<String, String> deployParams, Map<String, String | |
} | ||
} | ||
|
||
@Override | ||
public String getNextVmHostName(AutoScaleVmGroupVO asGroup) { | ||
protected Pair<String, String> getNextVmHostAndDisplayName(AutoScaleVmGroupVO asGroup, VirtualMachineTemplate template) { | ||
template.getGuestOSId(); | ||
GuestOSVO guestOSVO = guestOSDao.findById(template.getGuestOSId()); | ||
boolean isWindows = guestOSVO != null && guestOSVO.getName().toLowerCase().contains("windows"); | ||
String vmHostNameSuffix = "-" + asGroup.getNextVmSeq() + "-" + | ||
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()); | ||
return VM_HOSTNAME_PREFIX + asGroup.getName().substring(0, subStringLength) + vmHostNameSuffix; | ||
String name = VM_HOSTNAME_PREFIX + asGroup.getName().substring(0, subStringLength) + vmHostNameSuffix; | ||
if (!isWindows) { | ||
return new Pair<>(name, name); | ||
} | ||
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]", ""); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DaanHoogland I don't think this case can happen. |
||
if (!temp.isEmpty()) { | ||
return new Pair<>(temp.charAt(temp.length() - 1) + hostName.substring(1), name); | ||
} | ||
return new Pair<>('a' + hostName.substring(1), name); | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems digit it not supported as first char ? @shwstppr
cloudstack/server/src/main/java/com/cloud/vm/UserVmManagerImpl.java
Lines 4104 to 4109 in 1272b13