Skip to content

Commit

Permalink
Try to fix cache for objects in different namespaces
Browse files Browse the repository at this point in the history
Signed-off-by: Douglas Camata <[email protected]>
  • Loading branch information
douglascamata committed May 27, 2024
1 parent b7806b8 commit 4f65b12
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
15 changes: 8 additions & 7 deletions operators/endpointmetrics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
v1 "k8s.io/api/core/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -87,14 +88,15 @@ func main() {

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

watchNamespace := os.Getenv("WATCH_NAMESPACE")
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: metricsAddr},
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "7c30ca38.open-cluster-management.io",
NewCache: func(config *rest.Config, opts cache.Options) (cache.Cache, error) {
namespaceFieldSelector := fields.Set{"metadata.namespace": os.Getenv("WATCH_NAMESPACE")}.AsSelector()
namespaceFieldSelector := fields.Set{"metadata.namespace": watchNamespace}.AsSelector()

// The following RBAC resources will not be watched by MCO, the selector will not impact the mco behavior, which
// means MCO will fetch kube-apiserver for the correspoding resource if the resource can't be found in the cache.
Expand All @@ -104,11 +106,10 @@ func main() {
Field: namespaceFieldSelector,
},
&v1.ConfigMap{}: {
Field: namespaceFieldSelector,
},
&v1.ConfigMap{}: {
Field: fields.ParseSelectorOrDie(fmt.Sprintf("metadata.name==%s,metadata.namespace!=open-cluster-management-observability",
operatorconfig.AllowlistCustomConfigMapName)),
Namespaces: map[string]cache.Config{
watchNamespace: {LabelSelector: labels.Everything()},
cache.AllNamespaces: {FieldSelector: fields.ParseSelectorOrDie(fmt.Sprintf("metadata.name==%s", operatorconfig.AllowlistCustomConfigMapName))},
},
},
&appsv1.Deployment{}: {
Field: namespaceFieldSelector,
Expand Down Expand Up @@ -150,7 +151,7 @@ func main() {

namespace := os.Getenv("NAMESPACE")
if namespace == "" {
namespace = os.Getenv("WATCH_NAMESPACE")
namespace = watchNamespace
}
if err = (&statusctl.StatusReconciler{
Client: mgr.GetClient(),
Expand Down
16 changes: 11 additions & 5 deletions operators/multiclusterobservability/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,18 @@ func main() {
// means MCO will fetch kube-apiserver for the correspoding resource if the resource can't be found in the cache.
// Adding selector will reduce the cache size when the managedcluster scale.
opts.ByObject = map[client.Object]cache.ByObject{
&corev1.Secret{}: byObjectWithDefaultNamespace,
&corev1.Secret{}: {
Field: fields.Set{"metadata.namespace": mcoconfig.OpenshiftIngressOperatorCRName}.AsSelector(),
},
&corev1.Secret{}: {
Field: fields.Set{"metadata.namespace": mcoconfig.OpenshiftIngressOperatorNamespace}.AsSelector(),
Namespaces: map[string]cache.Config{
mcoconfig.GetDefaultNamespace(): {
LabelSelector: labels.Everything(),
},
mcoconfig.OpenshiftIngressOperatorNamespace: {
LabelSelector: labels.Everything(),
},
mcoconfig.OpenshiftIngressNamespace: {
LabelSelector: labels.Everything(),
},
},
},
&corev1.ConfigMap{}: byObjectWithDefaultNamespace,
&corev1.Service{}: byObjectWithDefaultNamespace,
Expand Down

0 comments on commit 4f65b12

Please sign in to comment.