From 8ea11e3c7a45b1eb0b40fe1e314bd88fbdcf151d Mon Sep 17 00:00:00 2001 From: David Grove Date: Fri, 16 Aug 2024 16:34:21 -0400 Subject: [PATCH] Get dev mode (make run) working again (#232) When running without webhooks, apply defaults that are normally set by the webhook during the Empty==>Suspended transition. --- .../appwrapper/appwrapper_controller.go | 16 +++++++++++++--- internal/webhook/appwrapper_webhook.go | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/controller/appwrapper/appwrapper_controller.go b/internal/controller/appwrapper/appwrapper_controller.go index 925422c..f18db2a 100644 --- a/internal/controller/appwrapper/appwrapper_controller.go +++ b/internal/controller/appwrapper/appwrapper_controller.go @@ -40,8 +40,11 @@ import ( "sigs.k8s.io/controller-runtime/pkg/handler" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/reconcile" + "sigs.k8s.io/kueue/pkg/controller/jobframework" + utilmaps "sigs.k8s.io/kueue/pkg/util/maps" workloadv1beta2 "github.com/project-codeflare/appwrapper/api/v1beta2" + wlc "github.com/project-codeflare/appwrapper/internal/controller/workload" "github.com/project-codeflare/appwrapper/pkg/config" "github.com/project-codeflare/appwrapper/pkg/utils" ) @@ -156,13 +159,20 @@ func (r *AppWrapperReconciler) Reconcile(ctx context.Context, req ctrl.Request) case workloadv1beta2.AppWrapperEmpty: // initial state if !controllerutil.ContainsFinalizer(aw, AppWrapperFinalizer) { - // When deployed normally (with webhook enabled); this block is expected to be unreachable. - // However, we need it to support `make run` (local dev of controller without the webhook). + // The AppWrapperFinalizer is added by our webhook, so if we get here it means that we are + // running in dev mode (`make run`) which disables the webhook. To make dev mode as + // useful as possible, replicate as much of AppWrapperWebhook.Default() as we can without having the admission.Request. + if r.Config.EnableKueueIntegrations { + if r.Config.DefaultQueueName != "" { + aw.Labels = utilmaps.MergeKeepFirst(aw.Labels, map[string]string{"kueue.x-k8s.io/queue-name": r.Config.DefaultQueueName}) + } + jobframework.ApplyDefaultForSuspend((*wlc.AppWrapper)(aw), r.Config.KueueJobReconciller.ManageJobsWithoutQueueName) + } controllerutil.AddFinalizer(aw, AppWrapperFinalizer) if err := r.Update(ctx, aw); err != nil { return ctrl.Result{}, err } - log.FromContext(ctx).Info("Finalizer Added") + log.FromContext(ctx).Info("No webhook: applied default initializations") } orig := copyForStatusPatch(aw) diff --git a/internal/webhook/appwrapper_webhook.go b/internal/webhook/appwrapper_webhook.go index d281d48..e35ba4b 100644 --- a/internal/webhook/appwrapper_webhook.go +++ b/internal/webhook/appwrapper_webhook.go @@ -77,7 +77,7 @@ func (w *AppWrapperWebhook) Default(ctx context.Context, obj runtime.Object) err if w.Config.DefaultQueueName != "" { aw.Labels = utilmaps.MergeKeepFirst(aw.Labels, map[string]string{QueueNameLabel: w.Config.DefaultQueueName}) } - jobframework.ApplyDefaultForSuspend((*wlc.AppWrapper)(aw), true) + jobframework.ApplyDefaultForSuspend((*wlc.AppWrapper)(aw), w.Config.KueueJobReconciller.ManageJobsWithoutQueueName) } // inject labels with user name and id