Skip to content

Commit 8ac8a22

Browse files
Merge branch '4.20'
2 parents 5ea1ada + ed0d606 commit 8ac8a22

File tree

9 files changed

+79
-13
lines changed

9 files changed

+79
-13
lines changed

engine/schema/src/main/java/com/cloud/storage/dao/VMTemplateDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public interface VMTemplateDao extends GenericDao<VMTemplateVO, Long>, StateDao<
7474

7575
VMTemplateVO findSystemVMTemplate(long zoneId);
7676

77-
VMTemplateVO findSystemVMReadyTemplate(long zoneId, HypervisorType hypervisorType);
77+
VMTemplateVO findSystemVMReadyTemplate(long zoneId, HypervisorType hypervisorType, String preferredArch);
7878

7979
List<VMTemplateVO> findSystemVMReadyTemplates(long zoneId, HypervisorType hypervisorType, String preferredArch);
8080

engine/schema/src/main/java/com/cloud/storage/dao/VMTemplateDaoImpl.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.HashMap;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.stream.Collectors;
2728

2829
import javax.inject.Inject;
2930
import javax.naming.ConfigurationException;
@@ -621,11 +622,19 @@ public List<VMTemplateVO> listAllReadySystemVMTemplates(Long zoneId) {
621622
}
622623

