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

Add extend CLI for generate policies proposal #6

Merged
merged 14 commits into from
Jun 2, 2022

Conversation

shubham4443
Copy link
Contributor

Signed-off-by: Shubham Nazare shubham4443@gmail.com

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.

Had a few questions for advanced support:

  • can we handle context lookups for generate policies using CLI? (Is this supported in validate and mutate policies via CLI?)
  • can we handle JMESPath variables? (Is this supported in mutate and validate?)
  • can we use existing resources in the cluster for this CLI test?
  • how do we handle policies with spec.rules.generate.synchronize set to true?


The method is similar to testing mutate policies. User will provide the desired resource manifest file as generatedResource in results array of the test file. This manifest file will be compared with the Kyverno generated resource and accordingly the test will display the result as pass or fail.

Additionally, like in the case of testing mutate policies, we provide the resource on which we want to perform mutation. For generate policies, the triggering resource should be provided on creation/updation of which our generation rule is applied.
Copy link
Member

Choose a reason for hiding this comment

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

..provided on creation/update of which..


### Mock Generate-Request Controller

To process generate rules, we need a Generate-Request Controller. We can make a simple instance of the Controller with only the Kubernetes' dynamic client. The dynamic client must be a fake one so as to not process the rule on an actual cluster. Additionally, the triggering resource and pre-existing resource for the clone object should exist in the cluster. The dynamic client will be initialized with these resources.
Copy link
Member

Choose a reason for hiding this comment

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

Based on the above proposal, all test resources are local resources, do we need the dynamic client?


### Mock Generate-Request Controller

To process generate rules, we need a Generate-Request Controller. We can make a simple instance of the Controller with only the Kubernetes' dynamic client. The dynamic client must be a fake one so as to not process the rule on an actual cluster. Additionally, the triggering resource and pre-existing resource for the clone object should exist in the cluster. The dynamic client will be initialized with these resources.
Copy link
Member

Choose a reason for hiding this comment

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

Based on the above proposal, all test resources are local resources, do we need the dynamic client?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes we require the dynamic client otherwise its not possible to invoke the function responsible for handling generate policies.

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.

Had a few questions for advanced support:

  • can we handle context lookups for generate policies using CLI? (Is this supported in validate and mutate policies via CLI?)
  • can we handle JMESPath variables? (Is this supported in mutate and validate?)
  • can we use existing resources in the cluster for this CLI test?
  • how do we handle policies with spec.rules.generate.synchronize set to true?

@chipzoller
Copy link
Contributor

One of the objectives of this enhancement would be to ensure all the presently-available generate policies on kyverno/policies can be tested successfully.

@vyankyGH
Copy link
Contributor

vyankyGH commented Apr 5, 2022

@shubham4443 Have we updated document for Namespace based generate policies ?

@shubham4443
Copy link
Contributor Author

Hi @chipzoller @realshuting
kyverno/kyverno#3456 has been merged. Is there anything left in this design proposal?

@chipzoller
Copy link
Contributor

Hi @chipzoller @realshuting kyverno/kyverno#3456 has been merged. Is there anything left in this design proposal?

There are comments asked by Shuting which you have not addressed. At this point, since we didn't really follow the process we created, let's just make sure this KDP reflects the current state of whatever was implemented. In other words, if there are things in this KDP which are different based on the current state, go back and update the KDP accordingly.

@shubham4443
Copy link
Contributor Author

Hi @chipzoller @realshuting kyverno/kyverno#3456 has been merged. Is there anything left in this design proposal?

There are comments asked by Shuting which you have not addressed. At this point, since we didn't really follow the process we created, let's just make sure this KDP reflects the current state of whatever was implemented. In other words, if there are things in this KDP which are different based on the current state, go back and update the KDP accordingly.

I addressed those comments in our weekly contributor's meeting. Nevertheless, I will answer those questions again. I have gone through the KDP just now and there aren't any things different from the current state.

@shubham4443
Copy link
Contributor Author

Had a few questions for advanced support:

  • can we handle context lookups for generate policies using CLI? (Is this supported in validate and mutate policies via CLI?)
  • can we handle JMESPath variables? (Is this supported in mutate and validate?)
  • can we use existing resources in the cluster for this CLI test?
  • how do we handle policies with spec.rules.generate.synchronize set to true?

Answers to the questions -

  • Yes - We can handle context lookups for generate policies and it is supported in validate and mutate policies too.
  • Yes - We can handle JMESPath variables and it is supported in validate and mutate policies too.
  • No
  • We are not handling policies with spec.rules.generate.synchronize set to true any differently.

@chipzoller chipzoller requested a review from realshuting May 30, 2022 14:06
@chipzoller
Copy link
Contributor

@realshuting to provide final review and merge.

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.

@shubham4443 - looks good overall.

Have a question regarding the name, see below


## Proposal

When using a generate rule, the origin resource can be either an existing resource defined within Kubernetes, or a new resource defined in the rule itself. When the origin resource is a pre-existing resource the clone object is used. When the origin resource is a new resource defined within the manifest of the rule, the data object is used. Both rules are mutually exclusive, and only one will be specified in a rule. Both objects require a different approach for testing through CLI command.
Copy link
Member

Choose a reason for hiding this comment

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

We usually have the trigger and the target resources in a generate policy. What is this origin resource?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its the same as target resource. In the documentation https://kyverno.io/docs/writing-policies/generate/ it is mentioned as origin resource so I used the same word.

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 rename them to the target resource? Ideally we should be consistent when referencing trigger and target resources for generate and mutate existing policies.


### Method to test data object

The method is similar to testing mutate policies. User will provide the desired resource manifest file as generatedResource in the results array of the test file. This manifest file will be compared with the Kyverno generated resource and accordingly the test will display the result as pass or fail.
Copy link
Member

Choose a reason for hiding this comment

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

.. is similar to the testing for mutate policies.

result: pass
```

User should provide the pre-existing resource in the cloneSourceResource field of the results array. The cloneSourceResource.yaml for the above test file will look like this -
Copy link
Member

Choose a reason for hiding this comment

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

.. in the cloneSourceResource field ..

@realshuting
Copy link
Member

@shubham4443 - can you resolve conversations that were addressed and sign off your commits?

shubham4443 and others added 13 commits June 1, 2022 12:55
Signed-off-by: Shubham Nazare <shubham4443@gmail.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: Shubham Nazare <shubham4443@gmail.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: Shubham Nazare <shubham4443@gmail.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: Shubham Nazare <shubham4443@gmail.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: Shubham Nazare <shubham4443@gmail.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: Shubham Nazare <shubham4443@gmail.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: Naman Lakhwani <namanlakhwani@gmail.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: Jim Bugwadia <jim@nirmata.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: Jim Bugwadia <jim@nirmata.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: Jim Bugwadia <jim@nirmata.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: Sambhav Kothari <skothari44@bloomberg.net>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
Signed-off-by: shubham4443 <shubham4443@gmail.com>
@shubham4443 shubham4443 force-pushed the extend_cli_for_generate_policies branch from 6e20ff8 to 691f028 Compare June 1, 2022 07:25
@vyankyGH
Copy link
Contributor

vyankyGH commented Jun 1, 2022

@shubham4443 please resolves conflicts

@realshuting
Copy link
Member

Was the implementation PR merged? May need to the change status on main page.

@shubham4443
Copy link
Contributor Author

Was the implementation PR merged? May need to the change status on main page.

Yes

@chipzoller chipzoller merged commit 0b764a1 into kyverno:main Jun 2, 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.

7 participants