Jelajahi Sumber

fix: narrow pushsecret watch predicate

Signed-off-by: Lucas Alves <lucasalves@tremendous.com>
Lucas Alves 2 bulan lalu
induk
melakukan
c717ed3454
1 mengubah file dengan 35 tambahan dan 13 penghapusan
  1. 35 13
      pkg/controllers/pushsecret/pushsecret_controller.go

+ 35 - 13
pkg/controllers/pushsecret/pushsecret_controller.go

@@ -89,24 +89,12 @@ type Reconciler struct {
 	ControllerClass string
 }
 
-type generationChangedOrDeletionPredicate struct {
-	predicate.GenerationChangedPredicate
-}
-
-func (generationChangedOrDeletionPredicate) Update(e event.UpdateEvent) bool {
-	if e.ObjectNew.GetDeletionTimestamp() != nil {
-		return true
-	}
-	return e.ObjectNew.GetGeneration() != e.ObjectOld.GetGeneration()
-}
-
 // storeInfo holds the identifying attributes of a secret store for per-store processing.
 type storeInfo struct {
 	Name   string
 	Kind   string
 	Labels map[string]string
 }
-}
 
 // SetupWithManager sets up the controller with the Manager.
 // It configures the controller to watch PushSecret resources and
@@ -144,10 +132,44 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
 
 	return ctrl.NewControllerManagedBy(mgr).
 		WithOptions(opts).
-		For(&esapi.PushSecret{}, builder.WithPredicates(generationChangedOrDeletionPredicate{})).
+		For(&esapi.PushSecret{}, builder.WithPredicates(pushSecretWatchPredicate())).
 		Complete(r)
 }
 
+func pushSecretWatchPredicate() predicate.Predicate {
+	return predicate.Funcs{
+		CreateFunc: func(event.CreateEvent) bool {
+			return true
+		},
+		DeleteFunc: func(event.DeleteEvent) bool {
+			return true
+		},
+		UpdateFunc: func(e event.UpdateEvent) bool {
+			if e.ObjectOld == nil || e.ObjectNew == nil {
+				return true
+			}
+
+			return shouldReconcilePushSecretUpdate(e.ObjectOld, e.ObjectNew)
+		},
+	}
+}
+
+func shouldReconcilePushSecretUpdate(oldObj, newObj client.Object) bool {
+	if oldObj.GetGeneration() != newObj.GetGeneration() {
+		return true
+	}
+	if !maps.Equal(oldObj.GetLabels(), newObj.GetLabels()) {
+		return true
+	}
+	if !maps.Equal(oldObj.GetAnnotations(), newObj.GetAnnotations()) {
+		return true
+	}
+
+	oldDeleting := oldObj.GetDeletionTimestamp() != nil
+	newDeleting := newObj.GetDeletionTimestamp() != nil
+	return oldDeleting != newDeleting
+}
+
 // Reconcile is part of the main kubernetes reconciliation loop which aims to
 // move the current state of the cluster closer to the desired state.
 // For more details, check Reconcile and its Result here: