sigs.k8s.io/kueue@v0.6.2/pkg/controller/jobs/jobset/jobset_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 jobset 18 19 import ( 20 "context" 21 22 "k8s.io/apimachinery/pkg/runtime" 23 "k8s.io/klog/v2" 24 ctrl "sigs.k8s.io/controller-runtime" 25 "sigs.k8s.io/controller-runtime/pkg/webhook" 26 "sigs.k8s.io/controller-runtime/pkg/webhook/admission" 27 jobsetapi "sigs.k8s.io/jobset/api/jobset/v1alpha2" 28 29 "sigs.k8s.io/kueue/pkg/controller/constants" 30 "sigs.k8s.io/kueue/pkg/controller/jobframework" 31 ) 32 33 type JobSetWebhook struct { 34 manageJobsWithoutQueueName bool 35 } 36 37 // SetupJobSetWebhook configures the webhook for kubeflow JobSet. 38 func SetupJobSetWebhook(mgr ctrl.Manager, opts ...jobframework.Option) error { 39 options := jobframework.ProcessOptions(opts...) 40 wh := &JobSetWebhook{ 41 manageJobsWithoutQueueName: options.ManageJobsWithoutQueueName, 42 } 43 return ctrl.NewWebhookManagedBy(mgr). 44 For(&jobsetapi.JobSet{}). 45 WithDefaulter(wh). 46 WithValidator(wh). 47 Complete() 48 } 49 50 // +kubebuilder:webhook:path=/mutate-jobset-x-k8s-io-v1alpha2-jobset,mutating=true,failurePolicy=fail,sideEffects=None,groups=jobset.x-k8s.io,resources=jobsets,verbs=create,versions=v1alpha2,name=mjobset.kb.io,admissionReviewVersions=v1 51 52 var _ webhook.CustomDefaulter = &JobSetWebhook{} 53 54 // Default implements webhook.CustomDefaulter so a webhook will be registered for the type 55 func (w *JobSetWebhook) Default(ctx context.Context, obj runtime.Object) error { 56 jobSet := fromObject(obj) 57 log := ctrl.LoggerFrom(ctx).WithName("jobset-webhook") 58 log.V(5).Info("Applying defaults", "jobset", klog.KObj(jobSet)) 59 60 // If a prebuilt workload is used, propagate the name to it's replicated job templates. 61 if wlName, hasPrebuilt := jobframework.PrebuiltWorkloadFor(jobSet); hasPrebuilt { 62 for i := range jobSet.Spec.ReplicatedJobs { 63 rjTemplate := &jobSet.Spec.ReplicatedJobs[i].Template 64 if rjTemplate.Annotations == nil { 65 rjTemplate.Annotations = make(map[string]string) 66 } 67 rjTemplate.Annotations[constants.ParentWorkloadAnnotation] = wlName 68 } 69 } 70 71 jobframework.ApplyDefaultForSuspend(jobSet, w.manageJobsWithoutQueueName) 72 return nil 73 } 74 75 // +kubebuilder:webhook:path=/validate-jobset-x-k8s-io-v1alpha2-jobset,mutating=false,failurePolicy=fail,sideEffects=None,groups=jobset.x-k8s.io,resources=jobsets,verbs=create;update,versions=v1alpha2,name=vjobset.kb.io,admissionReviewVersions=v1 76 77 var _ webhook.CustomValidator = &JobSetWebhook{} 78 79 // ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type 80 func (w *JobSetWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { 81 jobSet := fromObject(obj) 82 log := ctrl.LoggerFrom(ctx).WithName("jobset-webhook") 83 log.Info("Validating create", "jobset", klog.KObj(jobSet)) 84 return nil, jobframework.ValidateCreateForQueueName(jobSet).ToAggregate() 85 } 86 87 // ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type 88 func (w *JobSetWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { 89 oldJobSet := fromObject(oldObj) 90 newJobSet := fromObject(newObj) 91 log := ctrl.LoggerFrom(ctx).WithName("jobset-webhook") 92 log.Info("Validating update", "jobset", klog.KObj(newJobSet)) 93 allErrs := jobframework.ValidateUpdateForQueueName(oldJobSet, newJobSet) 94 allErrs = append(allErrs, jobframework.ValidateCreateForQueueName(newJobSet)...) 95 allErrs = append(allErrs, jobframework.ValidateUpdateForWorkloadPriorityClassName(oldJobSet, newJobSet)...) 96 return nil, allErrs.ToAggregate() 97 } 98 99 // ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type 100 func (w *JobSetWebhook) ValidateDelete(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { 101 return nil, nil 102 }