-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use webhook.CustomValidator instead of deprecated webhook.Validator.
- Loading branch information
1 parent
aafe2e0
commit 7a8b212
Showing
5 changed files
with
113 additions
and
102 deletions.
There are no files selected for viewing
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