-
Notifications
You must be signed in to change notification settings - Fork 13
Allow dynamic quota creation and removal #287
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
Draft
QuanMPhm
wants to merge
1
commit into
nerc-project:main
Choose a base branch
from
QuanMPhm:ops_1391/final
base: main
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.
+658
−402
Draft
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
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
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
118 changes: 118 additions & 0 deletions
118
src/coldfront_plugin_cloud/management/commands/add_quota_to_resource.py
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 |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import json | ||
| import logging | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
| from coldfront.core.resource.models import ( | ||
| Resource, | ||
| ResourceAttribute, | ||
| ResourceAttributeType, | ||
| ) | ||
| from coldfront.core.allocation.models import AllocationAttributeType, AttributeType | ||
|
|
||
| from coldfront_plugin_cloud import attributes | ||
| from coldfront_plugin_cloud.models.quota_models import QuotaSpecs, QuotaSpec | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| "--display_name", | ||
| type=str, | ||
| required=True, | ||
| help="The display name for the quota attribute to add to the resource type.", | ||
| ) | ||
| parser.add_argument( | ||
| "--default-quota", | ||
| type=int, | ||
| required=True, | ||
| help="The default quota value for the storage attribute. In GB", | ||
| ) | ||
| parser.add_argument( | ||
| "--resource_name", | ||
| type=str, | ||
| required=True, | ||
| help="The name of the resource to add the storage attribute to.", | ||
| ) | ||
| parser.add_argument( | ||
| "--quota-label", | ||
| dest="quota_label", | ||
| type=str, | ||
| required=True, | ||
| help="Human-readable quota_label for this quota (must be unique).", | ||
| ) | ||
| parser.add_argument( | ||
| "--multiplier", | ||
| dest="multiplier", | ||
| type=int, | ||
| default=0, | ||
| help="Multiplier applied per SU quantity (int).", | ||
| ) | ||
| parser.add_argument( | ||
| "--static-quota", | ||
| dest="static_quota", | ||
| type=int, | ||
| default=0, | ||
| help="Static quota added to every SU quantity (int).", | ||
| ) | ||
| parser.add_argument( | ||
| "--unit-suffix", | ||
| dest="unit_suffix", | ||
| type=str, | ||
| default="", | ||
| help='Unit suffix to append to formatted quota values (e.g. "Gi").', | ||
| ) | ||
| parser.add_argument( | ||
| "--is-storage-type", | ||
| action="store_true", | ||
| help="Indicates if this quota is for a storage type for billing purposes", | ||
| ) | ||
| parser.add_argument( | ||
| "--invoice-name", | ||
| type=str, | ||
| default="", | ||
| help="Name of quota as it appears on invoice. Required if --is-storage-type is set.", | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| if options["is_storage_type"] and not options["invoice_name"]: | ||
| logger.error( | ||
| "--invoice-name must be provided when --is-storage-type is set." | ||
| ) | ||
|
|
||
| resource_name = options["resource_name"] | ||
| display_name = options["display_name"] | ||
| new_quota_spec = QuotaSpec(**options) | ||
| new_quota_dict = {display_name: new_quota_spec.model_dump()} | ||
| QuotaSpecs.model_validate(new_quota_dict) | ||
|
|
||
| resource = Resource.objects.get(name=resource_name) | ||
| available_quotas_attr, created = ResourceAttribute.objects.get_or_create( | ||
| resource=resource, | ||
| resource_attribute_type=ResourceAttributeType.objects.get( | ||
| name=attributes.RESOURCE_QUOTA_RESOURCES | ||
| ), | ||
| defaults={"value": json.dumps(new_quota_dict)}, | ||
| ) | ||
|
|
||
| # TODO (Quan): Dict update allows migration of existing quotas. This is fine? | ||
| if not created: | ||
| available_quotas_dict = json.loads(available_quotas_attr.value) | ||
| available_quotas_dict.update(new_quota_dict) | ||
| QuotaSpecs.model_validate(available_quotas_dict) # Validate uniqueness | ||
| available_quotas_attr.value = json.dumps(available_quotas_dict) | ||
| available_quotas_attr.save() | ||
|
|
||
| # Now create Allocation Attribute for this quota | ||
| AllocationAttributeType.objects.get_or_create( | ||
| name=display_name, | ||
| defaults={ | ||
| "attribute_type": AttributeType.objects.get(name="Int"), | ||
| "has_usage": False, | ||
| "is_private": False, | ||
| "is_changeable": True, | ||
| }, | ||
| ) | ||
|
|
||
| logger.info("Added quota '%s' to resource '%s'.", display_name, resource_name) | ||
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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import csv | ||
| import json | ||
| from decimal import Decimal, ROUND_HALF_UP | ||
| import dataclasses | ||
| from datetime import datetime, timedelta, timezone | ||
|
|
@@ -7,6 +8,7 @@ | |
|
|
||
| from coldfront_plugin_cloud import attributes | ||
| from coldfront_plugin_cloud import utils | ||
| from coldfront_plugin_cloud.models.quota_models import QuotaSpecs | ||
|
|
||
| import boto3 | ||
| from django.core.management.base import BaseCommand | ||
|
|
@@ -20,6 +22,10 @@ | |
|
|
||
| _RATES = None | ||
|
|
||
| QUOTA_LIMITS_EPHEMERAL_STORAGE_GB = "OpenShift Limit on Ephemeral Storage Quota (GiB)" | ||
| QUOTA_REQUESTS_NESE_STORAGE = "OpenShift Request on NESE Storage Quota (GiB)" | ||
| QUOTA_REQUESTS_IBM_STORAGE = "OpenShift Request on IBM Storage Quota (GiB)" | ||
|
|
||
|
|
||
| def get_rates(): | ||
| # nerc-rates doesn't work with Python 3.9, which is what ColdFront is currently | ||
|
|
@@ -210,6 +216,16 @@ def upload_to_s3(s3_endpoint, s3_bucket, file_location, invoice_month, end_time) | |
| def handle(self, *args, **options): | ||
| generated_at = datetime.now(tz=timezone.utc).isoformat(timespec="seconds") | ||
|
|
||
| def get_storage_quotaspecs(allocation: Allocation): | ||
| """Get storage-related quota attributes for an allocation.""" | ||
| quotaspecs_dict = json.loads( | ||
| allocation.resources.first().get_attribute( | ||
| attributes.RESOURCE_QUOTA_RESOURCES | ||
| ) | ||
| ) | ||
| quotaspecs = QuotaSpecs.model_validate(quotaspecs_dict) | ||
| return quotaspecs.storage_quotas | ||
|
|
||
| def get_outages_for_service(cluster_name: str): | ||
| """Get outages for a service from nerc-rates. | ||
|
|
||
|
|
@@ -316,12 +332,15 @@ def process_invoice_row(allocation, attrs, su_name, rate): | |
| ) | ||
| logger.debug(f"Starting billing for allocation {allocation_str}.") | ||
|
|
||
| process_invoice_row( | ||
| allocation, | ||
| [attributes.QUOTA_VOLUMES_GB, attributes.QUOTA_OBJECT_GB], | ||
| "OpenStack Storage", | ||
| openstack_nese_storage_rate, | ||
| ) | ||
| # TODO (Quan): An illustration of how billing could be simplified. Shuold I follow with this? | ||
|
Contributor
Author
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. @knikolla I couldn't do the same refactoring for the Openshift allocations because different storages have their own rates. I could have refactored the code further to circumvent that issue, but I didn't want the PR to be too long. |
||
| quotaspecs = get_storage_quotaspecs(allocation) | ||
| for quota_name, quotaspec in quotaspecs.items(): | ||
| process_invoice_row( | ||
| allocation, | ||
| [quota_name], | ||
| quotaspec.invoice_name, | ||
| openstack_nese_storage_rate, | ||
| ) | ||
|
|
||
| for allocation in openshift_allocations: | ||
| allocation_str = ( | ||
|
|
||
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
69 changes: 69 additions & 0 deletions
69
src/coldfront_plugin_cloud/management/commands/remove_quota_from_resource.py
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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import json | ||
| import logging | ||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from coldfront.core.resource.models import ( | ||
| Resource, | ||
| ResourceAttribute, | ||
| ResourceAttributeType, | ||
| ) | ||
| from coldfront_plugin_cloud import attributes | ||
| from coldfront_plugin_cloud.models.quota_models import QuotaSpecs | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Remove a quota from a resource's available resource quotas. Use --apply to perform the change." | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| "resource_name", | ||
| type=str, | ||
| help="Name of the Resource to modify.", | ||
| ) | ||
| parser.add_argument( | ||
| "display_name", | ||
| type=str, | ||
| help="Display name of the quota to remove.", | ||
| ) | ||
| parser.add_argument( | ||
| "--apply", | ||
| action="store_true", | ||
| dest="apply", | ||
| help="If set, apply the removal", | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| resource_name = options["resource_name"] | ||
| display_name = options["display_name"] | ||
| apply_change = options["apply"] | ||
|
|
||
| resource = Resource.objects.get(name=resource_name) | ||
| rat = ResourceAttributeType.objects.get( | ||
| name=attributes.RESOURCE_QUOTA_RESOURCES | ||
| ) | ||
| available_attr = ResourceAttribute.objects.get( | ||
| resource=resource, resource_attribute_type=rat | ||
| ) | ||
|
|
||
| available_dict = json.loads(available_attr.value or "{}") | ||
|
|
||
| if display_name not in available_dict: | ||
| logger.info( | ||
| "Display name '%s' not present on resource '%s'. Nothing to remove.", | ||
| display_name, | ||
| resource_name, | ||
| ) | ||
| return | ||
|
|
||
| logger.info( | ||
| "Removing quota '%s' from resource '%s':", display_name, resource_name | ||
| ) | ||
| if not apply_change: | ||
| return | ||
|
|
||
| del available_dict[display_name] | ||
| QuotaSpecs.model_validate(available_dict) | ||
| available_attr.value = json.dumps(available_dict) | ||
| available_attr.save() |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
@knikolla @jtriley This is a pre-existing feature, so I assume the answer is yes. Just to make sure.