From ee3981f8d99bc237321c74f96e7c8662d359ebbf Mon Sep 17 00:00:00 2001 From: Steve Kuznetsov Date: Thu, 22 Jun 2023 06:43:22 -0600 Subject: [PATCH] manager: filter the cache of configmaps This controller LISTs and WATCHes ConfigMaps in order to react to changes to one and only one ConfigMap - the controller's (legacy) configuration. Previously, this controller would hold the entire cluster's ConfigMaps in cache in order to do this; with this change, we will only ever watch one ConfigMap and hold it in memory. This is unfortunately a cross-cutting concern and it blurs the control flow that previously exited, but we need these options to be passed to the cache constructor at the moment we create the manager, so it's not clear that there is a factoring that keeps this from happening until the controller initialization code runs, like where we add filtering today. Signed-off-by: Steve Kuznetsov --- pkg/cmd/operator/cmd.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/cmd/operator/cmd.go b/pkg/cmd/operator/cmd.go index 659020afd5..616e8f1360 100644 --- a/pkg/cmd/operator/cmd.go +++ b/pkg/cmd/operator/cmd.go @@ -29,9 +29,15 @@ import ( "github.com/golang/glog" "github.com/google/uuid" + "github.com/openshift/cloud-credential-operator/pkg/operator/constants" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/fields" + "k8s.io/client-go/rest" + "sigs.k8s.io/controller-runtime/pkg/cache" + "sigs.k8s.io/controller-runtime/pkg/client" minterv1 "github.com/openshift/cloud-credential-operator/pkg/apis/cloudcredential/v1" controller "github.com/openshift/cloud-credential-operator/pkg/operator" @@ -88,6 +94,18 @@ func NewOperator() *cobra.Command { log.Info("setting up manager") mgr, err := manager.New(cfg, manager.Options{ MetricsBindAddress: ":2112", + NewCache: func(config *rest.Config, opts cache.Options) (cache.Cache, error) { + if opts.SelectorsByObject == nil { + opts.SelectorsByObject = map[client.Object]cache.ObjectSelector{} + } + opts.SelectorsByObject[&corev1.ConfigMap{}] = cache.ObjectSelector{ + Field: fields.SelectorFromSet(fields.Set{ + "metadata.namespace": minterv1.CloudCredOperatorNamespace, + "metadata.name": constants.CloudCredOperatorConfigMap, + }), + } + return cache.New(config, opts) + }, }) if err != nil { log.WithError(err).Fatal("unable to set up overall controller manager")