Skip to content

Commit 646c6b0

Browse files
author
augusto.ribeiro
committed
adding a watch-namespace option in order to only watch one namespace if set
Signed-off-by: augusto.ribeiro <augusto.ribeiro@cloudkitchens.com>
1 parent 064eac2 commit 646c6b0

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

internal/cmd/operator/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,12 @@ func NewCmd() *cobra.Command {
103103

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

106+
cmd.Flags().String(
107+
"watch-namespace",
108+
"",
109+
"The namespace to watch for CloudNativePG clusters. If empty, all namespaces will be watched.",
110+
)
111+
_ = viper.BindPFlag("watch-namespace", cmd.Flags().Lookup("watch-namespace"))
112+
106113
return cmd
107114
}

internal/cnpgi/operator/manager.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3232
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3333
ctrl "sigs.k8s.io/controller-runtime"
34+
"sigs.k8s.io/controller-runtime/pkg/cache"
3435
"sigs.k8s.io/controller-runtime/pkg/healthz"
3536
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
3637
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
@@ -102,6 +103,14 @@ func Start(ctx context.Context) error {
102103
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
103104
}
104105

106+
var cacheOptions cache.Options
107+
if viper.GetString("watch-namespace") != "" {
108+
setupLog.Info("Watching a single namespace", "namespace", viper.GetString("watch-namespace"))
109+
cacheOptions = cache.Options{
110+
DefaultNamespaces: map[string]cache.Config{viper.GetString("watch-namespace"): {}},
111+
}
112+
}
113+
105114
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
106115
Scheme: scheme,
107116
Metrics: metricsServerOptions,
@@ -120,6 +129,7 @@ func Start(ctx context.Context) error {
120129
// if you are doing or is intended to do any operation such as perform cleanups
121130
// after the manager stops then its usage might be unsafe.
122131
LeaderElectionReleaseOnCancel: true,
132+
Cache: cacheOptions,
123133
})
124134
if err != nil {
125135
setupLog.Error(err, "unable to start manager")

0 commit comments

Comments
 (0)