|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package v1alpha1 |
| 15 | + |
| 16 | +import ( |
| 17 | + "k8s.io/apimachinery/pkg/runtime" |
| 18 | + ctrl "sigs.k8s.io/controller-runtime" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/webhook" |
| 20 | + |
| 21 | + batchv1 "k8s.io/api/batch/v1" |
| 22 | +) |
| 23 | + |
| 24 | +func (r *JobSet) SetupWebhookWithManager(mgr ctrl.Manager) error { |
| 25 | + return ctrl.NewWebhookManagedBy(mgr). |
| 26 | + For(r). |
| 27 | + Complete() |
| 28 | +} |
| 29 | + |
| 30 | +//+kubebuilder:webhook:path=/mutate-batch-x-k8s-io-v1alpha1-jobset,mutating=true,failurePolicy=fail,sideEffects=None,groups=batch.x-k8s.io,resources=jobsets,verbs=create;update,versions=v1alpha1,name=mjobset.kb.io,admissionReviewVersions=v1 |
| 31 | + |
| 32 | +var _ webhook.Defaulter = &JobSet{} |
| 33 | + |
| 34 | +// Default implements webhook.Defaulter so a webhook will be registered for the type |
| 35 | +func (r *JobSet) Default() { |
| 36 | + // Default job completion mode to indexed. |
| 37 | + for i, rjob := range r.Spec.Jobs { |
| 38 | + if rjob.Template.Spec.CompletionMode == nil { |
| 39 | + r.Spec.Jobs[i].Template.Spec.CompletionMode = completionModePtr(batchv1.IndexedCompletion) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +//+kubebuilder:webhook:path=/validate-batch-x-k8s-io-v1alpha1-jobset,mutating=false,failurePolicy=fail,sideEffects=None,groups=batch.x-k8s.io,resources=jobsets,verbs=create;update,versions=v1alpha1,name=vjobset.kb.io,admissionReviewVersions=v1 |
| 45 | + |
| 46 | +var _ webhook.Validator = &JobSet{} |
| 47 | + |
| 48 | +// ValidateCreate implements webhook.Validator so a webhook will be registered for the type |
| 49 | +func (r *JobSet) ValidateCreate() error { |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type |
| 54 | +func (r *JobSet) ValidateUpdate(old runtime.Object) error { |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +// ValidateDelete implements webhook.Validator so a webhook will be registered for the type |
| 59 | +func (r *JobSet) ValidateDelete() error { |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func completionModePtr(mode batchv1.CompletionMode) *batchv1.CompletionMode { |
| 64 | + return &mode |
| 65 | +} |
0 commit comments