|
47 | 47 | import javax.inject.Inject; |
48 | 48 | import javax.naming.ConfigurationException; |
49 | 49 |
|
| 50 | +import com.cloud.configuration.Resource; |
| 51 | +import com.cloud.user.ResourceLimitService; |
50 | 52 | import org.apache.cloudstack.acl.ControlledEntity; |
51 | 53 | import org.apache.cloudstack.acl.Role; |
52 | 54 | import org.apache.cloudstack.acl.RolePermissionEntity; |
@@ -398,6 +400,8 @@ public class KubernetesClusterManagerImpl extends ManagerBase implements Kuberne |
398 | 400 | public ProjectManager projectManager; |
399 | 401 | @Inject |
400 | 402 | RoleService roleService; |
| 403 | + @Inject |
| 404 | + ResourceLimitService resourceLimitService; |
401 | 405 |
|
402 | 406 | private void logMessage(final Level logLevel, final String message, final Exception e) { |
403 | 407 | if (logLevel == Level.WARN) { |
@@ -1350,8 +1354,58 @@ private void validateKubernetesClusterScaleParameters(ScaleKubernetesClusterCmd |
1350 | 1354 | validateServiceOfferingsForNodeTypesScale(serviceOfferingNodeTypeMap, defaultServiceOfferingId, kubernetesCluster, clusterVersion); |
1351 | 1355 |
|
1352 | 1356 | validateKubernetesClusterScaleSize(kubernetesCluster, clusterSize, maxClusterSize, zone); |
| 1357 | + |
| 1358 | + ensureResourceLimitsForScale(kubernetesCluster, serviceOfferingNodeTypeMap, |
| 1359 | + clusterSize != null ? clusterSize : null, |
| 1360 | + kubernetesCluster.getAccountId()); |
| 1361 | + } |
| 1362 | + |
| 1363 | + protected void ensureResourceLimitsForScale(final KubernetesClusterVO cluster, |
| 1364 | + final Map<String, Long> requestedServiceOfferingIds, |
| 1365 | + final Long targetNodeCounts, |
| 1366 | + final Long accountId) { |
| 1367 | + |
| 1368 | + long totalAdditionalVms = 0L; |
| 1369 | + long totalAdditionalCpuUnits = 0L; |
| 1370 | + long totalAdditionalRamMb = 0L; |
| 1371 | + |
| 1372 | + |
| 1373 | + List<KubernetesClusterVmMapVO> clusterVmMapVOS = kubernetesClusterVmMapDao.listByClusterIdAndVmType(cluster.getId(), WORKER); |
| 1374 | + long currentCount = clusterVmMapVOS != null ? clusterVmMapVOS.size() : 0L; |
| 1375 | + long desiredCount = targetNodeCounts != null ? targetNodeCounts : currentCount; |
| 1376 | + long additional = Math.max(0L, desiredCount - currentCount); |
| 1377 | + if (additional == 0L) { |
| 1378 | + return; |
| 1379 | + } |
| 1380 | + |
| 1381 | + Long offeringId = (requestedServiceOfferingIds != null && requestedServiceOfferingIds.containsKey(WORKER.name())) ? |
| 1382 | + requestedServiceOfferingIds.get(WORKER.name()) : |
| 1383 | + getExistingServiceOfferingIdForNodeType(WORKER.name(), cluster); |
| 1384 | + |
| 1385 | + if (offeringId == null) { |
| 1386 | + offeringId = cluster.getServiceOfferingId(); |
| 1387 | + } |
| 1388 | + |
| 1389 | + ServiceOffering so = serviceOfferingDao.findById(offeringId); |
| 1390 | + if (so == null) { |
| 1391 | + throw new InvalidParameterValueException(String.format("Invalid service offering for node type %s", WORKER.name())); |
| 1392 | + } |
| 1393 | + |
| 1394 | + totalAdditionalVms += additional; |
| 1395 | + long effectiveCpu = (long) so.getCpu() * so.getSpeed(); |
| 1396 | + totalAdditionalCpuUnits += effectiveCpu * additional; |
| 1397 | + totalAdditionalRamMb += so.getRamSize() * additional; |
| 1398 | + |
| 1399 | + try { |
| 1400 | + resourceLimitService.checkResourceLimit(accountDao.findById(accountId), Resource.ResourceType.user_vm, totalAdditionalVms); |
| 1401 | + resourceLimitService.checkResourceLimit(accountDao.findById(accountId), Resource.ResourceType.cpu, totalAdditionalCpuUnits); |
| 1402 | + resourceLimitService.checkResourceLimit(accountDao.findById(accountId), Resource.ResourceType.memory, totalAdditionalRamMb); |
| 1403 | + } catch (Exception e) { |
| 1404 | + throw new CloudRuntimeException("Resource limits prevent scaling the cluster: " + e.getMessage(), e); |
| 1405 | + } |
1353 | 1406 | } |
1354 | 1407 |
|
| 1408 | + |
1355 | 1409 | protected void validateServiceOfferingsForNodeTypesScale(Map<String, Long> map, Long defaultServiceOfferingId, KubernetesClusterVO kubernetesCluster, KubernetesSupportedVersion clusterVersion) { |
1356 | 1410 | for (String key : CLUSTER_NODES_TYPES_LIST) { |
1357 | 1411 | Long serviceOfferingId = map.getOrDefault(key, defaultServiceOfferingId); |
|
0 commit comments