Skip to content

Commit bcb44e3

Browse files
authored
Filter cluster events to trigger bundle deployment creation less often (#3796)
1 parent 2e8d6d9 commit bcb44e3

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

internal/cmd/controller/agentmanagement/controllers/cluster/import.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func (i *importHandler) OnChange(key string, cluster *fleet.Cluster) (_ *fleet.C
204204
return cluster, nil
205205
}
206206

207+
// cluster.spec.KubeConfigSecret is empty when agent-initiated registration is used
207208
if cluster.Spec.KubeConfigSecret == "" || agentDeployed(cluster) {
208209
return cluster, nil
209210
}

internal/cmd/controller/reconciler/bundle_controller.go

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
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

Comments
 (0)