Skip to content

Commit 0688240

Browse files
committed
Prevent scaling of cluster if count / resources exceed account resource limits
1 parent e23c7ef commit 0688240

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import javax.inject.Inject;
4848
import javax.naming.ConfigurationException;
4949

50+
import com.cloud.configuration.Resource;
51+
import com.cloud.user.ResourceLimitService;
5052
import org.apache.cloudstack.acl.ControlledEntity;
5153
import org.apache.cloudstack.acl.Role;
5254
import org.apache.cloudstack.acl.RolePermissionEntity;
@@ -398,6 +400,8 @@ public class KubernetesClusterManagerImpl extends ManagerBase implements Kuberne
398400
public ProjectManager projectManager;
399401
@Inject
400402
RoleService roleService;
403+
@Inject
404+
ResourceLimitService resourceLimitService;
401405

402406
private void logMessage(final Level logLevel, final String message, final Exception e) {
403407
if (logLevel == Level.WARN) {
@@ -1350,8 +1354,58 @@ private void validateKubernetesClusterScaleParameters(ScaleKubernetesClusterCmd
13501354
validateServiceOfferingsForNodeTypesScale(serviceOfferingNodeTypeMap, defaultServiceOfferingId, kubernetesCluster, clusterVersion);
13511355

13521356
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+
}
13531406
}
13541407

1408+
13551409
protected void validateServiceOfferingsForNodeTypesScale(Map<String, Long> map, Long defaultServiceOfferingId, KubernetesClusterVO kubernetesCluster, KubernetesSupportedVersion clusterVersion) {
13561410
for (String key : CLUSTER_NODES_TYPES_LIST) {
13571411
Long serviceOfferingId = map.getOrDefault(key, defaultServiceOfferingId);

0 commit comments

Comments
 (0)