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: Only set ConstraintTemplate's status.created on success #2208

Merged
merged 4 commits into from Aug 5, 2022

Conversation

stek29
Copy link
Contributor

@stek29 stek29 commented Jul 31, 2022

What this PR does / why we need it:
Currently ConstraintTemplate's .status.created field is set to true if there's at least one ConstraintTemplatePodStatus for it.
However, it doesn't check if ConstraintTemplatePodStatus has actually succeeded (has no errors). This PR fixes that – .status.created is only set to true if there's at least one ConstraintTemplatePodStatus for this template with no errors.

I've also noticed that Err..Code constants are used inconsistently in the constrainttemplate controller, so I've fixed that too. Please tell me if it has to be separated into a different PR.

Which issue(s) this PR fixes:
Fixes #2053

@codecov-commenter
Copy link

codecov-commenter commented Jul 31, 2022

Codecov Report

Merging #2208 (bad1f8c) into master (13692a9) will increase coverage by 0.14%.
The diff coverage is 70.00%.

@@            Coverage Diff             @@
##           master    #2208      +/-   ##
==========================================
+ Coverage   54.26%   54.41%   +0.14%     
==========================================
  Files         111      111              
  Lines        9529     9535       +6     
==========================================
+ Hits         5171     5188      +17     
+ Misses       3957     3949       -8     
+ Partials      401      398       -3     
Flag Coverage Δ
unittests 54.41% <70.00%> (+0.14%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...onstrainttemplate/constrainttemplate_controller.go 56.59% <33.33%> (+1.67%) ⬆️
...platestatus/constrainttemplatestatus_controller.go 70.58% <85.71%> (+1.56%) ⬆️
pkg/readiness/list.go 91.17% <0.00%> (+11.76%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 13692a9...bad1f8c. Read the comment docs.

@stek29 stek29 changed the title Only set ConstraintTemplate's status.created on success fix: Only set ConstraintTemplate's status.created on success Jul 31, 2022
@stek29
Copy link
Contributor Author

stek29 commented Jul 31, 2022

I'm not sure on how to test the other path (when byPod has a error), the only way I can think of is a different test which only has constraintTemplateStatus controller started, without ConstraintTemplateController running, and adding a fakePod PodStatus – and I'm not quite sure on how to do that properly.

@maxsmythe
Copy link
Contributor

thanks for the PR!

Probably the easiest way to hit the other path (all show errors) is to try to apply a constraint template with invalid Rego in it:

violation[ or similar.

@stek29 stek29 force-pushed the fix-2053 branch 2 times, most recently from bad1f8c to 151dbe9 Compare August 2, 2022 18:51
@stek29
Copy link
Contributor Author

stek29 commented Aug 2, 2022

@maxsmythe I've already tried that and failed, because I forgot to remove Constraint operations, so tests were hanging indefinitely because CRDs were missing.

Done, I've tests for .status.created being false :)

templateCpy.Spec.Targets[0].Rego = `
package foo

violtaion[invalid syntax error`
Copy link
Member

Choose a reason for hiding this comment

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

this is still a sufficient test case without the typo right? can we remove the typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ritazh the typo was accidental, didn't notice it :D

func verifyTStatusCreated(ctx context.Context, c client.Client, expected bool) func() error {
return func() error {
ct := &v1beta1.ConstraintTemplate{}
if err := c.Get(ctx, types.NamespacedName{Name: "denyall"}, ct); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

maybe not hardcode this but instead pass in a var?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

verifyTByPodStatusCount has it hardcoded, that's why I've kept it hardcoded :)

I'd suggest to add a const with a name instead of passing a var or hardcoding, would that be fine?

Copy link
Member

@ritazh ritazh left a comment

Choose a reason for hiding this comment

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

Few nits, but otherwise LGTM

Copy link
Contributor

@maxsmythe maxsmythe left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! LGTM, LMK when it's ready to merge vis-a-vis Rita's comments.

One thought:

There are some race conditions where this mistakenly points to "false" instead of true (e.g. all pods are restarted but the CRD exists from previous pods), but better to be pessimistic than optimistic.

Watching for the CRD resource could also work but would be subject to other errors (e.g. a CRD that corresponds to an old, deleted CT that has yet to be GC'd)

@stek29 stek29 force-pushed the fix-2053 branch 3 times, most recently from c17b742 to 17eec55 Compare August 3, 2022 09:06
Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>
Only set ConstraintTemplate status.created to true if at least one
ConstraintTemplatePodStatus had no errors

Fixes open-policy-agent#2053

Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>
Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>
Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>
@stek29
Copy link
Contributor Author

stek29 commented Aug 4, 2022

@maxsmythe @ritazh I've fixed the typo and moved names to constants :)

@ritazh ritazh merged commit e19a6c8 into open-policy-agent:master Aug 5, 2022
maxsmythe pushed a commit to davis-haba/gatekeeper that referenced this pull request Aug 17, 2022
…licy-agent#2208)

* fix: clean up error consts in constrainttemplate controller

Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>

* fix: only set CT status.created on success

Only set ConstraintTemplate status.created to true if at least one
ConstraintTemplatePodStatus had no errors

Fixes open-policy-agent#2053

Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>

* fix: add tests for CT status.created

Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>

* style: const for a constraint name in tests

Signed-off-by: Viktor Oreshkin <imselfish@stek29.rocks>
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.

status.created is set to true even when all pods have reported errors
4 participants