Skip to content

Commit

Permalink
Rename Defaulter to LosslessDefaulter
Browse files Browse the repository at this point in the history
Change-Id: I232b1f2cd1a8a0c6b2f2fa7506da4d17d3e3ae55
  • Loading branch information
alculquicondor committed Sep 25, 2024
1 parent de03011 commit c2f6eca
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/jobframework/base_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func DefaultWebhookFactory(job GenericJob, fromObject func(runtime.Object) Gener
}
return webhook.WebhookManagedBy(mgr).
For(job.Object()).
WithMutationHandler(webhook.WithDefaulter(mgr.GetScheme(), job.Object(), wh)).
WithMutationHandler(webhook.WithLosslessDefaulter(mgr.GetScheme(), job.Object(), wh)).
WithValidator(wh).
Complete()
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/controller/jobframework/webhook/defaulter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// WithDefaulter creates a new Handler for a CustomDefaulter interface that **drops** remove operations.
func WithDefaulter(scheme *runtime.Scheme, obj runtime.Object, defaulter admission.CustomDefaulter) admission.Handler {
return &defaulterForType{
// WithLosslessDefaulter creates a new Handler for a CustomDefaulter interface that **drops** remove operations,
// which are typically the result of new API fields not present in Kueue libraries.
func WithLosslessDefaulter(scheme *runtime.Scheme, obj runtime.Object, defaulter admission.CustomDefaulter) admission.Handler {
return &losslessDefaulter{
Handler: admission.WithCustomDefaulter(scheme, obj, defaulter).Handler,
}
}

type defaulterForType struct {
type losslessDefaulter struct {
admission.Handler
}

Expand All @@ -42,7 +43,7 @@ type defaulterForType struct {
// that are not present in the go types, which can occur when Kueue libraries are behind the latest
// released CRDs.
// Dropping the "remove" operations is safe because Kueue's job mutators never remove fields.
func (h *defaulterForType) Handle(ctx context.Context, req admission.Request) admission.Response {
func (h *losslessDefaulter) Handle(ctx context.Context, req admission.Request) admission.Response {
response := h.Handler.Handle(ctx, req)
if response.Allowed {
var patches []jsonpatch.Operation
Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/jobframework/webhook/defaulter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,22 @@ func (*TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) err
return nil
}

func TestDefaulter(t *testing.T) {
func TestLossLessDefaulter(t *testing.T) {
sch := runtime.NewScheme()
builder := scheme.Builder{GroupVersion: testResourceGVK.GroupVersion()}
builder.Register(&TestResource{})
if err := builder.AddToScheme(sch); err != nil {
t.Fatalf("Couldn't add types to scheme: %v", err)
}

handler := WithDefaulter(sch, &TestResource{}, &TestCustomDefaulter{})
handler := WithLosslessDefaulter(sch, &TestResource{}, &TestCustomDefaulter{})

req := admission.Request{
AdmissionRequest: admissionv1.AdmissionRequest{
Kind: metav1.GroupVersionKind(testResourceGVK),
Object: runtime.RawExtension{
// This raw object has a field not defined in the go type.
// controller-runtime CustomDefaulter would have added a remove operation for it.
Raw: []byte(`{"baz": "qux"}`),
},
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/jobs/deployment/deployment_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func SetupWebhook(mgr ctrl.Manager, opts ...jobframework.Option) error {
obj := &appsv1.Deployment{}
return webhook.WebhookManagedBy(mgr).
For(obj).
WithMutationHandler(webhook.WithDefaulter(mgr.GetScheme(), obj, wh)).
WithMutationHandler(webhook.WithLosslessDefaulter(mgr.GetScheme(), obj, wh)).
WithValidator(wh).
Complete()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/jobs/job/job_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func SetupWebhook(mgr ctrl.Manager, opts ...jobframework.Option) error {
obj := &batchv1.Job{}
return webhook.WebhookManagedBy(mgr).
For(obj).
WithMutationHandler(webhook.WithDefaulter(mgr.GetScheme(), obj, wh)).
WithMutationHandler(webhook.WithLosslessDefaulter(mgr.GetScheme(), obj, wh)).
WithValidator(wh).
Complete()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/jobs/jobset/jobset_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func SetupJobSetWebhook(mgr ctrl.Manager, opts ...jobframework.Option) error {
obj := &jobsetapi.JobSet{}
return webhook.WebhookManagedBy(mgr).
For(obj).
WithMutationHandler(webhook.WithDefaulter(mgr.GetScheme(), obj, wh)).
WithMutationHandler(webhook.WithLosslessDefaulter(mgr.GetScheme(), obj, wh)).
WithValidator(wh).
Complete()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/jobs/pod/pod_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func SetupWebhook(mgr ctrl.Manager, opts ...jobframework.Option) error {
obj := &corev1.Pod{}
return webhook.WebhookManagedBy(mgr).
For(obj).
WithMutationHandler(webhook.WithDefaulter(mgr.GetScheme(), obj, wh)).
WithMutationHandler(webhook.WithLosslessDefaulter(mgr.GetScheme(), obj, wh)).
WithValidator(wh).
Complete()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/jobs/raycluster/raycluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func SetupRayClusterWebhook(mgr ctrl.Manager, opts ...jobframework.Option) error
obj := &rayv1.RayCluster{}
return webhook.WebhookManagedBy(mgr).
For(obj).
WithMutationHandler(webhook.WithDefaulter(mgr.GetScheme(), obj, wh)).
WithMutationHandler(webhook.WithLosslessDefaulter(mgr.GetScheme(), obj, wh)).
WithValidator(wh).
Complete()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/jobs/rayjob/rayjob_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func SetupRayJobWebhook(mgr ctrl.Manager, opts ...jobframework.Option) error {
obj := &rayv1.RayJob{}
return webhook.WebhookManagedBy(mgr).
For(obj).
WithMutationHandler(webhook.WithDefaulter(mgr.GetScheme(), obj, wh)).
WithMutationHandler(webhook.WithLosslessDefaulter(mgr.GetScheme(), obj, wh)).
WithValidator(wh).
Complete()
}
Expand Down

0 comments on commit c2f6eca

Please sign in to comment.