-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Prevent scaling of cluster if count / resources exceed account resource limits #12167
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
Open
Pearl1594
wants to merge
1
commit into
4.22
Choose a base branch
from
fix-cks-scaling-resource-limit
base: 4.22
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -47,6 +47,8 @@ | |||
| import javax.inject.Inject; | ||||
| import javax.naming.ConfigurationException; | ||||
|
|
||||
| import com.cloud.configuration.Resource; | ||||
| import com.cloud.user.ResourceLimitService; | ||||
| import org.apache.cloudstack.acl.ControlledEntity; | ||||
| import org.apache.cloudstack.acl.Role; | ||||
| import org.apache.cloudstack.acl.RolePermissionEntity; | ||||
|
|
@@ -398,6 +400,8 @@ | |||
| public ProjectManager projectManager; | ||||
| @Inject | ||||
| RoleService roleService; | ||||
| @Inject | ||||
|
Check warning on line 403 in plugins/integrations/kubernetes-service/src/main/java/com/cloud/kubernetes/cluster/KubernetesClusterManagerImpl.java
|
||||
| ResourceLimitService resourceLimitService; | ||||
|
|
||||
| private void logMessage(final Level logLevel, final String message, final Exception e) { | ||||
| if (logLevel == Level.WARN) { | ||||
|
|
@@ -1350,8 +1354,58 @@ | |||
| validateServiceOfferingsForNodeTypesScale(serviceOfferingNodeTypeMap, defaultServiceOfferingId, kubernetesCluster, clusterVersion); | ||||
|
|
||||
| validateKubernetesClusterScaleSize(kubernetesCluster, clusterSize, maxClusterSize, zone); | ||||
|
|
||||
| ensureResourceLimitsForScale(kubernetesCluster, serviceOfferingNodeTypeMap, | ||||
| clusterSize != null ? clusterSize : null, | ||||
| kubernetesCluster.getAccountId()); | ||||
| } | ||||
|
|
||||
| protected void ensureResourceLimitsForScale(final KubernetesClusterVO cluster, | ||||
| final Map<String, Long> requestedServiceOfferingIds, | ||||
| final Long targetNodeCounts, | ||||
| final Long accountId) { | ||||
|
|
||||
| long totalAdditionalVms = 0L; | ||||
| long totalAdditionalCpuUnits = 0L; | ||||
| long totalAdditionalRamMb = 0L; | ||||
|
|
||||
|
|
||||
| List<KubernetesClusterVmMapVO> clusterVmMapVOS = kubernetesClusterVmMapDao.listByClusterIdAndVmType(cluster.getId(), WORKER); | ||||
| long currentCount = clusterVmMapVOS != null ? clusterVmMapVOS.size() : 0L; | ||||
| long desiredCount = targetNodeCounts != null ? targetNodeCounts : currentCount; | ||||
| long additional = Math.max(0L, desiredCount - currentCount); | ||||
| if (additional == 0L) { | ||||
| return; | ||||
| } | ||||
|
|
||||
| Long offeringId = (requestedServiceOfferingIds != null && requestedServiceOfferingIds.containsKey(WORKER.name())) ? | ||||
| requestedServiceOfferingIds.get(WORKER.name()) : | ||||
| getExistingServiceOfferingIdForNodeType(WORKER.name(), cluster); | ||||
|
|
||||
| if (offeringId == null) { | ||||
| offeringId = cluster.getServiceOfferingId(); | ||||
| } | ||||
|
|
||||
| ServiceOffering so = serviceOfferingDao.findById(offeringId); | ||||
| if (so == null) { | ||||
| throw new InvalidParameterValueException(String.format("Invalid service offering for node type %s", WORKER.name())); | ||||
| } | ||||
|
|
||||
| totalAdditionalVms += additional; | ||||
| long effectiveCpu = (long) so.getCpu() * so.getSpeed(); | ||||
| totalAdditionalCpuUnits += effectiveCpu * additional; | ||||
| totalAdditionalRamMb += so.getRamSize() * additional; | ||||
|
|
||||
| try { | ||||
| resourceLimitService.checkResourceLimit(accountDao.findById(accountId), Resource.ResourceType.user_vm, totalAdditionalVms); | ||||
| resourceLimitService.checkResourceLimit(accountDao.findById(accountId), Resource.ResourceType.cpu, totalAdditionalCpuUnits); | ||||
| resourceLimitService.checkResourceLimit(accountDao.findById(accountId), Resource.ResourceType.memory, totalAdditionalRamMb); | ||||
| } catch (Exception e) { | ||||
| throw new CloudRuntimeException("Resource limits prevent scaling the cluster: " + e.getMessage(), e); | ||||
| } | ||||
| } | ||||
|
|
||||
|
|
||||
|
Contributor
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.
Suggested change
|
||||
| protected void validateServiceOfferingsForNodeTypesScale(Map<String, Long> map, Long defaultServiceOfferingId, KubernetesClusterVO kubernetesCluster, KubernetesSupportedVersion clusterVersion) { | ||||
| for (String key : CLUSTER_NODES_TYPES_LIST) { | ||||
| Long serviceOfferingId = map.getOrDefault(key, defaultServiceOfferingId); | ||||
|
|
||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
the sonar warning here is new to me. If we need to deal with this that is going to be a huge effort as this pattern is all over the project.