Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a BaseWebhook that replaces all kubeflow webhooks #3102

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions pkg/controller/jobframework/base_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
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()
}
}

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
}

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
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 The Kubernetes Authors.
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.
Expand All @@ -14,19 +14,26 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package mpijob
package jobframework_test

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
kfmpi "github.com/kubeflow/mpi-operator/pkg/apis/kubeflow/v2beta1"
"k8s.io/apimachinery/pkg/runtime"

"sigs.k8s.io/kueue/pkg/controller/jobframework"
"sigs.k8s.io/kueue/pkg/controller/jobs/mpijob"
testingutil "sigs.k8s.io/kueue/pkg/util/testingjobs/mpijob"
)

func TestDefault(t *testing.T) {
func toMPIJob(o runtime.Object) jobframework.GenericJob {
return (*mpijob.MPIJob)(o.(*kfmpi.MPIJob))
}

func TestBaseWebhookDefault(t *testing.T) {
testcases := map[string]struct {
job *kfmpi.MPIJob
manageJobsWithoutQueueName bool
Expand All @@ -44,7 +51,10 @@ func TestDefault(t *testing.T) {
}
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
w := &MPIJobWebhook{manageJobsWithoutQueueName: tc.manageJobsWithoutQueueName}
w := &jobframework.BaseWebhook{
ManageJobsWithoutQueueName: tc.manageJobsWithoutQueueName,
FromObject: toMPIJob,
}
if err := w.Default(context.Background(), tc.job); err != nil {
t.Errorf("set defaults to a kubeflow/mpijob by a Defaulter")
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/controller/jobs/kubeflow/jobs/mxjob/mxjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ import (
var (
gvk = kftraining.SchemeGroupVersion.WithKind(kftraining.MXJobKind)
FrameworkName = "kubeflow.org/mxjob"

SetupMXJobWebhook = jobframework.DefaultWebhookFactory(
NewJob(),
func(o runtime.Object) jobframework.GenericJob {
return fromObject(o)
},
)
)

// +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
// +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

func init() {
utilruntime.Must(jobframework.RegisterIntegration(FrameworkName, jobframework.IntegrationCallbacks{
SetupIndexes: SetupIndexes,
Expand Down
92 changes: 0 additions & 92 deletions pkg/controller/jobs/kubeflow/jobs/mxjob/mxjob_webhook.go

This file was deleted.

56 changes: 0 additions & 56 deletions pkg/controller/jobs/kubeflow/jobs/mxjob/mxjob_webhook_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ import (
var (
gvk = kftraining.SchemeGroupVersion.WithKind(kftraining.PaddleJobKind)
FrameworkName = "kubeflow.org/paddlejob"

SetupPaddleJobWebhook = jobframework.DefaultWebhookFactory(
NewJob(),
func(o runtime.Object) jobframework.GenericJob {
return fromObject(o)
},
)
)

// +kubebuilder:webhook:path=/mutate-kubeflow-org-v1-paddlejob,mutating=true,failurePolicy=fail,sideEffects=None,groups=kubeflow.org,resources=paddlejobs,verbs=create,versions=v1,name=mpaddlejob.kb.io,admissionReviewVersions=v1
// +kubebuilder:webhook:path=/validate-kubeflow-org-v1-paddlejob,mutating=false,failurePolicy=fail,sideEffects=None,groups=kubeflow.org,resources=paddlejobs,verbs=create;update,versions=v1,name=vpaddlejob.kb.io,admissionReviewVersions=v1

func init() {
utilruntime.Must(jobframework.RegisterIntegration(FrameworkName, jobframework.IntegrationCallbacks{
SetupIndexes: SetupIndexes,
Expand Down
Loading