Skip to content

Commit

Permalink
Avoid reconciliation on CR status/condition updates
Browse files Browse the repository at this point in the history
Signed-off-by: Shiva Krishna, Merla <[email protected]>
  • Loading branch information
shivamerla committed Aug 29, 2024
1 parent 0af0135 commit 9957c12
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
16 changes: 16 additions & 0 deletions internal/controller/nimcache_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"
"io"
"reflect"
"strings"
"time"

Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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)
}

Expand Down
21 changes: 21 additions & 0 deletions internal/controller/nimpipeline_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
16 changes: 16 additions & 0 deletions internal/controller/nimservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit 9957c12

Please sign in to comment.