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

feat: add exception logic #5712

Merged
merged 2 commits into from
Dec 21, 2022

Conversation

Eileen-Yu
Copy link
Contributor

Signed-off-by: Eileen Yu eileenylj@gmail.com

Explanation

Add policy exception logic to:

  • mutate
  • generate
  • imageVerify

Related issue

#2627

Milestone of this PR

What type of PR is this

/kind feature

Proposed Changes

Proof Manifests

Checklist

  • I have read the contributing guidelines.
  • I have read the PR documentation guide and followed the process including adding proof manifests to this PR.
  • This is a bug fix and I have added unit tests that prove my fix is effective.
  • This is a feature and I have added CLI tests that are applicable.
  • My PR needs to be cherry picked to a specific release branch which is .
  • My PR contains new or altered behavior to Kyverno and
    • CLI support should be added and my PR doesn't contain that functionality.
    • I have added or changed the documentation myself in an existing PR and the link is:
    • I have raised an issue in kyverno/website to track the documentation update and the link is:

Further Comments

@codecov
Copy link

codecov bot commented Dec 16, 2022

Codecov Report

Merging #5712 (bc2ec6b) into main (14445bf) will decrease coverage by 0.00%.
The diff coverage is 23.91%.

@@            Coverage Diff             @@
##             main    #5712      +/-   ##
==========================================
- Coverage   34.62%   34.62%   -0.01%     
==========================================
  Files         190      190              
  Lines       21080    21106      +26     
==========================================
+ Hits         7300     7307       +7     
- Misses      12970    12986      +16     
- Partials      810      813       +3     
Impacted Files Coverage Δ
pkg/engine/background.go 0.00% <0.00%> (ø)
pkg/engine/imageVerify.go 69.52% <20.00%> (-0.56%) ⬇️
pkg/engine/validation.go 60.81% <25.00%> (-0.36%) ⬇️
pkg/engine/mutation.go 64.59% <50.00%> (-0.57%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@eddycharly
Copy link
Member

Looks good to me.
@JimBugwadia ?

Copy link
Member

@JimBugwadia JimBugwadia left a comment

Choose a reason for hiding this comment

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

looks good! a few minor comments for improvements.

if err == nil && exception != nil {
key, err := cache.MetaNamespaceKeyFunc(exception)
logger := logging.WithName("exception")
// TODO: increase metrics
Copy link
Member

Choose a reason for hiding this comment

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

what does those comment mean? do we need to increment the rule metrics or something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is recommended by @eddycharly .
I assume this is to instrument the triggered exception count.

cc: @eddycharly Pls fix me if I'm understanding wrong, or anything you'd like to point out.

// check if there is a corresponding policy exception
exception, err := matchesException(policyContext, &rule)
// if we found an exception
if err == nil && exception != nil {
Copy link
Member

Choose a reason for hiding this comment

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

can we make the handling of the exception reusable for all the rule types? Currently we are duplicating the same 15-20 lines of code in each.

Copy link
Contributor Author

@Eileen-Yu Eileen-Yu Dec 20, 2022

Choose a reason for hiding this comment

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

Yeah, that sounds good. So I extracted the logic to a separate function shouldRespondException. Will that be good?

logger := logging.WithName("exception")
// TODO: increase metrics
if err != nil {
logger.Error(err, "failed to compute policy exception key", "namespace", exception.GetNamespace(), "name", exception.GetName())
Copy link
Member

Choose a reason for hiding this comment

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

We need to report a rule error in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good! Updated with a RuleResponse returned( assume:Status: response.RuleStatusError). Will that be good?

logger.V(3).Info("policy rule skipped due to policy exception", "exception", key)
return &response.RuleResponse{
Name: rule.Name,
Message: "Rule skipped because of PolicyException" + key,
Copy link
Member

Choose a reason for hiding this comment

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

Can we make the text lower case? `rule skipped due to policy exception " + key

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, sounds good!

log.Error(err, "failed to compute policy exception key", "namespace", exception.GetNamespace(), "name", exception.GetName())
return &response.RuleResponse{
Name: rule.Name,
Message: "failed to find matched exception" + key,
Copy link
Member

Choose a reason for hiding this comment

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

needs a space after "...exception" before we + key

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx for letting me know 👍

// if we found an exception
if err == nil && exception != nil {
key, err := cache.MetaNamespaceKeyFunc(exception)
// TODO: increase metrics
Copy link
Member

Choose a reason for hiding this comment

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

remove as we are returning an error which counts in the metrics

@@ -800,3 +788,29 @@ func matchesException(policyContext *PolicyContext, rule *kyvernov1.Rule) (*kyve
}
return nil, nil
}

func shouldRespondException(ctx *PolicyContext, rule *kyvernov1.Rule, log logr.Logger) *response.RuleResponse {
Copy link
Member

Choose a reason for hiding this comment

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

rename to hasPolicyExceptions

log.V(3).Info("policy rule skipped due to policy exception", "exception", key)
return &response.RuleResponse{
Name: rule.Name,
Message: "rule skipped due to policy exception" + key,
Copy link
Member

Choose a reason for hiding this comment

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

missing space after "....exception"

@@ -800,3 +788,29 @@ func matchesException(policyContext *PolicyContext, rule *kyvernov1.Rule) (*kyve
}
return nil, nil
}

func shouldRespondException(ctx *PolicyContext, rule *kyvernov1.Rule, log logr.Logger) *response.RuleResponse {
Copy link
Member

Choose a reason for hiding this comment

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

change return to (bool, *response.RuleResponse)

Copy link
Member

Choose a reason for hiding this comment

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

If it returns true then we process the response. Otherwise, we expect a nil response.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed in channel, comment has been added to make it clear.

Copy link
Member

@JimBugwadia JimBugwadia left a comment

Choose a reason for hiding this comment

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

minor comments, please check!

Signed-off-by: Eileen Yu <eileenylj@gmail.com>
@JimBugwadia JimBugwadia enabled auto-merge (squash) December 21, 2022 04:01
@JimBugwadia JimBugwadia merged commit baa41bc into kyverno:main Dec 21, 2022
MdSahil-oss pushed a commit to MdSahil-oss/kyverno that referenced this pull request Dec 29, 2022
Signed-off-by: Eileen Yu <eileenylj@gmail.com>

Signed-off-by: Eileen Yu <eileenylj@gmail.com>
Co-authored-by: Jim Bugwadia <jim@nirmata.com>
Signed-off-by: Md Sahil <Mohdssahil1@gmail.com>
MdSahil-oss pushed a commit to MdSahil-oss/kyverno that referenced this pull request Jan 11, 2023
Signed-off-by: Eileen Yu <eileenylj@gmail.com>

Signed-off-by: Eileen Yu <eileenylj@gmail.com>
Co-authored-by: Jim Bugwadia <jim@nirmata.com>
Signed-off-by: MdSahil-oss <Mohdssahil1@gmail.com>
MdSahil-oss pushed a commit to MdSahil-oss/kyverno that referenced this pull request Jan 11, 2023
Signed-off-by: Eileen Yu <eileenylj@gmail.com>

Signed-off-by: Eileen Yu <eileenylj@gmail.com>
Co-authored-by: Jim Bugwadia <jim@nirmata.com>
Signed-off-by: MdSahil-oss <Mohdssahil1@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants