generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a BaseWebhook that replaces all kubeflow webhooks
Change-Id: Ia6343aa06900654011dec92ff4136b8c2deeaf3f
- Loading branch information
1 parent
82b6226
commit b44c458
Showing
19 changed files
with
162 additions
and
837 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package jobframework | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
"k8s.io/klog/v2" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/webhook/admission" | ||
) | ||
|
||
// BaseWebhook applies basic defaulting and validation for jobs. | ||
type BaseWebhook struct { | ||
ManageJobsWithoutQueueName bool | ||
FromObject func(runtime.Object) GenericJob | ||
} | ||
|
||
func DefaultWebhookFactory(job GenericJob, fromObject func(runtime.Object) GenericJob) func(ctrl.Manager, ...Option) error { | ||
return func(mgr ctrl.Manager, opts ...Option) error { | ||
options := ProcessOptions(opts...) | ||
wh := &BaseWebhook{ | ||
ManageJobsWithoutQueueName: options.ManageJobsWithoutQueueName, | ||
FromObject: fromObject, | ||
} | ||
return ctrl.NewWebhookManagedBy(mgr). | ||
For(job.Object()). | ||
WithDefaulter(wh). | ||
WithValidator(wh). | ||
Complete() | ||
} | ||
} | ||
|
||
// +kubebuilder:webhook:path=/mutate-kubeflow-org-v1-mxjob,mutating=true,failurePolicy=fail,sideEffects=None,groups=kubeflow.org,resources=mxjobs,verbs=create,versions=v1,name=mmxjob.kb.io,admissionReviewVersions=v1 | ||
|
||
var _ admission.CustomDefaulter = &BaseWebhook{} | ||
|
||
// Default implements webhook.CustomDefaulter so a webhook will be registered for the type | ||
func (w *BaseWebhook) Default(ctx context.Context, obj runtime.Object) error { | ||
job := w.FromObject(obj) | ||
log := ctrl.LoggerFrom(ctx) | ||
log.V(5).Info("Applying defaults", "job", klog.KObj(job.Object())) | ||
ApplyDefaultForSuspend(job, w.ManageJobsWithoutQueueName) | ||
return nil | ||
} | ||
|
||
// +kubebuilder:webhook:path=/validate-kubeflow-org-v1-mxjob,mutating=false,failurePolicy=fail,sideEffects=None,groups=kubeflow.org,resources=mxjobs,verbs=create;update,versions=v1,name=vmxjob.kb.io,admissionReviewVersions=v1 | ||
|
||
var _ admission.CustomValidator = &BaseWebhook{} | ||
|
||
// ValidateCreate implements webhook.CustomValidator so a webhook will be registered for the type | ||
func (w *BaseWebhook) ValidateCreate(ctx context.Context, obj runtime.Object) (admission.Warnings, error) { | ||
job := w.FromObject(obj) | ||
log := ctrl.LoggerFrom(ctx) | ||
log.V(5).Info("Validating create", "job", klog.KObj(job.Object())) | ||
return nil, validateCreate(job).ToAggregate() | ||
} | ||
|
||
func validateCreate(job GenericJob) field.ErrorList { | ||
return ValidateJobOnCreate(job) | ||
} | ||
|
||
// ValidateUpdate implements webhook.CustomValidator so a webhook will be registered for the type | ||
func (w *BaseWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj runtime.Object) (admission.Warnings, error) { | ||
oldJob := w.FromObject(oldObj) | ||
newJob := w.FromObject(newObj) | ||
log := ctrl.LoggerFrom(ctx).WithName("mxjob-webhook") | ||
log.Info("Validating update", "job", klog.KObj(newJob.Object())) | ||
allErrs := ValidateJobOnUpdate(oldJob, newJob) | ||
return nil, allErrs.ToAggregate() | ||
} | ||
|
||
// ValidateDelete implements webhook.CustomValidator so a webhook will be registered for the type | ||
func (w *BaseWebhook) ValidateDelete(context.Context, runtime.Object) (admission.Warnings, error) { | ||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
56 changes: 0 additions & 56 deletions
56
pkg/controller/jobs/kubeflow/jobs/mxjob/mxjob_webhook_test.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.