Skip to content
Open
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
7 changes: 7 additions & 0 deletions internal/cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,12 @@ func NewCmd() *cobra.Command {

_ = viper.BindEnv("sidecar-image", "SIDECAR_IMAGE")

cmd.Flags().String(
"watch-namespace",
"",
"The namespace to watch for CloudNativePG clusters. If empty, all namespaces will be watched.",
)
_ = viper.BindPFlag("watch-namespace", cmd.Flags().Lookup("watch-namespace"))

return cmd
}
10 changes: 10 additions & 0 deletions internal/cnpgi/operator/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
Expand Down Expand Up @@ -102,6 +103,14 @@ func Start(ctx context.Context) error {
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
}

var cacheOptions cache.Options
if viper.GetString("watch-namespace") != "" {
setupLog.Info("Watching a single namespace", "namespace", viper.GetString("watch-namespace"))
cacheOptions = cache.Options{
DefaultNamespaces: map[string]cache.Config{viper.GetString("watch-namespace"): {}},
}
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsServerOptions,
Expand All @@ -120,6 +129,7 @@ func Start(ctx context.Context) error {
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
LeaderElectionReleaseOnCancel: true,
Cache: cacheOptions,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down