Skip to content

Commit 16cf334

Browse files
authored
fix: reduce unnecessary caching and remove predicate from dynamic watches (#963)
Our cache configuration resulted in watching all cluster-scoped resources for each cluster-scoped resource that shows up in a ClusterExtension. Now, we only watch: - All ClusterExtensions - All ClusterCatalogs - All objects in the systemNamespace - All other objects that specifically match the dependentPredicate This commit also removes the dependent predicate so that we reconcile for any change to managed resources. This is important to ensure that future features (like health checking) can account for any change to managed operator objects. Signed-off-by: Joe Lanford <[email protected]>
1 parent 58b4363 commit 16cf334

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

cmd/manager/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3838
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
3939

40+
catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
4041
helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client"
4142
registryv1handler "github.com/operator-framework/rukpak/pkg/handler"
4243
"github.com/operator-framework/rukpak/pkg/provisioner/registry"
@@ -127,12 +128,13 @@ func main() {
127128
LeaderElectionID: "9c4404e7.operatorframework.io",
128129
Cache: crcache.Options{
129130
ByObject: map[client.Object]crcache.ByObject{
130-
&ocv1alpha1.ClusterExtension{}: {},
131+
&ocv1alpha1.ClusterExtension{}: {Label: k8slabels.Everything()},
132+
&catalogd.ClusterCatalog{}: {Label: k8slabels.Everything()},
131133
},
132134
DefaultNamespaces: map[string]crcache.Config{
133-
systemNamespace: {},
134-
crcache.AllNamespaces: {LabelSelector: dependentSelector},
135+
systemNamespace: {LabelSelector: k8slabels.Everything()},
135136
},
137+
DefaultLabelSelector: dependentSelector,
136138
},
137139
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
138140
// when the Manager ends. This requires the binary to immediately end when the

internal/controllers/clusterextension_controller.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"k8s.io/apimachinery/pkg/util/sets"
4646
apimachyaml "k8s.io/apimachinery/pkg/util/yaml"
4747
ctrl "sigs.k8s.io/controller-runtime"
48+
"sigs.k8s.io/controller-runtime/pkg/builder"
4849
"sigs.k8s.io/controller-runtime/pkg/cache"
4950
"sigs.k8s.io/controller-runtime/pkg/client"
5051
crcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
@@ -63,7 +64,6 @@ import (
6364
"github.com/operator-framework/operator-registry/alpha/property"
6465
rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2"
6566
registryv1handler "github.com/operator-framework/rukpak/pkg/handler"
66-
helmpredicate "github.com/operator-framework/rukpak/pkg/helm-operator-plugins/predicate"
6767
rukpaksource "github.com/operator-framework/rukpak/pkg/source"
6868
"github.com/operator-framework/rukpak/pkg/storage"
6969
"github.com/operator-framework/rukpak/pkg/util"
@@ -357,7 +357,6 @@ func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1alp
357357
source.Kind(r.cache,
358358
obj,
359359
crhandler.EnqueueRequestForOwner(r.Scheme(), r.RESTMapper(), ext, crhandler.OnlyControllerOwner()),
360-
helmpredicate.DependentPredicateFuncs[client.Object](),
361360
),
362361
); err != nil {
363362
return err
@@ -572,24 +571,24 @@ func (r *ClusterExtensionReconciler) SetupWithManager(mgr ctrl.Manager) error {
572571
controller, err := ctrl.NewControllerManagedBy(mgr).
573572
For(&ocv1alpha1.ClusterExtension{}).
574573
Watches(&catalogd.ClusterCatalog{},
575-
crhandler.EnqueueRequestsFromMapFunc(clusterExtensionRequestsForCatalog(mgr.GetClient(), mgr.GetLogger()))).
576-
WithEventFilter(predicate.Funcs{
577-
UpdateFunc: func(ue event.UpdateEvent) bool {
578-
oldObject, isOldCatalog := ue.ObjectOld.(*catalogd.ClusterCatalog)
579-
newObject, isNewCatalog := ue.ObjectNew.(*catalogd.ClusterCatalog)
580-
581-
if !isOldCatalog || !isNewCatalog {
582-
return true
583-
}
574+
crhandler.EnqueueRequestsFromMapFunc(clusterExtensionRequestsForCatalog(mgr.GetClient(), mgr.GetLogger())),
575+
builder.WithPredicates(predicate.Funcs{
576+
UpdateFunc: func(ue event.UpdateEvent) bool {
577+
oldObject, isOldCatalog := ue.ObjectOld.(*catalogd.ClusterCatalog)
578+
newObject, isNewCatalog := ue.ObjectNew.(*catalogd.ClusterCatalog)
579+
580+
if !isOldCatalog || !isNewCatalog {
581+
return true
582+
}
584583

585-
if oldObject.Status.ResolvedSource != nil && newObject.Status.ResolvedSource != nil {
586-
if oldObject.Status.ResolvedSource.Image != nil && newObject.Status.ResolvedSource.Image != nil {
587-
return oldObject.Status.ResolvedSource.Image.ResolvedRef != newObject.Status.ResolvedSource.Image.ResolvedRef
584+
if oldObject.Status.ResolvedSource != nil && newObject.Status.ResolvedSource != nil {
585+
if oldObject.Status.ResolvedSource.Image != nil && newObject.Status.ResolvedSource.Image != nil {
586+
return oldObject.Status.ResolvedSource.Image.ResolvedRef != newObject.Status.ResolvedSource.Image.ResolvedRef
587+
}
588588
}
589-
}
590-
return true
591-
},
592-
}).
589+
return true
590+
},
591+
})).
593592
Build(r)
594593

595594
if err != nil {

0 commit comments

Comments
 (0)