[go: up one dir, main page]

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

[Bug] Enabling many-to-one comparisons for AnyNotIn operator #9462

Merged
merged 17 commits into from
Apr 22, 2024

Conversation

1Shubham7
Copy link
Contributor
@1Shubham7 1Shubham7 commented Jan 20, 2024

Explanation

The AnyNotIn operator can't evaluate many-to-one comparisons (e.g. where key is array of int and value is int) for any datatype. This causes the rule in the webhook work differently from the CLI.

Related issue

Fixes #9319

Milestone of this PR

Documentation (optional)

My PR contains new or altered behavior to Kyverno.

What type of PR is this

/kind bug

Proposed Changes

I have added support for these many-to-one comparisons for all datatypes.

Proof Manifests

Before

Step 1. The example Policy below uses AnyNotIn operator and has a key of [0,1,2] and value is 0, so obviously it should evaluate to true, but since AnyNotIn as of now doesn't support comparison when key is array of int and partially in value, the default implementation is false.

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: test-policy
spec:
  validationFailureAction: Enforce
  background: false
  rules:
    - name: test-rule
      match:
        any:
        - resources:
            kinds:
              - ConfigMap
            names:
              - kyvernotest
            operations:
              - CREATE
      validate:
        message: Must have all.
        deny:
          conditions:
            all:
            - key:
              - 0
              - 1
              - 2
              operator: AnyNotIn
              value: 0

Step 2. So after applying that policy, when we create this cm

kubectl create cm kyvernotest --from-literal=foo=bar

Step 3. It allows the Configmap to be created.

configmap/kyvernotest created

After

After implementing these changes, AnyNotIn checks if the key of type []int is not included in value which is true and the cm is blocked. So when we now apply the policy and create the configmap

kubectl create cm kyvernotest --from-literal=foo=bar

It blocks the cm, and the result is

error: failed to create configmap: admission webhook "validate.kyverno.svc-fail" denied the request: 

resource ConfigMap/default/kyvernotest was blocked due to the following policies 

test-policy:
  test-rule: Must have all.

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.

Further Comments

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>
Copy link
codecov bot commented Jan 20, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 10.12%. Comparing base (022fc4f) to head (62c6b18).

Files Patch % Lines
pkg/engine/variables/operator/anyin.go 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9462      +/-   ##
==========================================
+ Coverage   10.11%   10.12%   +0.01%     
==========================================
  Files        1030     1030              
  Lines       91757    91763       +6     
==========================================
+ Hits         9285     9295      +10     
+ Misses      81455    81452       -3     
+ Partials     1017     1016       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@1Shubham7 1Shubham7 marked this pull request as ready for review January 27, 2024 12:44
@chipzoller
Copy link
Contributor

Please add unit tests which cover any missing scenarios for this operator in your PR.

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>
Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>
@1Shubham7 1Shubham7 changed the title [Bug] Enabling many-to-one comparisons in AnyNotIn (and other operators) [Bug] Enabling many-to-one comparisons for AnyNotIn operator Jan 27, 2024
Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>
@1Shubham7
Copy link
Contributor Author
1Shubham7 commented Jan 27, 2024

@chipzoller, I have added tests for scenarios like:

  • key is float
  • key is bool
  • key is array of int
  • key is array of float
  • key is array of bool
    this PR deals with last three scenarios.

@chipzoller
Copy link
Contributor

Please run go fmt

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>
@1Shubham7
Copy link
Contributor Author

@chipzoller, please have a look.

@chipzoller
Copy link
Contributor

You don't need to keep mentioning users upon every comment. This triggers a push notification every time is makes unnecessary noise when we already subscribe to these PRs/issues.

@chipzoller
Copy link
Contributor

@eddycharly or @realshuting: can you please ensure this is correct and we have 100% coverage on unit tests for all combinations and all types of supported values?

@realshuting
Copy link
Member

Please explain the changes in full so the reviewer understands what has been fixed.

The unit tests need to be fixed.

@chipzoller
Copy link
Contributor

PR template is also not complete.

@1Shubham7
Copy link
Contributor Author
1Shubham7 commented Feb 16, 2024

PR template is also not complete.

sorry for that, now its done.

@1Shubham7 1Shubham7 changed the title [Bug] Enabling many-to-one comparisons for AnyNotIn operator [Bug] Enabling many-to-one comparisons for AnyNotIn operator Feb 19, 2024
Copy link
Contributor
@chipzoller chipzoller left a comment

Choose a reason for hiding this comment

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

Will this also cover the same behavior in the value as well as the key? All of these operators need to work a similar way for both key and value fields.

@1Shubham7
Copy link
Contributor Author

Will this also cover the same behavior in the value as well as the key? All of these operators need to work a similar way for both key and value fields.

no, that will defy the current logic i.e. "AnyNotIn checks that ANY of the keys are NOT found in the values." . See if key = [0,1,2] and value = 0 then since 1,2 are not in value, AnyNotIn is true. but when key = 0 and value =[0,1,2] now 0 is in value therefore false.

@realshuting
Copy link
Member

Do we need to clarify this new behavior in our doc?

@1Shubham7
Copy link
Contributor Author
1Shubham7 commented Apr 17, 2024

Do we need to clarify this new behavior in our doc?

no, this is not new behavior, it is just a bug that defies existing behavior. Docs already says this - AnyNotIn: checks that ANY of the keys are NOT found in the values. link

Copy link
Member
@realshuting realshuting left a comment

Choose a reason for hiding this comment

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

/lgtm
Thanks @1Shubham7.

@realshuting realshuting enabled auto-merge (squash) April 18, 2024 10:02
@MariamFahmy98 MariamFahmy98 added this to the Kyverno Release 1.13.0 milestone Apr 22, 2024
@MariamFahmy98
Copy link
Contributor

@1Shubham7 - Could you please resolve all the comments? So that we can merge it.

@1Shubham7
Copy link
Contributor Author

@1Shubham7 - Could you please resolve all the comments? So that we can merge it.

Hey Mariam, all the comments are already resolved.

@realshuting realshuting merged commit dbc12ac into kyverno:main Apr 22, 2024
248 of 250 checks passed
jslivka pushed a commit to jslivka/kyverno-hpa that referenced this pull request Jul 2, 2024
…no#9462)

* added cases for int, float

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

* added bool as well

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

* added tests

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

* some more tests

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

* go fmt

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

* fixed the failing test cases

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>

---------

Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>
Co-authored-by: shuting <shuting@nirmata.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.

[Bug] AnyNotIn operator expects value as array/string and not float64
4 participants