Skip to content
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

ecs_taskdefinition: ensure cast to integer and idempotency fix #574

Merged
merged 4 commits into from
Aug 11, 2021

Conversation

alinabuzachis
Copy link
Contributor

@alinabuzachis alinabuzachis commented May 10, 2021

SUMMARY

ecs_taskdefinition: Ensure cast to integer and fix taskdefinition creation idempotency.
Fixes: #570

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

ecs_taskdefinition

@alinabuzachis alinabuzachis changed the title ecs_taskdefinition: ensure cast to integers and fix idempotency of taskdefinition creation ecs_taskdefinition: ensure cast to integer and fix idempotency of taskdefinition creation May 10, 2021
@alinabuzachis alinabuzachis changed the title ecs_taskdefinition: ensure cast to integer and fix idempotency of taskdefinition creation ecs_taskdefinition: ensure cast to integer and idempotency fix May 10, 2021
@amackenz2048
Copy link

I get an error with this branch when using maxSwap and swappiness:

File \"/tmp/ansible_ecs_taskdefinition_payload_tdgmg8iq/ansible_ecs_taskdefinition_payload.zip/ansible/modules/ecs_taskdefinition.py\", line 832, in main\nTypeError: '<' not supported between instances of 'str' and 'int'\n",

Adding some casts fixes it. Not sure if it would be better to do inline or ensure that container has the ints.

diff --git a/plugins/modules/ecs_taskdefinition.py b/plugins/modules/ecs_taskdefinition.py
index 577b0ed..c6e488b 100644
--- a/plugins/modules/ecs_taskdefinition.py
+++ b/plugins/modules/ecs_taskdefinition.py
@@ -829,12 +829,12 @@ def main():
 
                 if linux_param == 'maxSwap' and launch_type == 'FARGATE':
                     module.fail_json(msg='maxSwap parameter is not supported with the FARGATE launch type.')
-                elif linux_param == 'maxSwap' and container['linuxParameters']['maxSwap'] < 0:
+                elif linux_param == 'maxSwap' and int(container['linuxParameters']['maxSwap']) < 0:
                     module.fail_json(msg='Accepted values for maxSwap are 0 or any positive integer.')
 
                 if (
                     linux_param == 'swappiness' and
-                    (container['linuxParameters']['swappiness'] < 0 or container['linuxParameters']['swappiness'] > 100)
+                    (int(container['linuxParameters']['swappiness']) < 0 or int(container['linuxParameters']['swappiness']) > 100)
                 ):
                     module.fail_json(msg='Accepted values for swappiness are whole numbers between 0 and 100.')

@alinabuzachis
Copy link
Contributor Author

alinabuzachis commented May 11, 2021

I get an error with this branch when using maxSwap and swappiness:

File \"/tmp/ansible_ecs_taskdefinition_payload_tdgmg8iq/ansible_ecs_taskdefinition_payload.zip/ansible/modules/ecs_taskdefinition.py\", line 832, in main\nTypeError: '<' not supported between instances of 'str' and 'int'\n",

Adding some casts fixes it. Not sure if it would be better to do inline or ensure that container has the ints.

diff --git a/plugins/modules/ecs_taskdefinition.py b/plugins/modules/ecs_taskdefinition.py
index 577b0ed..c6e488b 100644
--- a/plugins/modules/ecs_taskdefinition.py
+++ b/plugins/modules/ecs_taskdefinition.py
@@ -829,12 +829,12 @@ def main():
 
                 if linux_param == 'maxSwap' and launch_type == 'FARGATE':
                     module.fail_json(msg='maxSwap parameter is not supported with the FARGATE launch type.')
-                elif linux_param == 'maxSwap' and container['linuxParameters']['maxSwap'] < 0:
+                elif linux_param == 'maxSwap' and int(container['linuxParameters']['maxSwap']) < 0:
                     module.fail_json(msg='Accepted values for maxSwap are 0 or any positive integer.')
 
                 if (
                     linux_param == 'swappiness' and
-                    (container['linuxParameters']['swappiness'] < 0 or container['linuxParameters']['swappiness'] > 100)
+                    (int(container['linuxParameters']['swappiness']) < 0 or int(container['linuxParameters']['swappiness']) > 100)
                 ):
                     module.fail_json(msg='Accepted values for swappiness are whole numbers between 0 and 100.')

Oh, that's true. It's missing the cast to int there. I fix it right now. Thank you for trying it and letting me know. Can you try again please? Thanks.

@amackenz2048
Copy link

That works for me!

Copy link
Member

@markuman markuman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits, otherwise looks good to me.

