-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixup conversion of memory and cpu settings to support k8s resource r…
…equest format (#11725) fix memory and cpu settings to suport k8s resource request format * fix conversion of memory setting to bytes This setting has not been getting set by default, and needed some fixing up to be compatible with setting the memory in the same way as we set it in the operator, as well as with other changes from last year which assume that ansible runner is returning memory in bytes. This way we can start setting this setting in the operator, and get a more accurate reflection of how much memory is available to the control pod in k8s. On platforms where services are all sharing memory, we deduct a penalty from the memory available. On k8s we don't need to do this because the web, redis, and task containers each have memory allocated to them. * Support CPU setting expressed in units used by k8s This setting has not been getting set by default, and needed some fixing up to be compatible with setting the CPU resource request/limits in the same way as we set it in the resource requests/limits. This way we can start setting this setting in the operator, and get a more accurate reflection of how much cpu is available to the control pod in k8s. Because cpu on k8s can be partial cores, migrate cpu field to decimal. k8s does not allow granularity of less than 100m (equivalent to 0.1 cores), so only store up to 1 decimal place. fix analytics to deal with decimal cpu need to use DjangoJSONEncoder when Decimal fields in data passed to json.dumps
- Loading branch information
Showing
7 changed files
with
202 additions
and
41 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,19 @@ | ||
# Generated by Django 2.2.24 on 2022-02-14 17:37 | ||
|
||
from decimal import Decimal | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('main', '0157_inventory_labels'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='instance', | ||
name='cpu', | ||
field=models.DecimalField(decimal_places=1, default=Decimal('0'), editable=False, max_digits=4), | ||
), | ||
] |
This file contains 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
61 changes: 61 additions & 0 deletions
61
awx/main/tests/unit/settings/test_k8s_resource_setttings.py
This file contains 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,61 @@ | ||
import pytest | ||
|
||
from unittest import mock | ||
|
||
from awx.main.utils.common import ( | ||
convert_mem_str_to_bytes, | ||
get_mem_effective_capacity, | ||
get_corrected_memory, | ||
convert_cpu_str_to_decimal_cpu, | ||
get_cpu_effective_capacity, | ||
get_corrected_cpu, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value,converted_value,mem_capacity", | ||
[ | ||
('2G', 2000000000, 19), | ||
('4G', 4000000000, 38), | ||
('2Gi', 2147483648, 20), | ||
('2.1G', 1, 1), # expressing memory with non-integers is not supported, and we'll fall back to 1 fork for memory capacity. | ||
('4Gi', 4294967296, 40), | ||
('2M', 2000000, 1), | ||
('3M', 3000000, 1), | ||
('2Mi', 2097152, 1), | ||
('2048Mi', 2147483648, 20), | ||
('4096Mi', 4294967296, 40), | ||
('64G', 64000000000, 610), | ||
('64Garbage', 1, 1), | ||
], | ||
) | ||
def test_SYSTEM_TASK_ABS_MEM_conversion(value, converted_value, mem_capacity): | ||
with mock.patch('django.conf.settings') as mock_settings: | ||
mock_settings.SYSTEM_TASK_ABS_MEM = value | ||
mock_settings.SYSTEM_TASK_FORKS_MEM = 100 | ||
mock_settings.IS_K8S = True | ||
assert convert_mem_str_to_bytes(value) == converted_value | ||
assert get_corrected_memory(-1) == converted_value | ||
assert get_mem_effective_capacity(-1) == mem_capacity | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"value,converted_value,cpu_capacity", | ||
[ | ||
('2', 2.0, 8), | ||
('1.5', 1.5, 6), | ||
('100m', 0.1, 1), | ||
('2000m', 2.0, 8), | ||
('4MillionCPUm', 1.0, 4), # Any suffix other than 'm' is not supported, we fall back to 1 CPU | ||
('Random', 1.0, 4), # Any setting value other than integers, floats millicores (e.g 1, 1.0, or 1000m) is not supported, fall back to 1 CPU | ||
('2505m', 2.5, 10), | ||
('1.55', 1.6, 6), | ||
], | ||
) | ||
def test_SYSTEM_TASK_ABS_CPU_conversion(value, converted_value, cpu_capacity): | ||
with mock.patch('django.conf.settings') as mock_settings: | ||
mock_settings.SYSTEM_TASK_ABS_CPU = value | ||
mock_settings.SYSTEM_TASK_FORKS_CPU = 4 | ||
assert convert_cpu_str_to_decimal_cpu(value) == converted_value | ||
assert get_corrected_cpu(-1) == converted_value | ||
assert get_cpu_effective_capacity(-1) == cpu_capacity |
This file contains 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