Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

Add validation of managed labels/annotations #134

Conversation

erikgb
Copy link
Contributor

@erikgb erikgb commented Jan 27, 2022

Closes #128. Part of #47.

This adds webhook validation of managed labels and annotations.

Tested: Added unit tests for validations. Ran e2e-tests, but no additions/modifications in this PR.

/cc @adrianludwin

@k8s-ci-robot k8s-ci-robot added do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jan 27, 2022
@k8s-ci-robot
Copy link
Contributor

Hi @erikgb. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jan 27, 2022
@erikgb erikgb force-pushed the feat/label-annotation-validation branch from b9c5dcf to 4f5145d Compare January 27, 2022 17:26
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jan 27, 2022
@k8s-ci-robot k8s-ci-robot requested a review from rjbez17 January 27, 2022 17:32
Copy link
Contributor

@adrianludwin adrianludwin left a comment

Choose a reason for hiding this comment

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

I'll only be able to look at this tomorrow.

Ryan, if you have a few minutes to check this out and like it, feel free to lgtm/approve without me! Otherwise I'll get it tomorrow.

/cc @rjbez17

@erikgb erikgb force-pushed the feat/label-annotation-validation branch from 4f5145d to 31d8df5 Compare January 27, 2022 17:48
@erikgb
Copy link
Contributor Author

erikgb commented Jan 27, 2022

FYI: I removed some refactoring/cleanup that can be done in a separate PR. Ready for review.

Copy link
Contributor

@adrianludwin adrianludwin left a comment

Choose a reason for hiding this comment

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

I think this is sufficient testing. We generally don't have e2e tests (which is what I think you mean by "integration tests"?) for webhook validation except to make sure that it's set up at all. Unit tests are more than enough to test whether the logic is correct.

Otherwise, this lgtm apart from some small requests. Thanks!

@erikgb erikgb force-pushed the feat/label-annotation-validation branch 3 times, most recently from 1b71be0 to 3e31634 Compare January 28, 2022 15:36
Copy link
Contributor

@adrianludwin adrianludwin left a comment

Choose a reason for hiding this comment

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

Sorry four last nits! LMK if you disagree with them.

Comment on lines +135 to +141
allErrs := validateManagedMeta(req.hc)
if len(allErrs) > 0 {
gk := schema.GroupKind{Group: api.GroupVersion.Group, Kind: "HierarchyConfiguration"}
err := apierrors.NewInvalid(gk, req.hc.Name, allErrs)
return webhooks.DenyFromAPIError(err)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we shorten this even more? I like the lower-level function to encapsulate as much information as possible so that the top-level function is easier to read, e.g:

if denied := validateManagedMeta(req.hc); denied != nil {
  return denied
}

Now the caller (handle()) doesn't have to know anything about the type of information coming out of validateManagedMeta other than that it's ready to be returned to its caller (Handle()).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There reason I left the allErrs there, is to make it more obvious how to include more field errors in the same response. And to avoid more refactoring if/when more fields are added to HierarchyConfiguration that require validation in this webhook.

If we were using the high-level webhook framework provided by controller-runtime, Error/field.ErrorList is the "contract" between your webhook implementation and controller-runtime. That is another reason for keeping it as suggested IMO.

So here I do not agree with you, and would like to keep it as is. But if you insist..... 😉

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting. Let's leave it as-is for now, but generally I prefer to keep the current state of the code as readable as possible, and save the flexibility until we actually use them in the PRs. But especially if you'll be adding that shortly, it's fine to go in like this.

Comment on lines 30 to 32
defer func() {
_ = config.SetManagedMeta(nil, nil)
}()
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not defer config.SetManagedMeta(nil, nil)? Is the anonymous function adding anything?

Copy link
Contributor Author

@erikgb erikgb Jan 28, 2022

Choose a reason for hiding this comment

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

Just my IDE complaining about unhandled error, but in this case it's "wrong". 😅 Fixed.

Part of kubernetes-retired#47. This adds validation of managed labels and annotations.

Tested: Added unit tests for validations. Ran e2e-tests, but no additions/modifications to those in this PR.
@erikgb erikgb force-pushed the feat/label-annotation-validation branch from 3e31634 to 7db7393 Compare January 28, 2022 16:18
Copy link
Contributor

@adrianludwin adrianludwin left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

Comment on lines +135 to +141
allErrs := validateManagedMeta(req.hc)
if len(allErrs) > 0 {
gk := schema.GroupKind{Group: api.GroupVersion.Group, Kind: "HierarchyConfiguration"}
err := apierrors.NewInvalid(gk, req.hc.Name, allErrs)
return webhooks.DenyFromAPIError(err)
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting. Let's leave it as-is for now, but generally I prefer to keep the current state of the code as readable as possible, and save the flexibility until we actually use them in the PRs. But especially if you'll be adding that shortly, it's fine to go in like this.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 28, 2022
@adrianludwin
Copy link
Contributor

/ok-to-test

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: adrianludwin, erikgb

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 approved Indicates a PR has been approved by an approver from all required OWNERS files. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jan 28, 2022
@k8s-ci-robot k8s-ci-robot merged commit 22c4260 into kubernetes-retired:master Jan 28, 2022
@adrianludwin adrianludwin added this to the release-v1.0 milestone Mar 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HierarchyConfiguration labels/annotations should be validated on admission
3 participants