-
Notifications
You must be signed in to change notification settings - Fork 453
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
Use webhook.CustomValidator instead of deprecated webhook.Validator. #2803
Merged
andrewsykim
merged 2 commits into
ray-project:master
from
mbobrovskyi:cleanup/use-custom-webhook-validator
Feb 5, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
|
||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
|
||
rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" | ||
) | ||
|
||
// log is for logging in this package. | ||
var ( | ||
rayclusterlog = logf.Log.WithName("raycluster-resource") | ||
nameRegex, _ = regexp.Compile("^[a-z]([-a-z0-9]*[a-z0-9])?$") | ||
) | ||
|
||
// SetupRayClusterWebhookWithManager registers the webhook for RayCluster in the manager. | ||
func SetupRayClusterWebhookWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(&rayv1.RayCluster{}). | ||
WithValidator(&RayClusterWebhook{}). | ||
Complete() | ||
} | ||
|
||
type RayClusterWebhook struct{} | ||
|
||
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. | ||
//+kubebuilder:webhook:path=/validate-ray-io-v1-raycluster,mutating=false,failurePolicy=fail,sideEffects=None,groups=ray.io,resources=rayclusters,verbs=create;update,versions=v1,name=vraycluster.kb.io,admissionReviewVersions=v1 | ||
|
||
var _ webhook.CustomValidator = &RayClusterWebhook{} | ||
|
||
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type | ||
func (w *RayClusterWebhook) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
rayCluster := obj.(*rayv1.RayCluster) | ||
rayclusterlog.Info("validate create", "name", rayCluster.Name) | ||
return nil, w.validateRayCluster(rayCluster) | ||
} | ||
|
||
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type | ||
func (w *RayClusterWebhook) ValidateUpdate(_ context.Context, _ runtime.Object, newObj runtime.Object) (admission.Warnings, error) { | ||
rayCluster := newObj.(*rayv1.RayCluster) | ||
rayclusterlog.Info("validate update", "name", rayCluster.Name) | ||
return nil, w.validateRayCluster(rayCluster) | ||
} | ||
|
||
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type | ||
func (w *RayClusterWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { | ||
return nil, nil | ||
} | ||
|
||
func (w *RayClusterWebhook) validateRayCluster(rayCluster *rayv1.RayCluster) error { | ||
var allErrs field.ErrorList | ||
|
||
if err := w.validateName(rayCluster); err != nil { | ||
allErrs = append(allErrs, err) | ||
} | ||
|
||
if err := w.validateWorkerGroups(rayCluster); err != nil { | ||
allErrs = append(allErrs, err) | ||
} | ||
|
||
if len(allErrs) == 0 { | ||
return nil | ||
} | ||
|
||
return apierrors.NewInvalid( | ||
schema.GroupKind{Group: "ray.io", Kind: "RayCluster"}, | ||
rayCluster.Name, allErrs) | ||
} | ||
|
||
func (w *RayClusterWebhook) validateName(rayCluster *rayv1.RayCluster) *field.Error { | ||
if !nameRegex.MatchString(rayCluster.Name) { | ||
return field.Invalid(field.NewPath("metadata").Child("name"), rayCluster.Name, "name must consist of lower case alphanumeric characters or '-', start with an alphabetic character, and end with an alphanumeric character (e.g. 'my-name', or 'abc-123', regex used for validation is '[a-z]([-a-z0-9]*[a-z0-9])?')") | ||
} | ||
return nil | ||
} | ||
|
||
func (w *RayClusterWebhook) validateWorkerGroups(rayCluster *rayv1.RayCluster) *field.Error { | ||
workerGroupNames := make(map[string]bool) | ||
|
||
for i, workerGroup := range rayCluster.Spec.WorkerGroupSpecs { | ||
if _, ok := workerGroupNames[workerGroup.GroupName]; ok { | ||
return field.Invalid(field.NewPath("spec").Child("workerGroupSpecs").Index(i), workerGroup, "worker group names must be unique") | ||
} | ||
workerGroupNames[workerGroup.GroupName] = true | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mbobrovskyi could you clarify why this file is moved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kubebuilder, by default, generates webhooks in the internal directory, so I believe this is the better location. Additionally,
make generate
generates DeepCopy functions for theRayClusterWebhook
, even though it is not an API object.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, would it be feasible to split into two commits ? So that the diffs are easier to read?
In any case posted #2803 (comment).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re-opening this, could we make the diff render as moving an existing file instead of a new file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can render the diff like that by looking at the first commit now: 179525c. Unfortunately after the second commit (the move) GH gets confused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't move the existent file due to RayCluster is an API object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, sorry, I think I misunderstood the previous comment. Never mind.