Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix: reduce unnecessary caching and remove predicate from dynamic watches #963

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"

catalogd "github.com/operator-framework/catalogd/api/core/v1alpha1"
helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client"
registryv1handler "github.com/operator-framework/rukpak/pkg/handler"
"github.com/operator-framework/rukpak/pkg/provisioner/registry"
Expand Down Expand Up @@ -127,12 +128,13 @@ func main() {
LeaderElectionID: "9c4404e7.operatorframework.io",
Cache: crcache.Options{
ByObject: map[client.Object]crcache.ByObject{
&ocv1alpha1.ClusterExtension{}: {},
&ocv1alpha1.ClusterExtension{}: {Label: k8slabels.Everything()},
&catalogd.ClusterCatalog{}: {Label: k8slabels.Everything()},
},
DefaultNamespaces: map[string]crcache.Config{
systemNamespace: {},
crcache.AllNamespaces: {LabelSelector: dependentSelector},
systemNamespace: {LabelSelector: k8slabels.Everything()},
},
DefaultLabelSelector: dependentSelector,
},
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
Expand Down
35 changes: 17 additions & 18 deletions internal/controllers/clusterextension_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"k8s.io/apimachinery/pkg/util/sets"
apimachyaml "k8s.io/apimachinery/pkg/util/yaml"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
crcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
Expand All @@ -63,7 +64,6 @@ import (
"github.com/operator-framework/operator-registry/alpha/property"
rukpakv1alpha2 "github.com/operator-framework/rukpak/api/v1alpha2"
registryv1handler "github.com/operator-framework/rukpak/pkg/handler"
helmpredicate "github.com/operator-framework/rukpak/pkg/helm-operator-plugins/predicate"
rukpaksource "github.com/operator-framework/rukpak/pkg/source"
"github.com/operator-framework/rukpak/pkg/storage"
"github.com/operator-framework/rukpak/pkg/util"
Expand Down Expand Up @@ -357,7 +357,6 @@ func (r *ClusterExtensionReconciler) reconcile(ctx context.Context, ext *ocv1alp
source.Kind(r.cache,
obj,
crhandler.EnqueueRequestForOwner(r.Scheme(), r.RESTMapper(), ext, crhandler.OnlyControllerOwner()),
helmpredicate.DependentPredicateFuncs[client.Object](),
),
); err != nil {
return err
Expand Down Expand Up @@ -572,24 +571,24 @@ func (r *ClusterExtensionReconciler) SetupWithManager(mgr ctrl.Manager) error {
controller, err := ctrl.NewControllerManagedBy(mgr).
For(&ocv1alpha1.ClusterExtension{}).
Watches(&catalogd.ClusterCatalog{},
crhandler.EnqueueRequestsFromMapFunc(clusterExtensionRequestsForCatalog(mgr.GetClient(), mgr.GetLogger()))).
WithEventFilter(predicate.Funcs{
UpdateFunc: func(ue event.UpdateEvent) bool {
oldObject, isOldCatalog := ue.ObjectOld.(*catalogd.ClusterCatalog)
newObject, isNewCatalog := ue.ObjectNew.(*catalogd.ClusterCatalog)

if !isOldCatalog || !isNewCatalog {
return true
}
crhandler.EnqueueRequestsFromMapFunc(clusterExtensionRequestsForCatalog(mgr.GetClient(), mgr.GetLogger())),
builder.WithPredicates(predicate.Funcs{
Copy link
Contributor

@perdasilva perdasilva Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A tab walks into a bar sits down. The barkeep asks him, "why the long space?"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 Yeah, the formatting looks weird because the predicate needs to go with the ClusterCatalog watch, and it means it all gets indented by an extra tab.

UpdateFunc: func(ue event.UpdateEvent) bool {
oldObject, isOldCatalog := ue.ObjectOld.(*catalogd.ClusterCatalog)
newObject, isNewCatalog := ue.ObjectNew.(*catalogd.ClusterCatalog)

if !isOldCatalog || !isNewCatalog {
return true
}

if oldObject.Status.ResolvedSource != nil && newObject.Status.ResolvedSource != nil {
if oldObject.Status.ResolvedSource.Image != nil && newObject.Status.ResolvedSource.Image != nil {
return oldObject.Status.ResolvedSource.Image.ResolvedRef != newObject.Status.ResolvedSource.Image.ResolvedRef
if oldObject.Status.ResolvedSource != nil && newObject.Status.ResolvedSource != nil {
if oldObject.Status.ResolvedSource.Image != nil && newObject.Status.ResolvedSource.Image != nil {
return oldObject.Status.ResolvedSource.Image.ResolvedRef != newObject.Status.ResolvedSource.Image.ResolvedRef
}
}
}
return true
},
}).
return true
},
})).
Build(r)

if err != nil {
Expand Down
Loading