66 "context"
77 "errors"
88 "fmt"
9+ "maps"
10+ "reflect"
911 "slices"
1012 "time"
1113
@@ -36,6 +38,7 @@ import (
3638 "sigs.k8s.io/controller-runtime/pkg/client"
3739 "sigs.k8s.io/controller-runtime/pkg/controller"
3840 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
41+ "sigs.k8s.io/controller-runtime/pkg/event"
3942 "sigs.k8s.io/controller-runtime/pkg/handler"
4043 "sigs.k8s.io/controller-runtime/pkg/log"
4144 "sigs.k8s.io/controller-runtime/pkg/predicate"
@@ -79,7 +82,8 @@ func (r *BundleReconciler) SetupWithManager(mgr ctrl.Manager) error {
7982 For (& fleet.Bundle {}).
8083 // Note: Maybe improve with WatchesMetadata, does it have access to labels?
8184 Watches (
82- // Fan out from bundledeployment to bundle
85+ // Fan out from bundledeployment to bundle, this is useful to update the
86+ // bundle's status fields.
8387 & fleet.BundleDeployment {},
8488 handler .EnqueueRequestsFromMapFunc (func (ctx context.Context , a client.Object ) []ctrl.Request {
8589 bd := a .(* fleet.BundleDeployment )
@@ -103,7 +107,7 @@ func (r *BundleReconciler) SetupWithManager(mgr ctrl.Manager) error {
103107 builder .WithPredicates (bundleDeploymentStatusChangedPredicate ()),
104108 ).
105109 Watches (
106- // Fan out from cluster to bundle
110+ // Fan out from cluster to bundle, this is useful for targeting and templating.
107111 & fleet.Cluster {},
108112 handler .EnqueueRequestsFromMapFunc (func (ctx context.Context , a client.Object ) []ctrl.Request {
109113 cluster := a .(* fleet.Cluster )
@@ -123,13 +127,54 @@ func (r *BundleReconciler) SetupWithManager(mgr ctrl.Manager) error {
123127
124128 return requests
125129 }),
126- builder .WithPredicates (predicate. ResourceVersionChangedPredicate {} ),
130+ builder .WithPredicates (clusterChangedPredicate () ),
127131 ).
128132 WithEventFilter (sharding .FilterByShardID (r .ShardID )).
129133 WithOptions (controller.Options {MaxConcurrentReconciles : r .Workers }).
130134 Complete (r )
131135}
132136
137+ // clusterChangedPredicate filters cluster events that relate to bundldeployment creation.
138+ func clusterChangedPredicate () predicate.Funcs {
139+ return predicate.Funcs {
140+ CreateFunc : func (e event.CreateEvent ) bool {
141+ return true
142+ },
143+ UpdateFunc : func (e event.UpdateEvent ) bool {
144+ n := e .ObjectNew .(* fleet.Cluster )
145+ o := e .ObjectOld .(* fleet.Cluster )
146+ // cluster deletion will eventually trigger a delete event
147+ if n == nil || ! n .DeletionTimestamp .IsZero () {
148+ return true
149+ }
150+ // labels and annotations are used for templating and targeting
151+ if ! maps .Equal (n .Labels , o .Labels ) {
152+ return true
153+ }
154+ if ! maps .Equal (n .Annotations , o .Annotations ) {
155+ return true
156+ }
157+ // spec templateValues is used in templating
158+ if ! reflect .DeepEqual (n .Spec , o .Spec ) {
159+ return true
160+ }
161+ // this namespace contains the bundledeployments
162+ if n .Status .Namespace != o .Status .Namespace {
163+ return true
164+ }
165+ // this namespace indicates the agent is running
166+ if n .Status .Agent .Namespace != o .Status .Agent .Namespace {
167+ return true
168+ }
169+
170+ return false
171+ },
172+ DeleteFunc : func (e event.DeleteEvent ) bool {
173+ return true
174+ },
175+ }
176+ }
177+
133178//+kubebuilder:rbac:groups=fleet.cattle.io,resources=bundles,verbs=get;list;watch;create;update;patch;delete
134179//+kubebuilder:rbac:groups=fleet.cattle.io,resources=bundles/status,verbs=get;update;patch
135180//+kubebuilder:rbac:groups=fleet.cattle.io,resources=bundles/finalizers,verbs=update
@@ -294,8 +339,9 @@ func (r *BundleReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
294339 continue
295340 }
296341
297- // NOTE we don't use the existing BundleDeployment, we discard annotations, status, etc
298- // copy labels from Bundle as they might have changed
342+ // NOTE we don't re-use the existing BundleDeployment, we discard annotations, status, etc.
343+ // and copy labels from Bundle as they might have changed.
344+ // However, matchedTargets target.Deployment contains existing BundleDeployments.
299345 bd := target .BundleDeployment ()
300346
301347 // No need to check the deletion timestamp here before adding a finalizer, since the bundle has just
0 commit comments