From 38c7364896957bff0948a1f0120b55c9d2907a32 Mon Sep 17 00:00:00 2001 From: desmax74 Date: Tue, 7 Oct 2025 11:44:57 +0200 Subject: [PATCH] Instructions to observe only the namespace where the operator is installed Signed-off-by: desmax74 --- README.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/README.md b/README.md index 5123a9df..5acf6df3 100644 --- a/README.md +++ b/README.md @@ -41,3 +41,55 @@ if err := reconciler.SetupWithManager(mgr); err != nil { panic(fmt.Sprintf("unable to create reconciler: %s", err)) } ``` + +### Creating a Helm reconciler with multiple namespace installation + +Add the WATCH_NAMESPACE to the manager files to restrict the namespace to observe where the operator is installed + +```json +name: manager +env: + - name: WATCH_NAMESPACE + valueFrom: + fieldRef: + fieldPath: metadata.namespace +``` + +Filter the events to the namespace where the operator is installed + +```go +// Operator's main.go +watchNamespace = os.Getenv("WATCH_NAMESPACE") + +var cacheOpts cache.Options +if watchNamespace != "" { + setupLog.Info("Watching specific namespace", "namespace", watchNamespace) + cacheOpts = cache.Options{ + DefaultNamespaces: map[string]cache.Config{ + watchNamespace: {}, + }, + } +} else { + setupLog.Info("Watching all namespaces") +} + +mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ + ... + Cache: cacheOpts, +}) + +... +chart, err := loader.Load("path/to/chart") +if err != nil { + panic(err) +} + +reconciler := reconciler.New( + reconciler.WithChart(*chart), + reconciler.WithGroupVersionKind(gvk), +) + +if err := reconciler.SetupWithManager(mgr); err != nil { + panic(fmt.Sprintf("unable to create reconciler: %s", err)) +} +```