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

Refactor AdmissionPolicy for code sharing with mutating #122919

Merged

Conversation

alexzielenski
Copy link
Contributor

@alexzielenski alexzielenski commented Jan 22, 2024

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

KEP-3962 adds MutatingAdmissionPolicy. This PR refactors the ValidatingAdmissionPolicy code to be useable from mutatingadmissionpolicy, and makes it look a lot like the familiar generic webhook implementation.

ValidatingAdmissionPolicy controller was already split into controller.go and controller_reconcile.go. This separation just happened to be the same separation used for the webhook framework's dispatcher and source concepts; so it was natural to refactor it in the same way.

I created a generic admission Plugin framework (that may also be merged with webhooks eventually? sicne it is very similar) which links together a Source and a Dispatcher. The source is in charge of gathering configurations and digesting them into "Hooks", or in our case matching policies/bindings/params. The dispatcher evaluates the compiled & matched policies/bindings from the source to evaluate them against an admissoin request.

For mutating and validating admission policy, we can share the same PolicySource[P, B] to manage params, matching policies to bindings, and compilation where P, B are the types for the (Validating/Mutating)Admission(Policy/Biinding). Most of the code for controller_reconcile.go was used for this implementation.

The dispatcher for validating/mutating will be different. For validating, it was a small refactor to change controller.go into a dispatcher in the new framework. Mutating dispatcher will come in a future PR.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

/assign @cici37 @jiahuif

Does this PR introduce a user-facing change?

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

- [KEP-3962]: https://github.com/kubernetes/enhancements/issues/3962

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. labels Jan 22, 2024
@k8s-ci-robot k8s-ci-robot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. labels Jan 22, 2024
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. area/apiserver sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/apps Categorizes an issue or PR as relevant to SIG Apps. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Jan 22, 2024
@alexzielenski alexzielenski force-pushed the apiserver/policy/mutating-initial branch 11 times, most recently from 89dd6c1 to 68ba230 Compare January 24, 2024 19:59
@alexzielenski alexzielenski changed the title [WIP] Refactor AdmissionPolicy for code sharing with mutating Refactor AdmissionPolicy for code sharing with mutating Jan 26, 2024
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 26, 2024
@alexzielenski
Copy link
Contributor Author

I think this is ready for review @cici37

@alexzielenski alexzielenski force-pushed the apiserver/policy/mutating-initial branch from 96380f5 to e608dee Compare February 12, 2024 18:59
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 12, 2024
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 12, 2024
@alexzielenski alexzielenski force-pushed the apiserver/policy/mutating-initial branch from e608dee to 4b725ba Compare February 13, 2024 03:30
Copy link
Contributor

@jpbetz jpbetz left a comment

Choose a reason for hiding this comment

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

I've reviewed all the implementation code. Are we satisfied with the test coverage?

// and we'd have a no-op after comparing resource versions on the next sync.
klog.Infof("refreshing policies")
policies, err := s.calculatePolicyData()
s.policies.Store(&policies)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this policies update when a non-nil err is returned intentional?

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, added godocs to make it more clear. err is non-nil if a policy had a configuration error. We still want those partial policies included in the result to produce errors for the user.

@alexzielenski alexzielenski force-pushed the apiserver/policy/mutating-initial branch 2 times, most recently from d04d7d8 to c3e28ac Compare February 13, 2024 20:22
@alexzielenski
Copy link
Contributor Author

alexzielenski commented Feb 13, 2024

I've reviewed all the implementation code. Are we satisfied with the test coverage?

The current refactor passes all existing tests. I reviewed a combined test coverage output for the unit and integration tests against k8s.io/apiserver/pkg/admission/plugin/policy/...:

profile.cov.zip

80% package coverage. The new files of interest have following coverage:

  • policy/generic/policy_source.go: 88.1% (~500 LOC)
  • policy/generic/plugin.go: 80.4% (~200 LOC)
  • policy/validating/accessor.go: 92..9% (~100 LOC)
  • policy/validating/dispatcher.go: 81.5% (~500 LOC)

I reviewed the remaining lines which for the most part are unreachable by precondition. I think coverage is good to continue for now. We will have time later in release to improve it even more.

@alexzielenski alexzielenski force-pushed the apiserver/policy/mutating-initial branch from c3e28ac to b7e70f0 Compare February 13, 2024 21:06
@jpbetz
Copy link
Contributor

jpbetz commented Feb 14, 2024

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 14, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 7fad7602acf583611744a6e2f39445700fa6e502

@deads2k
Copy link
Contributor

deads2k commented Feb 14, 2024

Validation and controllers deps cel compiler which moved. The usage didn't change, so we're equivalent

/approve

Copy link
Member

@BenTheElder BenTheElder left a comment

Choose a reason for hiding this comment

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

/approve
for vendor/

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alexzielenski, BenTheElder, cici37, deads2k, jpbetz

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 14, 2024
@k8s-ci-robot
Copy link
Contributor

@alexzielenski: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubernetes-node-e2e-containerd b7e70f0 link unknown /test pull-kubernetes-node-e2e-containerd
pull-kubernetes-e2e-gce b7e70f0 link unknown /test pull-kubernetes-e2e-gce

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@alexzielenski
Copy link
Contributor Author

/retest

@k8s-ci-robot k8s-ci-robot merged commit 684a997 into kubernetes:master Feb 14, 2024
13 of 15 checks passed
@k8s-ci-robot k8s-ci-robot added this to the v1.30 milestone Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/apiserver cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/apps Categorizes an issue or PR as relevant to SIG Apps. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

8 participants