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

feat: Add webhook for ClusterResourcePlacement #249

Merged
merged 3 commits into from
Aug 26, 2022
Merged

Conversation

Fei-Guo
Copy link
Contributor

@Fei-Guo Fei-Guo commented Aug 26, 2022

Description of your changes

This change adds basic webhook functionality to validate the ClusterResourcePlacement object.

Besides the general selector validity check, this change check if the resource listed in the selector is a cluster scoped resource.
The implementation relies on the dynamic informer manager in the change detector controller.

I also refactor the utils package to avoid circular import dependencies. The pkg/utils/validator directory can be
removed by the follow up change (not in this change).

I have:

  • Run make reviewable to ensure this PR is ready for review.

How has this code been tested

Deploy the webhook in an e2e environment and performance a few manual tests:

  • Specifying a pod object in the CRP is denied
  • Specifying a namespace object in the CRP succeeds
  • Specifying a CR without installing the CRD is denied
  • Specifying a CR after installing the CRD is succeeds

@Fei-Guo Fei-Guo changed the title Add webhook for ClusterResourcePlacement feat: Add webhook for ClusterResourcePlacement Aug 26, 2022
@@ -3,7 +3,7 @@ Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/

package utils
package informermanager
Copy link
Contributor

Choose a reason for hiding this comment

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

this package name is the same as the main structure. We normally want to avoid the informermanager. informermanager

)

// Reconciler reconciles a MemberCluster object
type Reconciler struct {
// the informer contains the cache for all the resources we need
InformerManager utils.InformerManager
InformerManager informermanager.InformerManager
Copy link
Contributor

Choose a reason for hiding this comment

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

this does not read right. Maybe we can leave this refactor to another PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

or change this to informers.Manager

allErr := make([]error, 0)

for _, selector := range clusterResourcePlacement.Spec.ResourceSelectors {
//TODO: make sure the selector's gvk is valid
Copy link
Contributor

Choose a reason for hiding this comment

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

looks now like you can do this because there is restMapper

continue
}

if !ResourceInformer.IsClusterScopedResources(restMapping.Resource) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The map inside the ResourceInformer is only initialized after the resource detector is initialized. It also has a delay when a new CRD is created so this might fail in between. I am not sure if there is a way to make sure the map is stable before this is called

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The nil check can avoid the initialization problem by suggesting retry. The delay of picking up crd can be mitigated by adding watcher to crds later. I can suggest retrying if user is sure that the gvk is a cluster scope resource in the error msg.

@Fei-Guo
Copy link
Contributor Author

Fei-Guo commented Aug 26, 2022

/ok-to-test

@ryanzhang-oss ryanzhang-oss self-requested a review August 26, 2022 22:43
@ryanzhang-oss ryanzhang-oss merged commit 4c0164d into main Aug 26, 2022
@Fei-Guo Fei-Guo deleted the fguo-dev1 branch September 13, 2022 03:38
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.

2 participants