Comment on lines +696 to +698
for param in ('maxSwap', 'swappiness', 'sharedMemorySize'):
if param in linux_param:
container['linuxParameters'][param] = int(container['linuxParameters'][param])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for param in ('maxSwap', 'swappiness', 'sharedMemorySize'):
if param in linux_param:
container['linuxParameters'][param] = int(container['linuxParameters'][param])
if linux_param in ('maxSwap', 'swappiness', 'sharedMemorySize'):`
container['linuxParameters'][param] = int(container['linuxParameters'][param])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markuman Thank you for reviewing. I used the for loop here because I need to cast to int all these parameters ('maxSwap', 'swappiness', 'sharedMemorySize'). The user can use all together.

Comment on lines +701 to +704
for limits_mapping in container['ulimits']:
for limit in ('softLimit', 'hardLimit'):
if limit in limits_mapping:
limits_mapping[limit] = int(limits_mapping[limit])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for limits_mapping in container['ulimits']:
for limit in ('softLimit', 'hardLimit'):
if limit in limits_mapping:
limits_mapping[limit] = int(limits_mapping[limit])
for limits_mapping in container['ulimits']:
for limit in ('softLimit', 'hardLimit'):
if limit == limits_mapping:
limits_mapping[limit] = int(limits_mapping[limit])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markuman limits_mapping is a dict, for this reason I used if limit in limits_mapping.

module.fail_json(msg='maxSwap parameter is not supported with the FARGATE launch type.')
elif linux_param.get('maxSwap') and linux_param['maxSwap'] < 0:
elif linux_param == 'maxSwap' and int(container['linuxParameters']['maxSwap']) < 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happen if key maxSwap does not exist? Is it possible? I've no idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for swappiness

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markuman maxSwap and swappiness are optional and they can be specified within linuxParameters. If this is what you meant. This flow here is only executed when the parameters are specified.

@alinabuzachis alinabuzachis requested review from tremble and jillr May 20, 2021 16:00
Copy link
Member

@markuman markuman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@markuman
Copy link
Member

markuman commented Jul 7, 2021

This shoud be merged before next release. Because it fixes more than the known issue.
With the current release (1.5.0) the entire linuxParameter.capabilities is not useable (worked with 2.9.12).

        containers:
          - name: somename
            linuxParameters:
              capabilities:
                add:
                  - NET_BIND_SERVICE

Currently it results in

line 806, in main\nAttributeError: 'str' object has no attribute 'get'\n"

This PR fixes also this case.

Maybe the integrationtest should also include something like

ecs_task_containers_with_linux_parameters:
- name: "{{ ecs_task_name }}"
  image: "{{ ecs_task_image_path }}"
  essential: true
  memory: "{{ ecs_task_memory }}"
  linuxParameters:
    capabilities:
      add:
        - NET_BIND_SERVICE
  portMappings:
  - containerPort: "{{ ecs_task_container_port }}"
    hostPort: "{{ ecs_task_host_port|default(0) }}"
  mountPoints: "{{ ecs_task_mount_points|default([]) }}"

fyi @alinabuzachis @jillr

@ansibullbot
Copy link

@ansibullbot ansibullbot added bug This issue/PR relates to a bug has_issue integration tests/integration module module needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR needs_triage plugins plugin (any type) tests tests labels Jul 7, 2021
@@ -190,7 +192,7 @@
linuxParameters:
description: Linux-specific modifications that are applied to the container, such as Linux kernel capabilities.
required: False
type: list
type: dict
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it's going from a list of dicts to a dict, is this not a breaking change? (Although I guess we can do this with 2.0.0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Imho) it never worked as a list. I guess it was one of the transformation errors when elements where introduced.

Copy link
Collaborator

@jillr jillr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alinabuzachis This lgtm, could you please add a changelog? As tremble noted we should call out that linuxParameters requires a dict (and that list was a bug that never worked)

Signed-off-by: Alina Buzachis <[email protected]>
Signed-off-by: Alina Buzachis <[email protected]>
Signed-off-by: Alina Buzachis <[email protected]>
@ansibullbot ansibullbot removed the needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html label Jul 13, 2021
@ansibullbot ansibullbot added community_review and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR needs_triage labels Jul 13, 2021
Copy link
Member

@markuman markuman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@ansible-zuul ansible-zuul bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ansible-zuul ansible-zuul bot merged commit ed8ad61 into ansible-collections:main Aug 11, 2021
alinabuzachis pushed a commit to alinabuzachis/community.aws that referenced this pull request May 25, 2022
)

[3.0.0] Bump minimal botocore version to 1.19.0

SUMMARY
In preparation for release 3.0.0, bump the minimal botocore version
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
README.md
plugins/doc_fragments/aws.py
plugins/lookup/aws_account_attribute.py
plugins/lookup/aws_secret.py
plugins/lookup/aws_ssm.py
plugins/module_utils/core.py
plugins/modules/s3_bucket.py
requirements.txt
tests/integration/constraints.txt
tests/integration/targets/setup_botocore_pip/defaults/main.yml
tests/unit/constraints.txt
tests/unit/module_utils/core/ansible_aws_module/test_minimal_versions.py
ADDITIONAL INFORMATION
botocore $ git show 1.19.0
tag 1.19.0
Tagger: aws-sdk-python-automation <[email protected]>
Date:   Mon Oct 19 18:09:03 2020 +0000

Tagging 1.19.0 release.

boto3 $ git show 1.16.0
tag 1.16.0
Tagger: aws-sdk-python-automation <[email protected]>
Date:   Mon Oct 19 18:08:56 2020 +0000

Tagging 1.16.0 release.

Reviewed-by: Alina Buzachis <None>
Reviewed-by: None <None>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug community_review has_issue integration tests/integration module module plugins plugin (any type) tests tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ecs_taskdefinition: Integer parameters for maxSwap and swappiness are not converted from strings.
6 participants