From 9957c12a3c669a51a60df79af41d3c7039592fb9 Mon Sep 17 00:00:00 2001 From: "Shiva Krishna, Merla" Date: Thu, 29 Aug 2024 11:52:14 -0700 Subject: [PATCH] Avoid reconciliation on CR status/condition updates Signed-off-by: Shiva Krishna, Merla --- internal/controller/nimcache_controller.go | 16 ++++++++++++++ internal/controller/nimpipeline_controller.go | 21 +++++++++++++++++++ internal/controller/nimservice_controller.go | 16 ++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/internal/controller/nimcache_controller.go b/internal/controller/nimcache_controller.go index 28d38937..25aa9da3 100644 --- a/internal/controller/nimcache_controller.go +++ b/internal/controller/nimcache_controller.go @@ -22,6 +22,7 @@ import ( "encoding/json" "fmt" "io" + "reflect" "strings" "time" @@ -49,7 +50,9 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/predicate" ) const ( @@ -196,6 +199,19 @@ func (r *NIMCacheReconciler) SetupWithManager(mgr ctrl.Manager) error { Owns(&batchv1.Job{}). Owns(&corev1.Pod{}). Owns(&corev1.PersistentVolumeClaim{}). + WithEventFilter(predicate.Funcs{ + UpdateFunc: func(e event.UpdateEvent) bool { + // Type assert to NIMCache + if oldNIMCache, ok := e.ObjectOld.(*appsv1alpha1.NIMCache); ok { + newNIMCache := e.ObjectNew.(*appsv1alpha1.NIMCache) + + // Handle only spec updates + return !reflect.DeepEqual(oldNIMCache.Spec, newNIMCache.Spec) + } + // For other types we watch, reconcile them + return true + }, + }). Complete(r) } diff --git a/internal/controller/nimpipeline_controller.go b/internal/controller/nimpipeline_controller.go index 99db836b..80a9390d 100644 --- a/internal/controller/nimpipeline_controller.go +++ b/internal/controller/nimpipeline_controller.go @@ -19,6 +19,7 @@ package controller import ( "context" "fmt" + "reflect" utils "github.com/NVIDIA/k8s-nim-operator/internal/utils" corev1 "k8s.io/api/core/v1" @@ -28,7 +29,9 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/predicate" appsv1alpha1 "github.com/NVIDIA/k8s-nim-operator/api/apps/v1alpha1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -209,11 +212,16 @@ func (r *NIMPipelineReconciler) syncResource(ctx context.Context, currentNamespa logger.V(2).Info("NIMService spec has changed, updating") if errors.IsNotFound(err) { + // Resource doesn't exist, so create it err = r.Create(ctx, desired) if err != nil { return err } } else { + // Resource exists, so update it + // Ensure the resource version is carried over to the desired object + desired.ResourceVersion = current.ResourceVersion + err = r.Update(ctx, desired) if err != nil { return err @@ -353,5 +361,18 @@ func (r *NIMPipelineReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&appsv1alpha1.NIMPipeline{}). Owns(&appsv1alpha1.NIMService{}). + WithEventFilter(predicate.Funcs{ + UpdateFunc: func(e event.UpdateEvent) bool { + // Type assert to NIMPipeline + if oldNIMPipeline, ok := e.ObjectOld.(*appsv1alpha1.NIMPipeline); ok { + newNIMPipeline := e.ObjectNew.(*appsv1alpha1.NIMPipeline) + + // Handle only spec updates + return !reflect.DeepEqual(oldNIMPipeline.Spec, newNIMPipeline.Spec) + } + // For other types we watch, reconcile them + return true + }, + }). Complete(r) } diff --git a/internal/controller/nimservice_controller.go b/internal/controller/nimservice_controller.go index 08a1c971..7867cfe2 100644 --- a/internal/controller/nimservice_controller.go +++ b/internal/controller/nimservice_controller.go @@ -18,6 +18,7 @@ package controller import ( "context" + "reflect" appsv1alpha1 "github.com/NVIDIA/k8s-nim-operator/api/apps/v1alpha1" "github.com/NVIDIA/k8s-nim-operator/internal/conditions" @@ -35,7 +36,9 @@ import ( ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/predicate" ) // NIMServiceFinalizer is the finalizer annotation @@ -185,5 +188,18 @@ func (r *NIMServiceReconciler) SetupWithManager(mgr ctrl.Manager) error { Owns(&rbacv1.RoleBinding{}). Owns(&networkingv1.Ingress{}). Owns(&autoscalingv2.HorizontalPodAutoscaler{}). + WithEventFilter(predicate.Funcs{ + UpdateFunc: func(e event.UpdateEvent) bool { + // Type assert to NIMService + if oldNIMService, ok := e.ObjectOld.(*appsv1alpha1.NIMService); ok { + newNIMService := e.ObjectNew.(*appsv1alpha1.NIMService) + + // Handle only spec updates + return !reflect.DeepEqual(oldNIMService.Spec, newNIMService.Spec) + } + // For other types we watch, reconcile them + return true + }, + }). Complete(r) }