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

fix: skip incorrect action_date instead of failing c7n #7594

Merged

Conversation

paladin-dranser
Copy link
Contributor

Since 'action_date' is parsed in try/except block, there is an issue
if parsing of 'action_date' fails, because Python 'action_date' variable
is used later

Error example:

UnboundLocalError: local variable 'action_date' referenced before assignment

Examples:

Before:

$ custodian run --cache-period 0 --profile acloudguru -s policy -r us-east-1 policy.yaml 
2022-07-31 10:30:24,124: custodian.filters:WARNING could not parse tag:action_tag value:This EC2 instance has had less than 5 percent CPU utilization for over 5 days: stop@2022/33/03 on i-0c293fd3707bf57cb
2022-07-31 10:30:24,125: custodian.output:ERROR Error while executing policy
Traceback (most recent call last):
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/policy.py", line 290, in run
    resources = self.policy.resource_manager.resources()
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/resources/ec2.py", line 140, in resources
    return super(EC2, self).resources(query=query)
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/query.py", line 532, in resources
    resources = self.filter_resources(resources)
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/manager.py", line 111, in filter_resources
    resources = f.process(resources, event)
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/filters/core.py", line 190, in process
    return list(filter(self, resources))
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/tags.py", line 321, in __call__
    if action_date.tzinfo:
UnboundLocalError: local variable 'action_date' referenced before assignment
2022-07-31 10:30:24,126: custodian.commands:ERROR Error while executing policy action_date, continuing
Traceback (most recent call last):
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/commands.py", line 301, in run
    policy()
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/policy.py", line 1242, in __call__
    resources = mode.run()
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/policy.py", line 290, in run
    resources = self.policy.resource_manager.resources()
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/resources/ec2.py", line 140, in resources
    return super(EC2, self).resources(query=query)
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/query.py", line 532, in resources
    resources = self.filter_resources(resources)
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/manager.py", line 111, in filter_resources
    resources = f.process(resources, event)
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/filters/core.py", line 190, in process
    return list(filter(self, resources))
  File "/home/dranser/contribution/cloud-custodian/tests/terraform/tag_action_filter_call/venv/lib/python3.7/site-packages/c7n/tags.py", line 321, in __call__
    if action_date.tzinfo:
UnboundLocalError: local variable 'action_date' referenced before assignment
2022-07-31 10:30:24,126: custodian.commands:ERROR The following policies had errors while executing
 - action_date

After:

$ poetry run custodian run --cache-period 0 --profile acloudguru -s policy -r us-east-1 policy.yaml 
2022-07-31 10:28:40,530: custodian.filters:WARNING could not parse tag:action_tag value:This EC2 instance has had less than 5 percent CPU utilization for over 5 days: stop@2022/33/03 on i-0c293fd3707bf57cb
2022-07-31 10:28:40,531: custodian.filters:WARNING could not parse tag:action_tag value:This EC2 instance has had less than 5 percent CPU utilization for over 5 days: stop@2022/06/33 on i-0ce0b4bca0e7f2931
2022-07-31 10:28:40,531: custodian.policy:INFO policy:action_date resource:ec2 region:us-east-1 count:1 time:0.85
2022-07-31 10:28:41,122: custodian.policy:INFO policy:action_date action:removetag resources:1 execution_time:0.59

Since 'action_date' is parsed in try/except block, there is an issue
if parsing of 'action_date' fails, because Python 'action_date' variable
is used later

Error example:

UnboundLocalError: local variable 'action_date' referenced before assignment
@paladin-dranser
Copy link
Contributor Author

Didn't check why but CI linter finds extra linter errors so that I fixed them to pass PR checks.

@paladin-dranser
Copy link
Contributor Author

'codecov/patch' fails in files where I fixed linter errors.

@kapilt
Do I need to fix these 'codecov/patch' errors (writing tests) even if these files are not required for my changes?

@kapilt
Copy link
Collaborator

kapilt commented Aug 1, 2022

@kapilt Do I need to fix these 'codecov/patch' errors (writing tests) even if these files are not required for my changes?

I think its fine in this context (re codecov/patch failure), thanks for the pr.

Copy link
Collaborator

@kapilt kapilt left a comment

Choose a reason for hiding this comment

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

lgtm

@kapilt kapilt merged commit 8c819bf into cloud-custodian:master Aug 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants