Skip to content

fix(pkg/render/apiserver.go): Correctly add validatingadmissionpolicy rules for kubernetes 1.30+ #3781

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

philroche
Copy link

@philroche philroche commented Feb 20, 2025

The expectation was that the "validatingadmissionpolicies" and "validatingadmissionpolicybindings" rules would
be added when using kubernetes 1.30+ but this is not happening due to reversed < > logic.

This was highlighted using calico-apiserver 3.29.2 where this validation is performed by default but was unable
to when using the tigera operator due to missing rules.

This resolves issue #3780

@CLAassistant
Copy link

CLAassistant commented Feb 20, 2025

CLA assistant check
All committers have signed the CLA.

… rules for kubernetes 1.30+

The expectation was that the "validatingadmissionpolicies" and "validatingadmissionpolicybindings" rules would
be added when using kubernetes 1.30+ but this is not happening due to reversed < > logic.

This was highlighted using calico-apiserver 3.29.2 where this validation is performed by default but was unable
to when using the tigera operator due to missing rules.

This resolves issue tigera#3780
@philroche philroche force-pushed the fix/apiserver-validating-admission-policy-rules branch from 61af20c to 99e56be Compare February 20, 2025 12:50
@philroche philroche marked this pull request as ready for review February 20, 2025 12:52
@philroche philroche requested a review from a team as a code owner February 20, 2025 12:52
@rene-dekker
Copy link
Member

Hi Phil, thank you for your contribution. I did add a small suggestion, other than that it looks good.

… "validatingadmission*" rules


We need to also include 1.30, since that is the earliest version that ships the API.

Co-authored-by: Rene Dekker <[email protected]>
@rene-dekker
Copy link
Member

/merge-when-ready

@marvin-tigera
Copy link
Contributor

OK, I will merge the pull request when it's ready, leave the commits as is when I merge it, and leave the branch after I've merged it.

@@ -651,7 +651,7 @@ func (c *apiServerComponent) calicoCustomResourcesClusterRole() *rbacv1.ClusterR
},
},
}
if c.cfg.KubernetesVersion == nil || !(c.cfg.KubernetesVersion != nil && c.cfg.KubernetesVersion.Major < 2 && c.cfg.KubernetesVersion.Minor < 30) {
if c.cfg.KubernetesVersion == nil || !(c.cfg.KubernetesVersion != nil && c.cfg.KubernetesVersion.Major < 2 && c.cfg.KubernetesVersion.Minor >= 30) {
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 sure we have unit tests that verify the correct behavior for relevant versions?

e.g., 1.29, 1.30, 1.31

Copy link
Author

Choose a reason for hiding this comment

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

I am not familiar with the codebase yet and was hoping i'd learn how to run the tests via CI check logs.

I'll spend some time understanding the tests.

Copy link
Member

@rene-dekker rene-dekker left a comment

Choose a reason for hiding this comment

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

On second glance I missed the !. I think the original code was correct. That said, it is an overly hard-to-read statement.

I think it can be simplified to if c.cfg.KubernetesVersion == nil || (c.cfg.KubernetesVersion.Major == 1 && c.cfg.KubernetesVersion.Minor >= 30) {

@philroche
Copy link
Author

philroche commented Feb 20, 2025

On second glance I missed the !. I think the original code was correct. That said, it is an overly hard-to-read statement.

I think it can be simplified to if c.cfg.KubernetesVersion == nil || (c.cfg.KubernetesVersion.Major == 1 && c.cfg.KubernetesVersion.Minor >= 30) {

On further review you are right yes, but in my tests the rules are not being added when testing with Kubernetes 1.32

@philroche
Copy link
Author

@rene-dekker Thanks for pointing out my error. Revisiting the original if statement

if c.cfg.KubernetesVersion == nil || !(c.cfg.KubernetesVersion != nil && c.cfg.KubernetesVersion.Major < 2 && c.cfg.KubernetesVersion.Minor < 30) {

And given a test kubernetes version of 1.32

# if c.cfg.KubernetesVersion == nil || !(c.cfg.KubernetesVersion != nil && c.cfg.KubernetesVersion.Major < 2 && c.cfg.KubernetesVersion.Minor < 30)

c.cfg.KubernetesVersion = 1.32

c.cfg.KubernetesVersion.Major = 1

c.cfg.KubernetesVersion.Minor = 32

# !(c.cfg.KubernetesVersion != nil && c.cfg.KubernetesVersion.Major < 2 && c.cfg.KubernetesVersion.Minor < 30)

(c.cfg.KubernetesVersion != nil) = true

(c.cfg.KubernetesVersion.Major < 2) = true

(c.cfg.KubernetesVersion.Minor < 30) = false 

(true && true && false) = false 

!(false) = true 

# c.cfg.KubernetesVersion == nil || !(c.cfg.KubernetesVersion != nil && c.cfg.KubernetesVersion.Major < 2 && c.cfg.KubernetesVersion.Minor < 30)

(c.cfg.KubernetesVersion == nil) = false

(false || true) = true

So evaluating to true should be adding the rules. I didn't see this in my tests. I did see the other rules being added though.

I think it can be simplified to if c.cfg.KubernetesVersion == nil || (c.cfg.KubernetesVersion.Major == 1 && c.cfg.KubernetesVersion.Minor >= 30) {

Yes, much more readable

@caseydavenport
Copy link
Member

@philroche FYI we'll need you to sign the CLA in order to make the bot happy before merging

@philroche
Copy link
Author

@philroche FYI we'll need you to sign the CLA in order to make the bot happy before merging

Yup I have escalated to Chainguard legal team to make sure that's OK for me using my Chainguard email.

@philroche
Copy link
Author

Further debug output added to original issue @ #3780 (comment)

@philroche
Copy link
Author

@caseydavenport CLA signed

@danudey danudey modified the milestones: v1.38.0, v1.39.0 Mar 21, 2025
Copy link

This PR is stale because it has been open for 60 days with no activity.

@github-actions github-actions bot added the stale label Jun 10, 2025
@caseydavenport
Copy link
Member

/sem-approve

@caseydavenport
Copy link
Member

Sorry this got delayed.. I am not sure if this is still needed, but it seems like there are some test failures.

@radTuti radTuti modified the milestones: v1.39.0, v1.40.0 Jun 18, 2025
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.

7 participants