623624
@Override
624-
public VMTemplateVO findSystemVMReadyTemplate(long zoneId, HypervisorType hypervisorType) {
625+
public VMTemplateVO findSystemVMReadyTemplate(long zoneId, HypervisorType hypervisorType, String preferredArch) {
625626
List<VMTemplateVO> templates = listAllReadySystemVMTemplates(zoneId);
626627
if (CollectionUtils.isEmpty(templates)) {
627628
return null;
628629
}
630+
if (StringUtils.isNotBlank(preferredArch)) {
631+
// Sort the templates by preferred architecture first
632+
templates = templates.stream()
633+
.sorted(Comparator.comparing(
634+
x -> !x.getArch().getType().equalsIgnoreCase(preferredArch)
635+
))
636+
.collect(Collectors.toList());
637+
}
629638
if (hypervisorType == HypervisorType.Any) {
630639
return templates.get(0);
631640
}

engine/schema/src/test/java/com/cloud/storage/dao/VMTemplateDaoImplTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.List;
3737
import java.util.Map;
3838

39+
import org.junit.Assert;
3940
import org.junit.Test;
4041
import org.junit.runner.RunWith;
4142
import org.mockito.InjectMocks;
@@ -316,4 +317,24 @@ public void testListIdsByExtensionId_ReturnsIds() {
316317
verify(searchCriteria).setParameters("extensionId", extensionId);
317318
verify(templateDao).customSearchIncludingRemoved(eq(searchCriteria), isNull());
318319
}
320+
321+
@Test
322+
public void testFindSystemVMReadyTemplate() {
323+
Long zoneId = 1L;
324+
VMTemplateVO systemVmTemplate1 = mock(VMTemplateVO.class);
325+
Mockito.when(systemVmTemplate1.getArch()).thenReturn(CPU.CPUArch.x86);
326+
VMTemplateVO systemVmTemplate2 = mock(VMTemplateVO.class);
327+
Mockito.when(systemVmTemplate2.getArch()).thenReturn(CPU.CPUArch.x86);
328+
VMTemplateVO systemVmTemplate3 = mock(VMTemplateVO.class);
329+
Mockito.when(systemVmTemplate3.getArch()).thenReturn(CPU.CPUArch.arm64);
330+
Mockito.when(systemVmTemplate3.getHypervisorType()).thenReturn(Hypervisor.HypervisorType.KVM);
331+
List<VMTemplateVO> templates = Arrays.asList(systemVmTemplate1, systemVmTemplate2, systemVmTemplate3);
332+
Mockito.when(hostDao.listDistinctHypervisorTypes(zoneId)).thenReturn(Arrays.asList(Hypervisor.HypervisorType.KVM));
333+
SearchBuilder<VMTemplateVO> sb = mock(SearchBuilder.class);
334+
templateDao.readySystemTemplateSearch = sb;
335+
when(sb.create()).thenReturn(mock(SearchCriteria.class));
336+
doReturn(templates).when(templateDao).listBy(any(SearchCriteria.class), any(Filter.class));
337+
VMTemplateVO readyTemplate = templateDao.findSystemVMReadyTemplate(zoneId, Hypervisor.HypervisorType.KVM, CPU.CPUArch.arm64.getType());
338+
Assert.assertEquals(CPU.CPUArch.arm64, readyTemplate.getArch());
339+
}
319340
}

plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/KubernetesClusterManagerImpl.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,14 @@ private IpAddress getSourceNatIp(Network network) {
482482
return null;
483483
}
484484

485-
public VMTemplateVO getKubernetesServiceTemplate(DataCenter dataCenter, Hypervisor.HypervisorType hypervisorType, Map<String, Long> templateNodeTypeMap, KubernetesClusterNodeType nodeType) {
486-
VMTemplateVO template = templateDao.findSystemVMReadyTemplate(dataCenter.getId(), hypervisorType);
485+
public VMTemplateVO getKubernetesServiceTemplate(DataCenter dataCenter, Hypervisor.HypervisorType hypervisorType, Map<String, Long> templateNodeTypeMap, KubernetesClusterNodeType nodeType,
486+
KubernetesSupportedVersion clusterKubernetesVersion) {
487+
String systemVMPreferredArchitecture = ResourceManager.SystemVmPreferredArchitecture.valueIn(dataCenter.getId());
488+
VMTemplateVO cksIso = clusterKubernetesVersion != null ?
489+
templateDao.findById(clusterKubernetesVersion.getIsoId()) :
490+
null;
491+
String preferredArchitecture = getCksClusterPreferredArch(systemVMPreferredArchitecture, cksIso);
492+
VMTemplateVO template = templateDao.findSystemVMReadyTemplate(dataCenter.getId(), hypervisorType, preferredArchitecture);
487493
if (DataCenter.Type.Edge.equals(dataCenter.getType()) && template != null && !template.isDirectDownload()) {
488494
logger.debug(String.format("Template %s can not be used for edge zone %s", template, dataCenter));
489495
template = templateDao.findRoutingTemplate(hypervisorType, networkHelper.getHypervisorRouterTemplateConfigMap().get(hypervisorType).valueIn(dataCenter.getId()));
@@ -510,6 +516,14 @@ public void throwDefaultCksTemplateNotFound(String datacenterId) {
510516
throw new CloudRuntimeException("Not able to find the System or Routing template in ready state for the zone " + datacenterId);
511517
}
512518

519+
protected String getCksClusterPreferredArch(String systemVMPreferredArchitecture, VMTemplateVO cksIso) {
520+
if (cksIso == null) {
521+
return systemVMPreferredArchitecture;
522+
}
523+
String cksIsoArchName = cksIso.getArch().name();
524+
return cksIsoArchName.equals(systemVMPreferredArchitecture) ? systemVMPreferredArchitecture : cksIsoArchName;
525+
}
526+
513527
protected void validateIsolatedNetworkIpRules(long ipId, FirewallRule.Purpose purpose, Network network, int clusterTotalNodeCount) {
514528
List<FirewallRuleVO> rules = firewallRulesDao.listByIpPurposeProtocolAndNotRevoked(ipId, purpose, NetUtils.TCP_PROTO);
515529
for (FirewallRuleVO rule : rules) {
@@ -1541,11 +1555,12 @@ public KubernetesCluster createManagedKubernetesCluster(CreateKubernetesClusterC
15411555
}
15421556

15431557
Map<String, Long> templateNodeTypeMap = cmd.getTemplateNodeTypeMap();
1544-
final VMTemplateVO finalTemplate = getKubernetesServiceTemplate(zone, hypervisorType, templateNodeTypeMap, DEFAULT);
1545-
final VMTemplateVO controlNodeTemplate = getKubernetesServiceTemplate(zone, hypervisorType, templateNodeTypeMap, CONTROL);
1546-
final VMTemplateVO workerNodeTemplate = getKubernetesServiceTemplate(zone, hypervisorType, templateNodeTypeMap, WORKER);
1547-
final VMTemplateVO etcdNodeTemplate = getKubernetesServiceTemplate(zone, hypervisorType, templateNodeTypeMap, ETCD);
1558+
final VMTemplateVO finalTemplate = getKubernetesServiceTemplate(zone, hypervisorType, templateNodeTypeMap, DEFAULT, clusterKubernetesVersion);
1559+
final VMTemplateVO controlNodeTemplate = getKubernetesServiceTemplate(zone, hypervisorType, templateNodeTypeMap, CONTROL, clusterKubernetesVersion);
1560+
final VMTemplateVO workerNodeTemplate = getKubernetesServiceTemplate(zone, hypervisorType, templateNodeTypeMap, WORKER, clusterKubernetesVersion);
1561+
final VMTemplateVO etcdNodeTemplate = getKubernetesServiceTemplate(zone, hypervisorType, templateNodeTypeMap, ETCD, clusterKubernetesVersion);
15481562
final Network defaultNetwork = getKubernetesClusterNetworkIfMissing(cmd.getName(), zone, owner, (int)controlNodeCount, (int)clusterSize, cmd.getExternalLoadBalancerIpAddress(), cmd.getNetworkId(), asNumber);
1563+
15491564
final SecurityGroup finalSecurityGroup = securityGroup;
15501565
final KubernetesClusterVO cluster = Transaction.execute(new TransactionCallback<KubernetesClusterVO>() {
15511566
@Override

plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/actionworkers/KubernetesClusterActionWorker.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
import javax.inject.Inject;
3838

39+
import com.cloud.kubernetes.version.KubernetesSupportedVersionVO;
3940
import org.apache.logging.log4j.Level;
4041
import org.apache.logging.log4j.Logger;
4142
import org.apache.logging.log4j.LogManager;
@@ -257,7 +258,8 @@ protected void init() {
257258
DataCenterVO dataCenterVO = dataCenterDao.findById(zoneId);
258259
VMTemplateVO template = templateDao.findById(templateId);
259260
Hypervisor.HypervisorType type = template.getHypervisorType();
260-
this.clusterTemplate = manager.getKubernetesServiceTemplate(dataCenterVO, type, null, KubernetesClusterNodeType.DEFAULT);
261+
KubernetesSupportedVersionVO kubernetesSupportedVersion = kubernetesSupportedVersionDao.findById(this.kubernetesCluster.getKubernetesVersionId());
262+
this.clusterTemplate = manager.getKubernetesServiceTemplate(dataCenterVO, type, null, KubernetesClusterNodeType.DEFAULT, kubernetesSupportedVersion);
261263
this.controlNodeTemplate = templateDao.findById(this.kubernetesCluster.getControlNodeTemplateId());
262264
this.workerNodeTemplate = templateDao.findById(this.kubernetesCluster.getWorkerNodeTemplateId());
263265
this.etcdTemplate = templateDao.findById(this.kubernetesCluster.getEtcdNodeTemplateId());

plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/actionworkers/KubernetesClusterResourceModifierActionWorker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ protected DeployDestination plan(final long nodesCount, final DataCenter zone, f
212212
for (Map.Entry<String, Pair<HostVO, Integer>> hostEntry : hosts_with_resevered_capacity.entrySet()) {
213213
Pair<HostVO, Integer> hp = hostEntry.getValue();
214214
HostVO h = hp.first();
215-
if (!h.getHypervisorType().equals(clusterTemplate.getHypervisorType())) {
215+
if (!h.getHypervisorType().equals(clusterTemplate.getHypervisorType()) || !h.getArch().equals(clusterTemplate.getArch())) {
216216
continue;
217217
}
218218
hostDao.loadHostTags(h);

plugins/integrations/kubernetes-service/src/test/java/com/cloud/kubernetes/cluster/KubernetesClusterManagerImplTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.cloud.api.query.dao.TemplateJoinDao;
2323
import com.cloud.api.query.vo.TemplateJoinVO;
24+
import com.cloud.cpu.CPU;
2425
import com.cloud.dc.DataCenter;
2526
import com.cloud.exception.InvalidParameterValueException;
2627
import com.cloud.exception.PermissionDeniedException;
@@ -422,4 +423,22 @@ public void testCreateNodeTypeToServiceOfferingMap() {
422423
Assert.assertEquals(workerOffering, mapping.get(WORKER.name()));
423424
Assert.assertEquals(controlOffering, mapping.get(CONTROL.name()));
424425
}
426+
427+
@Test
428+
public void testGetCksClusterPreferredArchDifferentArchsPreferCKSIsoArch() {
429+
String systemVMArch = "x86_64";
430+
VMTemplateVO cksIso = Mockito.mock(VMTemplateVO.class);
431+
Mockito.when(cksIso.getArch()).thenReturn(CPU.CPUArch.arm64);
432+
String cksClusterPreferredArch = kubernetesClusterManager.getCksClusterPreferredArch(systemVMArch, cksIso);
433+
Assert.assertEquals(CPU.CPUArch.arm64.name(), cksClusterPreferredArch);
434+
}
435+
436+
@Test
437+
public void testGetCksClusterPreferredArchSameArch() {
438+
String systemVMArch = "x86_64";
439+
VMTemplateVO cksIso = Mockito.mock(VMTemplateVO.class);
440+
Mockito.when(cksIso.getArch()).thenReturn(CPU.CPUArch.amd64);
441+
String cksClusterPreferredArch = kubernetesClusterManager.getCksClusterPreferredArch(systemVMArch, cksIso);
442+
Assert.assertEquals(CPU.CPUArch.amd64.name(), cksClusterPreferredArch);
443+
}
425444
}

plugins/storage/sharedfs/storagevm/src/main/java/org/apache/cloudstack/storage/sharedfs/lifecycle/StorageVmSharedFSLifeCycle.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,11 @@ private UserVm deploySharedFSVM(Long zoneId, Account owner, List<Long> networkId
175175
customParameterMap.put("maxIopsDo", maxIops.toString());
176176
}
177177
List<String> keypairs = new ArrayList<String>();
178+
String preferredArchitecture = ResourceManager.SystemVmPreferredArchitecture.valueIn(zoneId);
178179

179180
for (final Iterator<Hypervisor.HypervisorType> iter = hypervisors.iterator(); iter.hasNext();) {
180181
final Hypervisor.HypervisorType hypervisor = iter.next();
181-
VMTemplateVO template = templateDao.findSystemVMReadyTemplate(zoneId, hypervisor);
182+
VMTemplateVO template = templateDao.findSystemVMReadyTemplate(zoneId, hypervisor, preferredArchitecture);
182183
if (template == null && !iter.hasNext()) {
183184
throw new CloudRuntimeException(String.format("Unable to find the systemvm template for %s or it was not downloaded in %s.", hypervisor.toString(), zone.toString()));
184185
}

plugins/storage/sharedfs/storagevm/src/test/java/org/apache/cloudstack/storage/sharedfs/lifecycle/StorageVmSharedFSLifeCycleTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ private SharedFS prepareDeploySharedFS() throws ResourceUnavailableException, In
235235
when(serviceOfferingDao.findById(s_serviceOfferingId)).thenReturn(serviceOffering);
236236

237237
VMTemplateVO template = mock(VMTemplateVO.class);
238-
when(templateDao.findSystemVMReadyTemplate(s_zoneId, Hypervisor.HypervisorType.KVM)).thenReturn(template);
238+
when(templateDao.findSystemVMReadyTemplate(s_zoneId, Hypervisor.HypervisorType.KVM, ResourceManager.SystemVmPreferredArchitecture.defaultValue())).thenReturn(template);
239239
when(template.getId()).thenReturn(s_templateId);
240240

241241
return sharedFS;
@@ -302,7 +302,6 @@ public void testDeploySharedFSTemplateNotFound() throws ResourceUnavailableExcep
302302
when(dataCenterDao.findById(s_zoneId)).thenReturn(zone);
303303
when(resourceMgr.getSupportedHypervisorTypes(s_zoneId, false, null)).thenReturn(List.of(Hypervisor.HypervisorType.KVM));
304304

305-
when(templateDao.findSystemVMReadyTemplate(s_zoneId, Hypervisor.HypervisorType.KVM)).thenReturn(null);
306305
lifeCycle.deploySharedFS(sharedFS, s_networkId, s_diskOfferingId, s_size, s_minIops, s_maxIops);
307306
}
308307

0 commit comments

Comments
 (0)