mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-02 08:56:07 +00:00
fix(instances): the quota counts every VM and refuses what it cannot verify
check_user_quota added up the vCPUs, memory and disks of the user's VMs, but skipped a VM whose host failed a TCP probe. A host that was down therefore let users exceed their quota, and the probe always failed for ssh computes named by an ssh_config alias or reached through ProxyJump, so their VMs were never counted. A VM that could not be read turned the page into the error page instead. - Hosts are checked with Compute.status, the connection WebVirtCloud uses, once per host. A VM that cannot be read (host down, VM gone) makes the usage unknown: clone and resize up are refused with their own message (fail closed). - A request that adds nothing is not checked, so a user over quota can still shrink a VM. Users without limits, and an exceeded instance count (from the database), need no host either. - Memory resize of a running VM changes only the current memory, but the quota was charged with the submitted maximum. It is now charged with no change, and resize_mem gets the current maximum, so a VM that shuts down meanwhile cannot take an unchecked one. - The VM page no longer computes the quota on every load (it read every VM of the user on every host for a "quota reached" banner). The limit is reported when a change is refused. The test that expected a down host to pass the quota now expects it to be refused. Checked against the test hosts: with a VM behind an ssh_config alias the previous code counted 29 vCPUs, the new one 31; with a VM on an unreachable host the previous code allowed +1 vCPU, the new one refuses it after 5.1 s and allows -1 at once. The new tests fail on the previous code, except the ones guarding unchanged behaviour.
This commit is contained in:
parent
a3d3cca477
commit
fb3f686532
5 changed files with 245 additions and 35 deletions
|
|
@ -34,6 +34,7 @@ from libvirt import (VIR_DOMAIN_UNDEFINE_KEEP_NVRAM,
|
|||
VIR_DOMAIN_UNDEFINE_SNAPSHOTS_METADATA,
|
||||
VIR_DOMAIN_UNDEFINE_NVRAM,
|
||||
VIR_DOMAIN_START_PAUSED,
|
||||
VIR_DOMAIN_RUNNING,
|
||||
libvirtError)
|
||||
from logs.views import addlogmsg
|
||||
from vrtManager import util
|
||||
|
|
@ -118,7 +119,6 @@ def instance(request, pk):
|
|||
if instance.cur_memory not in memory_range:
|
||||
insort(memory_range, instance.cur_memory)
|
||||
clone_free_names = utils.get_clone_free_names()
|
||||
user_quota_msg = utils.check_user_quota(request.user, 0, 0, 0, 0)
|
||||
|
||||
default_bus = app_settings.INSTANCE_VOLUME_DEFAULT_BUS
|
||||
default_io = app_settings.INSTANCE_VOLUME_DEFAULT_IO
|
||||
|
|
@ -403,6 +403,14 @@ def serialize_instance_mutation(func):
|
|||
return wrapper
|
||||
|
||||
|
||||
def quota_refused(quota_msg, refused):
|
||||
"""The message for a refused change: refused is the action's own
|
||||
'User %(quota_msg)s quota reached, ...' text, already formatted."""
|
||||
if quota_msg == utils.QUOTA_UNVERIFIED:
|
||||
return _("The quota cannot be checked because a host of your virtual machines cannot be reached. Nothing was changed.")
|
||||
return refused
|
||||
|
||||
|
||||
def serialize_user_quota(func):
|
||||
"""Outermost lock for quota-checked changes: concurrent requests of one
|
||||
user (also on different computes) must not pass the check together."""
|
||||
|
|
@ -766,12 +774,12 @@ def resizevm_cpu(request, pk):
|
|||
request.user, 0, int(new_vcpu) - vcpu, 0, 0
|
||||
)
|
||||
if not request.user.is_superuser and quota_msg:
|
||||
msg = _(
|
||||
msg = quota_refused(quota_msg, _(
|
||||
"User %(quota_msg)s quota reached, cannot resize CPU of '%(instance_name)s'!"
|
||||
) % {
|
||||
"quota_msg": quota_msg,
|
||||
"instance_name": instance.name,
|
||||
}
|
||||
})
|
||||
messages.error(request, msg)
|
||||
else:
|
||||
cur_vcpu = new_cur_vcpu
|
||||
|
|
@ -804,16 +812,19 @@ def resize_memory(request, pk):
|
|||
new_cur_memory_custom = request.POST.get("cur_memory_custom", "")
|
||||
if new_cur_memory_custom:
|
||||
new_cur_memory = new_cur_memory_custom
|
||||
quota_msg = utils.check_user_quota(
|
||||
request.user, 0, 0, int(new_memory) - memory, 0
|
||||
)
|
||||
# The quota charges the maximum. A running VM keeps it (resize_mem changes
|
||||
# only the current memory there); pass the current maximum, so a VM that
|
||||
# shuts down meanwhile does not get an unchecked one.
|
||||
if instance.proxy.get_status() == VIR_DOMAIN_RUNNING:
|
||||
new_memory = memory
|
||||
quota_msg = utils.check_user_quota(request.user, 0, 0, int(new_memory) - memory, 0)
|
||||
if not request.user.is_superuser and quota_msg:
|
||||
msg = _(
|
||||
msg = quota_refused(quota_msg, _(
|
||||
"User %(quota_msg)s quota reached, cannot resize memory of '%(instance_name)s'!"
|
||||
) % {
|
||||
"quota_msg": quota_msg,
|
||||
"instance_name": instance.name,
|
||||
}
|
||||
})
|
||||
messages.error(request, msg)
|
||||
else:
|
||||
instance.proxy.resize_mem(new_cur_memory, new_memory)
|
||||
|
|
@ -859,12 +870,12 @@ def resize_disk(request, pk):
|
|||
request.user, 0, 0, 0, disk_new_sum - disk_sum
|
||||
)
|
||||
if not request.user.is_superuser and quota_msg:
|
||||
msg = _(
|
||||
msg = quota_refused(quota_msg, _(
|
||||
"User %(quota_msg)s quota reached, cannot resize disks of '%(instance_name)s'!"
|
||||
) % {
|
||||
"quota_msg": quota_msg,
|
||||
"instance_name": instance.name,
|
||||
}
|
||||
})
|
||||
messages.error(request, msg)
|
||||
else:
|
||||
instance.proxy.resize_disk(disks_new)
|
||||
|
|
@ -1789,12 +1800,12 @@ def clone(request, pk):
|
|||
]
|
||||
|
||||
if not request.user.is_superuser and quota_msg:
|
||||
msg = _(
|
||||
msg = quota_refused(quota_msg, _(
|
||||
"User '%(quota_msg)s' quota reached, cannot create '%(clone_name)s'!"
|
||||
) % {
|
||||
"quota_msg": quota_msg,
|
||||
"clone_name": clone_data["name"],
|
||||
}
|
||||
})
|
||||
messages.error(request, msg)
|
||||
elif check_instance:
|
||||
msg = _("Instance '%(clone_name)s' already exists!") % {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue