sigs.k8s.io/kueue@v0.6.2/pkg/controller/jobs/mpijob/mpijob_webhook.go (about) 1 /* 2 Copyright 2023 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 mpijob 18 19 import ( 20 "context" 21 22 kubeflow "github.com/kubeflow/mpi-operator/pkg/apis/kubeflow/v2beta1" 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 "sigs.k8s.io/kueue/pkg/controller/jobframework" 31 ) 32 33 type MPIJobWebhook struct { 34 manageJobsWithoutQueueName bool 35 } 36 37 // SetupMPIJobWebhook configures the webhook for kubeflow MPIJob. 38 func SetupMPIJobWebhook(mgr ctrl.Manager, opts ...jobframework.Option) error { 39 options := jobframework.ProcessOptions(opts...) 40 wh := &MPIJobWebhook{ 41 manageJobsWithoutQueueName: options.ManageJobsWithoutQueueName, 42 } 43 return ctrl.NewWebhookManagedBy(mgr). 44 For(&kubeflow.MPIJob{}). 45 WithDefaulter(wh). 46 WithValidator(wh). 47 Complete() 48 } 49 50 // +kubebuilder:webhook:path=/mutate-kubeflow-org-v2beta1-mpijob,mutating=true,failurePolicy=fail,sideEffects=None,groups=kubeflow.org,resources=mpijobs,verbs=create,versions=v2beta1,name=mmpijob.kb.io,admissionReviewVersions=v1 51 52 var _ webhook.CustomDefaulter = &MPIJobWebhook{} 53 54 // Default implements webhook.CustomDefaulter so a webhook will be registered for the type 55 func (w *MPIJobWebhook) Default(ctx context.Context, obj runtime.Object) error { 56 job := fromObject(obj) 57 log := ctrl.LoggerFrom(ctx).WithName("mpijob-webhook") 58 log.V(5).Info("Applying defaults", "job", klog.KObj(job)) 59 60 jobframework.ApplyDefaultForSuspend(job, w.manageJobsWithoutQueueName) 61 return nil 62 } 63 64 // +kubebuilder:webhook:path=/validate-kubeflow-org-v2beta1-mpijob,mutating=false,failurePolicy=fail,sideEffects=None,groups=kubeflow.org,resources=mpijobs,verbs=create;update,versions=v2beta1,name=vmpijob.kb.io,admissionReviewVersions=v1 65 66 var _ webhook.CustomValidator = &MPIJobWebhook{} 67 68 // ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type 69 func (w *MPIJobWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { 70 job := fromObject(obj) 71 log := ctrl.LoggerFrom(ctx).WithName("mpijob-webhook") 72 log.Info("Validating create", "job", klog.KObj(job)) 73 return nil, validateCreate(job).ToAggregate() 74 } 75 76 func validateCreate(job jobframework.GenericJob) field.ErrorList { 77 return jobframework.ValidateCreateForQueueName(job) 78 } 79 80 // ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type 81 func (w *MPIJobWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { 82 oldJob := fromObject(oldObj) 83 newJob := fromObject(newObj) 84 log := ctrl.LoggerFrom(ctx).WithName("mpijob-webhook") 85 log.Info("Validating update", "job", klog.KObj(newJob)) 86 allErrs := jobframework.ValidateUpdateForQueueName(oldJob, newJob) 87 allErrs = append(allErrs, jobframework.ValidateUpdateForWorkloadPriorityClassName(oldJob, newJob)...) 88 return nil, allErrs.ToAggregate() 89 } 90 91 // ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type 92 func (w *MPIJobWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { 93 return nil, nil 94 }