|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "regexp" |
| 6 | + |
| 7 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 8 | + "k8s.io/apimachinery/pkg/runtime" |
| 9 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 10 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 11 | + ctrl "sigs.k8s.io/controller-runtime" |
| 12 | + logf "sigs.k8s.io/controller-runtime/pkg/log" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 14 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 15 | + |
| 16 | + rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" |
| 17 | +) |
| 18 | + |
| 19 | +// log is for logging in this package. |
| 20 | +var ( |
| 21 | + rayclusterlog = logf.Log.WithName("raycluster-resource") |
| 22 | + nameRegex, _ = regexp.Compile("^[a-z]([-a-z0-9]*[a-z0-9])?$") |
| 23 | +) |
| 24 | + |
| 25 | +// SetupRayClusterWebhookWithManager registers the webhook for RayCluster in the manager. |
| 26 | +func SetupRayClusterWebhookWithManager(mgr ctrl.Manager) error { |
| 27 | + return ctrl.NewWebhookManagedBy(mgr). |
| 28 | + For(&rayv1.RayCluster{}). |
| 29 | + WithValidator(&RayClusterWebhook{}). |
| 30 | + Complete() |
| 31 | +} |
| 32 | + |
| 33 | +type RayClusterWebhook struct{} |
| 34 | + |
| 35 | +// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation. |
| 36 | +//+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 |
| 37 | + |
| 38 | +var _ webhook.CustomValidator = &RayClusterWebhook{} |
| 39 | + |
| 40 | +// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type |
| 41 | +func (w *RayClusterWebhook) ValidateCreate(_ context.Context, obj runtime.Object) (admission.Warnings, error) { |
| 42 | + rayCluster := obj.(*rayv1.RayCluster) |
| 43 | + rayclusterlog.Info("validate create", "name", rayCluster.Name) |
| 44 | + return nil, w.validateRayCluster(rayCluster) |
| 45 | +} |
| 46 | + |
| 47 | +// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type |
| 48 | +func (w *RayClusterWebhook) ValidateUpdate(_ context.Context, _ runtime.Object, newObj runtime.Object) (admission.Warnings, error) { |
| 49 | + rayCluster := newObj.(*rayv1.RayCluster) |
| 50 | + rayclusterlog.Info("validate update", "name", rayCluster.Name) |
| 51 | + return nil, w.validateRayCluster(rayCluster) |
| 52 | +} |
| 53 | + |
| 54 | +// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type |
| 55 | +func (w *RayClusterWebhook) ValidateDelete(_ context.Context, _ runtime.Object) (admission.Warnings, error) { |
| 56 | + return nil, nil |
| 57 | +} |
| 58 | + |
| 59 | +func (w *RayClusterWebhook) validateRayCluster(rayCluster *rayv1.RayCluster) error { |
| 60 | + var allErrs field.ErrorList |
| 61 | + |
| 62 | + if err := w.validateName(rayCluster); err != nil { |
| 63 | + allErrs = append(allErrs, err) |
| 64 | + } |
| 65 | + |
| 66 | + if err := w.validateWorkerGroups(rayCluster); err != nil { |
| 67 | + allErrs = append(allErrs, err) |
| 68 | + } |
| 69 | + |
| 70 | + if len(allErrs) == 0 { |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + return apierrors.NewInvalid( |
| 75 | + schema.GroupKind{Group: "ray.io", Kind: "RayCluster"}, |
| 76 | + rayCluster.Name, allErrs) |
| 77 | +} |
| 78 | + |
| 79 | +func (w *RayClusterWebhook) validateName(rayCluster *rayv1.RayCluster) *field.Error { |
| 80 | + if !nameRegex.MatchString(rayCluster.Name) { |
| 81 | + 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])?')") |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (w *RayClusterWebhook) validateWorkerGroups(rayCluster *rayv1.RayCluster) *field.Error { |
| 87 | + workerGroupNames := make(map[string]bool) |
| 88 | + |
| 89 | + for i, workerGroup := range rayCluster.Spec.WorkerGroupSpecs { |
| 90 | + if _, ok := workerGroupNames[workerGroup.GroupName]; ok { |
| 91 | + return field.Invalid(field.NewPath("spec").Child("workerGroupSpecs").Index(i), workerGroup, "worker group names must be unique") |
| 92 | + } |
| 93 | + workerGroupNames[workerGroup.GroupName] = true |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
0 commit comments