Skip to content

Commit a586eaa

Browse files
authored
Instructions to observe only the namespace where the operator is installed (#495)
Signed-off-by: desmax74 <[email protected]>
1 parent 05a6963 commit a586eaa

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,55 @@ if err := reconciler.SetupWithManager(mgr); err != nil {
4141
panic(fmt.Sprintf("unable to create reconciler: %s", err))
4242
}
4343
```
44+
45+
### Creating a Helm reconciler with multiple namespace installation
46+
47+
Add the WATCH_NAMESPACE to the manager files to restrict the namespace to observe where the operator is installed
48+
49+
```json
50+
name: manager
51+
env:
52+
- name: WATCH_NAMESPACE
53+
valueFrom:
54+
fieldRef:
55+
fieldPath: metadata.namespace
56+
```
57+
58+
Filter the events to the namespace where the operator is installed
59+
60+
```go
61+
// Operator's main.go
62+
watchNamespace = os.Getenv("WATCH_NAMESPACE")
63+
64+
var cacheOpts cache.Options
65+
if watchNamespace != "" {
66+
setupLog.Info("Watching specific namespace", "namespace", watchNamespace)
67+
cacheOpts = cache.Options{
68+
DefaultNamespaces: map[string]cache.Config{
69+
watchNamespace: {},
70+
},
71+
}
72+
} else {
73+
setupLog.Info("Watching all namespaces")
74+
}
75+
76+
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
77+
...
78+
Cache: cacheOpts,
79+
})
80+
81+
...
82+
chart, err := loader.Load("path/to/chart")
83+
if err != nil {
84+
panic(err)
85+
}
86+
87+
reconciler := reconciler.New(
88+
reconciler.WithChart(*chart),
89+
reconciler.WithGroupVersionKind(gvk),
90+
)
91+
92+
if err := reconciler.SetupWithManager(mgr); err != nil {
93+
panic(fmt.Sprintf("unable to create reconciler: %s", err))
94+
}
95+
```

0 commit comments

Comments
 (0)