sigs.k8s.io/kueue@v0.6.2/pkg/webhooks/localqueue_webhook.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package webhooks 18 19 import ( 20 "context" 21 22 apivalidation "k8s.io/apimachinery/pkg/api/validation" 23 "k8s.io/apimachinery/pkg/runtime" 24 "k8s.io/apimachinery/pkg/util/validation/field" 25 "k8s.io/klog/v2" 26 ctrl "sigs.k8s.io/controller-runtime" 27 "sigs.k8s.io/controller-runtime/pkg/webhook" 28 "sigs.k8s.io/controller-runtime/pkg/webhook/admission" 29 30 kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1" 31 ) 32 33 type LocalQueueWebhook struct{} 34 35 func setupWebhookForLocalQueue(mgr ctrl.Manager) error { 36 return ctrl.NewWebhookManagedBy(mgr). 37 For(&kueue.LocalQueue{}). 38 WithValidator(&LocalQueueWebhook{}). 39 Complete() 40 } 41 42 // +kubebuilder:webhook:path=/validate-kueue-x-k8s-io-v1beta1-localqueue,mutating=false,failurePolicy=fail,sideEffects=None,groups=kueue.x-k8s.io,resources=localqueues,verbs=create;update,versions=v1beta1,name=vlocalqueue.kb.io,admissionReviewVersions=v1 43 44 var _ webhook.CustomValidator = &LocalQueueWebhook{} 45 46 // ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type 47 func (w *LocalQueueWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { 48 q := obj.(*kueue.LocalQueue) 49 log := ctrl.LoggerFrom(ctx).WithName("localqueue-webhook") 50 log.V(5).Info("Validating create", "localQueue", klog.KObj(q)) 51 return nil, ValidateLocalQueue(q).ToAggregate() 52 } 53 54 // ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type 55 func (w *LocalQueueWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { 56 newQ := newObj.(*kueue.LocalQueue) 57 oldQ := oldObj.(*kueue.LocalQueue) 58 log := ctrl.LoggerFrom(ctx).WithName("localqueue-webhook") 59 log.V(5).Info("Validating update", "localQueue", klog.KObj(newQ)) 60 return nil, ValidateLocalQueueUpdate(newQ, oldQ).ToAggregate() 61 } 62 63 // ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type 64 func (w *LocalQueueWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { 65 return nil, nil 66 } 67 68 func ValidateLocalQueue(q *kueue.LocalQueue) field.ErrorList { 69 var allErrs field.ErrorList 70 clusterQueuePath := field.NewPath("spec", "clusterQueue") 71 allErrs = append(allErrs, validateNameReference(string(q.Spec.ClusterQueue), clusterQueuePath)...) 72 return allErrs 73 } 74 75 func ValidateLocalQueueUpdate(newObj, oldObj *kueue.LocalQueue) field.ErrorList { 76 return apivalidation.ValidateImmutableField(newObj.Spec.ClusterQueue, oldObj.Spec.ClusterQueue, field.NewPath("spec", "clusterQueue")) 77 }