Add resource limits extensions for CPU, memory, and shared memory#331
Add resource limits extensions for CPU, memory, and shared memory#331
Conversation
- Enhanced ShmSize extension with build-time shm-size support via --shm-size-build - Added CpuLimits extension providing --cpus argument for runtime CPU limits - Added MemoryLimits extension providing --memory/-m argument for runtime memory limits - Extended core infrastructure to support docker build arguments from extensions - Added comprehensive input validation for memory and CPU formats - Updated documentation and added test coverage for all new functionality - Improved .gitignore with comprehensive Python and development file patterns These extensions expose docker build and runtime resource limit arguments (shm-size, cpus, memory) to rocker users, providing better control over container resource allocation.
tfoote
left a comment
There was a problem hiding this comment.
Thanks adding these has been on my wishlist for a while, aka #311 and #239
I have a few requests that I've put in line.
At a high level I think it might make sense to separate out the resource constrant based optoins into a separate constraint_extensions.py file since the extensions.py file is getting quite long. And the resource constraints is a well defined but still decently sized set of possibilities: https://docs.docker.com/engine/containers/resource_constraints/
If you can update this PR with those adjustments I'll be happy to land this.
.gitignore
Outdated
| dist | ||
| deb_dist | ||
| *.pyc | ||
| # Byte-compiled / optimized / DLL files |
There was a problem hiding this comment.
Please revert your changes to the .gitignore files. These look like good options for a global git config ignore on your system. We don't want to put ignores for all potential frameworks and tools into every project.
There was a problem hiding this comment.
Please revert these changes to the .gitignore
src/rocker/extensions.py
Outdated
|
|
||
| @staticmethod | ||
| def _validate_memory_format(value): | ||
| """Validate memory format (e.g., '1g', '512m', '1024k').""" |
There was a problem hiding this comment.
This looks incomplete. From https://docs.docker.com/reference/compose-file/build/#shm_size It refers to a formal "byte-values" definition which allows more variations: https://docs.docker.com/reference/compose-file/extension/#specifying-byte-values
Values express a byte value as a string in {amount}{byte unit} format: The supported units are b (bytes), k or kb (kilo bytes), m or mb (mega bytes) and g or gb (giga bytes).
2b
1024kb
2048k
300m
1gb
It would be good to cover that spec as well as link to it for reference.
src/rocker/extensions.py
Outdated
| return args | ||
|
|
||
| @staticmethod | ||
| def _validate_memory_format(value): |
There was a problem hiding this comment.
The memory argument validation routine should be reused and not duplicated, it's just a helper function that can be validated and tested separately and then just reused in the two different extensions and potentially in more in the future.
src/rocker/extensions.py
Outdated
| return value | ||
| except ValueError: | ||
| from argparse import ArgumentTypeError | ||
| raise ArgumentTypeError(f"Invalid CPU format: {value}. Expected a number (e.g., '1.5')") |
There was a problem hiding this comment.
| raise ArgumentTypeError(f"Invalid CPU format: {value}. Expected a number (e.g., '1.5')") | |
| raise ArgumentTypeError(f"Invalid CPU format: {value}. Expected a floating point number (e.g., '1.5')") |
|
The CI looks to have failed, it might be related to the use of EOL images i've updated here: #332 If that's the issue a rebase will help. |
- Move ShmSize, CpuLimits, and MemoryLimits to separate constraint_extensions.py file - Create reusable validate_memory_format() helper function to avoid code duplication - Enhance memory validation to support Docker byte-value formats (2b, 1024kb, 300m, 1gb) - Update CPU error message to "Expected a floating point number (e.g., '1.5')" - Update setup.py entry points to reference new constraint_extensions module Addresses all feedback from tfoote in PR osrf#331 review comments. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
- Move ShmSize, CpuLimits, and MemoryLimits to separate constraint_extensions.py file - Create reusable validate_memory_format() helper function to avoid code duplication - Enhance memory validation to support Docker byte-value formats (2b, 1024kb, 300m, 1gb) - Update CPU error message to "Expected a floating point number (e.g., '1.5')" - Update setup.py entry points to reference new constraint_extensions module Addresses all feedback from tfoote in PR osrf#331 review comments.
tfoote
left a comment
There was a problem hiding this comment.
Thanks for the refactors it looks good with the new file and string validation.
Could you add one quick unittest for the validation function.
And with the source in a separate file it would be great to match that with splitting out the test cases into a separate file with the same name pattern.
And the .gitignore changes still need to be reverted.
With those changes this should be good to go. With the rebase I see that the tests are passing now.
tfoote
left a comment
There was a problem hiding this comment.
Thanks for moving the content and the tests to the new locations.
There's duplicate copies of the test now in the test_extension.py and please revert the .gitignore changes and this will be good to go.
.gitignore
Outdated
| dist | ||
| deb_dist | ||
| *.pyc | ||
| # Byte-compiled / optimized / DLL files |
There was a problem hiding this comment.
Please revert these changes to the .gitignore
test/test_extension.py
Outdated
| self.assertEqual(build_args, {'shm_size': '2g'}) | ||
|
|
||
|
|
||
| class CpuLimitsExtensionTest(unittest.TestCase): |
There was a problem hiding this comment.
This is now in test_constraint_extensions.py please remove this duplicate.
test/test_extension.py
Outdated
| self.assertIn('--cpus 2.5', args) | ||
|
|
||
|
|
||
| class MemoryLimitsExtensionTest(unittest.TestCase): |
There was a problem hiding this comment.
This is now in test_constraint_extensions.py please remove this duplicate too.
tfoote
left a comment
There was a problem hiding this comment.
Thanks for iterating and adding these extra features.
These extensions expose docker build and runtime resource limit arguments (shm-size, cpus, memory) to rocker users, providing better control over container resource allocation.