Skip to content

Commit

Permalink
Merge pull request #604 from PocketMobsters/task/namespace-scoping-cache
Browse files Browse the repository at this point in the history
Allow scoping cache to specific namespaces
  • Loading branch information
Zerpet authored May 4, 2023
2 parents d02b0b1 + c1deb1f commit cb822d6
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ package main
import (
"flag"
"fmt"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"os"
"regexp"
"strconv"
"strings"
"time"

utilruntime "k8s.io/apimachinery/pkg/util/runtime"

corev1 "k8s.io/api/core/v1"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/rabbitmq/messaging-topology-operator/rabbitmqclient"
Expand Down Expand Up @@ -95,6 +97,9 @@ func main() {
os.Exit(1)
}

// If the environment variable is not set Getenv returns an empty string which ctrl.Options.Namespace takes to mean all namespaces should be watched
operatorScopeNamespace := os.Getenv("OPERATOR_SCOPE_NAMESPACE")

clusterDomain := sanitizeClusterDomainInput(os.Getenv(controllers.KubernetesInternalDomainEnvVar))

managerOpts := ctrl.Options{
Expand All @@ -103,6 +108,12 @@ func main() {
LeaderElection: true,
LeaderElectionNamespace: operatorNamespace,
LeaderElectionID: "messaging-topology-operator-leader-election",
Namespace: operatorScopeNamespace,
}

if strings.Contains(operatorScopeNamespace, ",") {
managerOpts.Namespace = ""
managerOpts.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(operatorScopeNamespace, ","))
}

if syncPeriod := os.Getenv(controllers.ControllerSyncPeriodEnvVar); syncPeriod != "" {
Expand Down

0 comments on commit cb822d6

Please sign in to comment.