Skip to content

Commit

Permalink
Add a BaseWebhook that replaces all kubeflow webhooks
Browse files Browse the repository at this point in the history
Change-Id: Ia6343aa06900654011dec92ff4136b8c2deeaf3f
  • Loading branch information
alculquicondor committed Sep 19, 2024
1 parent 82b6226 commit b44c458
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 837 deletions.
92 changes: 92 additions & 0 deletions pkg/controller/jobframework/base_webhook.go
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
}
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

0 comments on commit b44c458

Please sign in to comment.